2012-09-05 201 views
1

我使用Mozilla的附加組件生成器。我正在尋找一種方法來刪除contentScript中的事件偵聽器。我使用端口方式在附加腳本代碼和內容腳本代碼之間進行通信。如何刪除事件偵聽器?

問題是事件「響應」的回調被多次調用。我希望它被調用一次並在事件展示的回調中聲明。

有人可以幫我嗎?

main.js代碼:

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

內容的腳本(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

+0

如何創建監聽器以禁用隱藏面板,當我點擊面板時? – 2015-09-07 06:25:23

回答

1

最後我發現這個問題。這是附加套件中的一個錯誤。在函數removeListener的文件api-utils/lib/content/content-worker.js中,索引始終爲-1。

indexOf中給出的參數是事件的名稱,它搜索一個函數。這是不正確的。

所以爲了解決這個問題,我用let index = listeners[name].indexOf(callback);替換let index = listeners[name].indexOf(name);

編輯

的錯誤已得到修復。它會在版本1.10中發佈,請參閱here

+0

是的,這就是我發現的 - 你快一點。提交了[bug 788981](https://bugzilla.mozilla.org/show_bug.cgi?id=788981)。 –

+0

非常感謝修正弗拉基米爾^^。 –

相關問題