火狐插件窗口。addEventListener错误:没有定义窗口。

时间:2022-08-04 04:39:22

i'm trying to follow this tutorial for creating a firefox addon that intercept when the url in the address bar change:

我正在尝试按照这个教程来创建一个firefox addon,当地址栏的url改变时,它会拦截。

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

https://developer.mozilla.org/en-US/Add-ons/Code_snippets/Progress_Listeners的例子:_Notification_when_the_value_in_Address_Bar_changes

I just copied the code, and just added an alert to see if it works, but i can't have it run in any way.

我只是复制了代码,只是添加了一个警告,看看它是否有效,但我不能让它以任何方式运行。

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 start/test this extension i receive 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.

Probably it's me that i don't understand something from the tutorial/extension creation process.

可能是我不理解教程/扩展创建过程中的一些东西。

Can you help me to understand what is missing?

你能帮我了解一下缺少什么吗?

EDIT After kapep answer, i resolved the window not defined error. But it seems that nothing happen when i change the url in the address bar. Any idea?

在kapep回答后,我解决了窗口未定义的错误。但是,当我在地址栏中更改url时,似乎什么也没有发生。任何想法?

1 个解决方案

#1


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.

没有全局窗口或gBrowser对象,您需要获取浏览器并选择要添加侦听器的窗口(nsIDOMWindow)。在本教程中,这部分似乎缺少或超出了范围。

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:

有很多方法可以得到一个窗口。我将使用低层的windowUtils API。你可以得到最近的一个,比如最近的一个浏览器窗口,或者更可靠的,所有当前打开的窗口都有windowUtils.windows()

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);
});

#1


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.

没有全局窗口或gBrowser对象,您需要获取浏览器并选择要添加侦听器的窗口(nsIDOMWindow)。在本教程中,这部分似乎缺少或超出了范围。

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:

有很多方法可以得到一个窗口。我将使用低层的windowUtils API。你可以得到最近的一个,比如最近的一个浏览器窗口,或者更可靠的,所有当前打开的窗口都有windowUtils.windows()

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);
});