4
votes

I need to do some actions when new tab in Firefox is opened. I'm using addon-sdk and I'm totally new to firefox extensions development.

I downloaded some new tab extensions in store, unpack them and mostly of them use such code:

var newtab = {
    init: function ()
    {
        gBrowser.addEventListener("NewTab", newtab.opentab, false);
    },

    opentab: function (aEvent)
    {
        // action here
    }
}

window.addEventListener( "load", newtab.init, false);

They are subscribing on load event of window, then they guaranteed have gBrowser to subscribe on new tab opened event.

When I'm trying to do this, I'm getting:

Message: ReferenceError: window is not defined

As far as I understood, there is no window object in that context.

Firefox Addon-sdk. On page load

StackOverflow – Firefos Addon: eventlistener: windows is not defined

According to sdk and SO answer, It's possible to do it described way, the only difference between my extension and downloaded extensions from the store (and above topics), that I haven't any xul files, because I haven't any UI.

I also tried to use code from SO answer Firefox Add-On window.addEventListener error: window not defined, but the Firefox is crashing.

How to do it right way? Should I have any xul files if I have no UI?

2
Did you come to a working solution ? Can you share it ? Thanks.enguerranws

2 Answers

4
votes

Thar code you're quoting is regular XUL overlay code, and does not apply to the SDK (well, it could be made to work in the SDK by jumping through a lot of hoops, but that's beside the point).

The SDK provides APIs for dealing with tabs in the sdk/tabs module. You should use that.

3
votes

Try this:

var data = require('sdk/self').data;

require('sdk/page-mod').PageMod({
  include: ["about:newtab"],
  //contentScriptFile: [data.url('cs.js')], // <<< you dont need this unless you want to run stuff inside the about:newtab page
  attachTo: ["existing", "top"], // <<<< im not sure what this does
  onAttach: function(worker) {
    worker.port.emit('attached', true);
  }
});

self.port.on('attached', function() {
  console.log('new tab page loaded!!!');
});

disclaimer: im not an sdk guy, this might fail, so you might have to tweak it after implementing, but im pretty sure its in the right direction