Search code examples
javascriptfirefox-addon-sdkmozilla

Any way to restrict targets of page-mod in Mozilla Add-On SDK


I am currently working on a Firefox add-on using an SDK which modifies a text content of the page in a specific way. However, I ran into the following problem.

Let's say I have 2 tabs, both of which have the same site loaded into them. The site has multiple iframes, some cross-domain. I need to update only the page in the tab I am currently on.

If I add a worker to a tab using tab.attach(), I can inject my content-altering code to just that tab so all is great. Except it doesn't affect iframes.

In order to get to the iframes, I have to to use PageMod using something like this:

tabs.on('ready', function(tab) {
    pageMod.PageMod({
            include: tab.url,
            contentScriptFile: self.data.url('./pagemod.js'),
            attachTo: ["top", "frame", "existing"],
            onAttach: function(worker) {
                worker.on('message', function(msg){
                    me.handleMessage(msg);
                });
                tracker.workers.push(worker);
            }
        });
    } 
});

It works fine because it attaches workers to the frames as well as the top document, so I can inject my code into all of them. However, it also affects the other page that I have open in the second tab, since it has the same url.

Is there any way that I can limit the page mod effects only to a current tab? I don't find anything obvious but maybe I've just been looking at it for too long.

Thanks!


Solution

  • Each tab has an id and you can always access the current active tab this way. So in onAttach attribute you can do if tab.id == tabs.activeTab.id { doSomethingElse(); }.