我在extension.js中从我的background.js调用一个函数,但它似乎执行了几次(三次)而不是一次。从我的控制台消息看起来,背景只发送一次消息,但addListener执行三次。有什么想法可以避免这种情况吗?
Background.js:
appAPI.tabs.getActive(function(tabInfo) {
console.log(1); // only once
tabUrl = tabInfo.tabUrl;
appAPI.message.toActiveTab({type:'clickedItem', url:tabUrl});
});
Extension.js:
appAPI.message.addListener(function(msg) {
if (msg.type === 'clickedItem') {
console.log(msg.url); // why three times?
}
});