我试图遵循本教程创建一个Firefox的插件,拦截时,在地址栏更改URL:
https://developer.mozilla.org/en-US/Add-ons/Code_snippets/Progress_Listeners#Example:_Notification_when_the_value_in_Address_Bar_changes
我刚才复制的代码,只是增加了一个警告,看看它的工作原理,但我不能以任何方式运行。
我的代码是:
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);
每当我试图启动/测试这种扩展我收到以下错误:
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.
也许这就是我,我不知道从教程/扩展的创建过程中的东西。
你能帮助我了解什么缺什么?
编辑 kapep的答案后,我决定不定义的错误窗口。 但似乎当我在地址栏更改URL没有发生。 任何想法?