Firefox Add-On window.addEventListener error: window not defined

I am trying to follow this guide to create a firefox addon that intercepts when the url in the address bar changes:

https://developer.mozilla.org/en-US/Add-ons/Code_snippets/Progress_Listeners#Example:_Notification_when_the_value_in_Address_Bar_changes

I simply copied the code and simply added a warning to find out if it works, but I cannot run it.

My code is:

const {Cu} = require("chrome");
Cu.import("resource://gre/modules/XPCOMUtils.jsm", this);

var urlListener = {
    oldURL: null,

    init: function() {
        gBrowser.addProgressListener(this);
    },

    uninit: function() {
        gBrowser.removeProgressListener(this);
    },

    processNewURL: function(aURI) {
        if (aURI.spec == this.oldURL) return;

        // now we know the url is new...
        alert(aURI.spec);
        this.oldURL = aURI.spec;
    },

    // nsIWebProgressListener
    QueryInterface: XPCOMUtils.generateQI(["nsIWebProgressListener",
                                           "nsISupportsWeakReference"]),

    onLocationChange: function(aProgress, aRequest, aURI) {
    alert("Called");
        this.processNewURL(aURI);
    },

    onStateChange: function() {},
    onProgressChange: function() {},
    onStatusChange: function() {},
    onSecurityChange: function() {}
};

  window.addEventListener("load", function() { urlListener.init() }, false);
  window.addEventListener("unload", function() { urlListener.uninit() }, false);

Whenever I try to run / test this extension, I get the following error:

Running tests on Firefox 24.3.0/Gecko 24.3.0 ({ec8030f7-c20a-464f-9b0e-13a3a9e97384}) under linux/x86-gcc3.
Error: ReferenceError: window is not defined 
 Traceback (most recent call last):
  File "resource://jid1-u33krawc1uacsa-at-jetpack/amazon_rewriter/tests/test-main.js", line 1, in 
    var main = require("./main");
  File "resource://gre/modules/commonjs/sdk/loader/cuddlefish.js", line 133, in CuddlefishLoader/options<.load
    result = load(loader, module);
  File "resource://jid1-u33krawc1uacsa-at-jetpack/amazon_rewriter/lib/main.js", line 38, in 
    window.addEventListener("load", function() { urlListener.init() }, false);
0 of 1 tests passed.

This is probably to me that I do not understand anything from the process of creating a textbook / extension.

Can you help me understand what is missing?

kapep , . , , URL- . ?

+1
1

window gBrowser, , (nsIDOMWindow) . , -, .

var gBrowser = windowUtils.getMostRecentBrowserWindow().getBrowser();

, . , windowUtils API. , , getMostRecentBrowserWindow windowUtils.windows() :

const windowUtils = require("sdk/window/utils");

for each (let window in windowUtils.windows()) {
    urlListener.init();
    window.addEventListener("unload", function() { urlListener.uninit(); }, false);
}

, , :

const windows = require("sdk/windows");
windows.browserWindows.on("open", domWindow => {
    urlListener.init();
    windowUtils.getMostRecentBrowserWindow().addEventListener("unload", function() { urlListener.uninit(); }, false);
});
+1

Source: https://habr.com/ru/post/1546534/


All Articles