Change code to work on main window

Hi,
ive been trying to learn to code a firefox addon and came across this code, It will open the link into a new tab from a panel. But how can I change the code so it work from links from main window not in the panel. Ive been trying without luck, please help
cheers

//index.js
var data = require(“sdk/self”).data;

var button = require(“sdk/ui/button/action”).ActionButton({
id: “reddit-panel”,
label: “Reddit Panel”,
icon: “./icon-16.png”,
onClick: function() {
reddit_panel.show();
}
});

var reddit_panel = require(“sdk/panel”).Panel({
width: 240,
height: 320,
contentURL: “http://www.reddit.com/.mobile?keep_extension=True”,
contentScriptFile: [data.url(“jquery-2.1.0.min.js”),
data.url(“panel.js”)]
});

reddit_panel.port.on(“click”, function(url) {
require(“sdk/tabs”).open(url);
});


//panel.js

$(window).click(function (event) {
var t = event.target;

// Don’t intercept the click if it isn’t on a link.

if (t.nodeName != “A”)
return;

// Don’t intercept the click if it was on one of the links in the header
// or next/previous footer, since those links should load in the panel itself.

if ($(t).parents(’#header’).length || $(t).parents(’.nextprev’).length)
return;

// Intercept the click, passing it to the addon, which will load it in a tab.

event.stopPropagation();
event.preventDefault();
self.port.emit(‘click’, t.toString());
});

If I understand you correctly, you want to change the behavior of links in the main content area. If that’s the case, what you want to do is use a page-mod to change content behavior.

1 Like

Thank you for your reply I have been trying that like this, without much luck

//index.js

var tabs = require(“sdk/tabs”);
var pageMod = require(“sdk/page-mod”);

pageMod.PageMod({
include: “*”,
contentScriptFile: [data.url("./jquery-2.1.4.min.js"),
data.url("./panel.js")]
});
pageMod.port.on(“click”, function(url) {
tabs.open(url);
});

but leaving panel.js unchanged

In your contentScriptFile of .panel.js you have to do something like:

var as = document.querySelectorAll('a');
for (var i=0; i<as.length; i++) {
    as[i].setAttribute('target', '_blank');
}