Firefox加载项window.addEventListener错误:未定义窗口(Firefox Add-On window.addEventListener error: window not defined)

其他开发 IT屋
百度翻译此文   有道翻译此文
问 题

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/Progress_Listeners#Example:_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?

解决方案

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

本文地址:IT屋 » Firefox Add-On window.addEventListener error: window not defined

问 题

我试图按照这个教程来创建一个firefox插件,当地址栏中的url发生变化时,这个插件会被拦截:

https://developer.mozilla.org/zh-CN/Add-ons/Code_snippets/Progress_Listeners #示例:_Notification_when_the_value_in_Address_Bar_changes



我刚刚复制了代码,只是添加了一个警报来查看它是否有效,但是我不能让它运行办法。



我的代码是:

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

var urlListener = {
oldURL:null,

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

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

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

//现在我们知道url是新的...
alert(aURI.spec);
this.oldURL = aURI.spec;


// nsIWebProgressListener
QueryInterface:XPCOMUtils.generateQI([“nsIWebProgressListener”,$ b $“nsISupportsWeakReference”]),

onLocationChange:function(aProgress,aRequest,aURI){
alert(“Called”);
this.processNewURL(aURI);函数(){},
onProgressChange:函数(){},
onStatusChange:函数(){},

$,

onStateChange onSecurityChange:function(){}
};

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


每当我尝试启动/测试此扩展时,我收到以下错误:

 在linux / x86-gcc3下运行Firefox 24.3.0 / Gecko 24.3.0({ec8030f7-c20a-464f-9b0e-13a3a9e97384})的测试。 
错误:ReferenceError:窗口未定义
Traceback(最近一次调用最后一个):
文件“resource:// jid1-u33krawc1uacsa-at-jetpack / amazon_rewriter / tests / test-main。 js“,第1行,在
var main = require(”./ main“);
文件“resource://gre/modules/commonjs/sdk/loader/cuddlefish.js”,第133行,在CuddlefishLoader / options< .load
result = load(loader,module);
文件“resource://jid1-u33krawc1uacsa-at-jetpack/amazon_rewriter/lib/main.js”,第38行,在
window.addEventListener(“load”,function(){urlListener.init ()},false);
0个测试通过。


可能是我不明白教程/扩展程序创建过程中的某些内容。



你能帮我理解缺失的东西吗?

编辑
在kapep答案后,我解决了窗口未定义的错误。但是,当我更改地址栏中的网址时,似乎没有任何事情发生。任何想法?


解决方案

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



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


可能有多种方法来获取窗口。我会这样做是使用低级 windowUtils API。使用 getMostRecentBrowserWindow 或更可靠的方法获取最近的一个,如 windowUtils.windows()像这样:
$ b $ pre $ const windowUtils = require(“sdk / window / utils”);

(windowUtils.windows()中的let window){
urlListener.init();
window.addEventListener(“unload”,function(){urlListener.uninit();},false);





$ b

为了防止你还想把监听器添加到所有打开的窗口将来,您可以在打开一个新窗口时添加它们:

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

本文地址:IT屋 » Firefox加载项window.addEventListener错误:未定义窗口

官方微信
扫一扫关注IT屋
微信公众号搜索 “ IT屋 ” ,选择关注
与百万开发者在一起