Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firefox addon: new tab extension

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?

like image 882
nicolausYes Avatar asked Mar 20 '23 04:03

nicolausYes


1 Answers

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.

like image 118
nmaier Avatar answered Apr 01 '23 04:04

nmaier