gpt4 book ai didi

javascript - 在 Javascript 中更改语法事件监听器

转载 作者:行者123 更新时间:2023-12-04 00:40:58 25 4
gpt4 key购买 nike

我用 Javascript 编写了一些代码,需要进行一些转换,但我不知道如何实现。

我的代码:

gBrowser.addEventListener("DOMContentLoaded", function(e) {...}), false);

进入

window.addEventListener("load", function() {myExtension.init()}, false);

这里的关键是传递参数e。我已经尝试过“function(e)”和“myExtension.init(e)”,但它不起作用。

编辑(我所有的代码 - 函数代码位于代码的末尾):

var myExt_urlBarListener = {
QueryInterface: function(aIID)
{
if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
aIID.equals(Components.interfaces.nsISupports))
return this;
throw Components.results.NS_NOINTERFACE;
},

onLocationChange: function(aProgress, aRequest, aURI)
{
myExtension.processNewURL(aURI);
},

onStateChange: function(a, b, c, d) {},
onProgressChange: function(a, b, c, d, e, f) {},
onStatusChange: function(a, b, c, d) {},
onSecurityChange: function(a, b, c) {}
};

var myExtension = {
oldURL: null,

init: function(e) {
// Listen for webpage loads

gBrowser.addProgressListener(myExt_urlBarListener,
Components.interfaces.nsIWebProgress.NOTIFY_LOCATION);
},

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

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

// now we know the url is new...
this.oldURL = aURI.spec;

var href = aURI.spec;

var vars = [], hash;
var hashes = href.slice(href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}

if(vars[0].match(/^http:\/\/www\.google\.fr/))
{
//I want use the event e here
//But it doesn't work
displayBookmarks(e);
}
}
};

//The following doesn't work (I get "e is not defined" in the Error Console)
window.addEventListener("DOMContentLoaded", function(e) {myExtension.init(e)}, false);
window.addEventListener("unload", function() {myExtension.uninit()}, false);

最佳答案

不要忘记在函数声明中添加参数:

window.addEventListener("load", function(e) {myExtension.init(e)}, false);
...
init: function(e) {

关于javascript - 在 Javascript 中更改语法事件监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6580264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com