如何删除事件监听器? [英] How to remove an event listener?

查看:333
本文介绍了如何删除事件监听器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Mozilla的附加组件生成器。我正在寻找一种方法来删除contentScript中的事件监听器。我使用端口方式在附加脚本代码和内容脚本代码之间进行通信。



问题是多次调用事件响应的回调。我希望它被调用一次,并在事件显示的回调声明。



有人可以帮我吗?

$ main.js code:

var Panel = require(panel)。Panel;
var popup_panel = Panel({
width:286,
height:340,
contentURL:require(self)。data.url(popup.html),
allow:{script:true},
contentScriptWhen:end,
contentScriptFile:[
require(self)。data.url(test.js)
]
onShow:function(){
this.port.emit(show);
var pan = this;
this.port.on hide,function(){pan.hide();});
}
});

var Widget = require(widget)。Widget;
var widget = Widget({
id:mozilla-icon,
label:我的Mozilla Widget,
contentURL:http://www.mozilla.org /favicon.ico,
面板:popup_panel
});
$ b popup_panel.port.on(get,function(){
popup_panel.port.emit(response);
});内容脚本( test.js ):



  self.port.on(show,function(){
console .log(show);
function response(){
console.log(reponse called);
}

self.port.emit get);
self.port.once(response,response);
self.port.removeListener(response,response);
});

完整源代码

解决方案

最后我发现了这个问题。这是附加套件中的一个错误。在函数removeListener中的文件api-utils / lib / content / content-worker.js中,索引总是-1。



indexOf中给出的参数是事件的名称,它搜索一个功能。这是不正确的。

所以要解决这个问题,我换成 let index = listeners [name] .indexOf(name); let index = listeners [name] .indexOf(callback);

编辑



这个错误已被修复。它将在1.10版本中发布,请参阅此处


I use the Mozilla's Add-on Builder. I am looking for a way to remove an event listener in a contentScript. I use the port way to communicate between add-on script code and the content script code.

The problem is the callback on event "response" is called more than once. I want it to be called once and declared in the callback of the event show.

Can someone help me with that?

main.js code:

var Panel = require("panel").Panel;
var popup_panel = Panel({
    width: 286,
    height: 340,
    contentURL: require("self").data.url("popup.html"),
    allow: { script: true },
    contentScriptWhen: "end",
    contentScriptFile : [
        require("self").data.url("test.js")
    ],
    onShow: function(){
        this.port.emit("show");
        var pan = this;
        this.port.on("hide", function(){pan.hide();});
    }
});

var Widget = require("widget").Widget;
var widget = Widget({
    id: "mozilla-icon",
    label: "My Mozilla Widget",
    contentURL: "http://www.mozilla.org/favicon.ico",
    panel: popup_panel
});

popup_panel.port.on("get", function(){
    popup_panel.port.emit("response");
});

Content script (test.js):

self.port.on("show", function(){
    console.log("show");
    function response(){
        console.log("reponse called");
    }

    self.port.emit("get");
    self.port.once("response", response);
    self.port.removeListener("response", response);
});

full source code

解决方案

Finally I found the problem. It is a bug in the add-on kit. In the file api-utils/lib/content/content-worker.js in the function removeListener the index is always -1.

The parameter given in the indexOf is the name of the event and it search a function. It is incorrect.

So to solve the problem I replace the line let index = listeners[name].indexOf(name); by let index = listeners[name].indexOf(callback);.

EDIT

The bug has been fixed. It will publish in the version 1.10 see here

这篇关于如何删除事件监听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆