Skip to content Skip to sidebar Skip to footer

Firefox Add-On Window.addEventListener Error: Window Not Defined

i'm trying to follow this tutorial for creating a firefox addon that intercept when the url in the address bar change: https://developer.mozilla.org/en-US/Add-ons/Code_snippets/Pr

Solution 1:

There are no global window or gBrowser objects, you need to get the browser and choose which window (nsIDOMWindow) you want to add the listener. This part seems to be missing or out of scope in the tutorial.

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

There are probably multiple ways to get a window. I would do this is using the low-level windowUtils API. You could get the most recent one like above with getMostRecentBrowserWindow or more reliable get all currently opened windows with windowUtils.windows() like this:

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

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

Just in case you also want to add the listener to all windows opened in the future, you can add them when a new window opens:

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

Post a Comment for "Firefox Add-On Window.addEventListener Error: Window Not Defined"