Javascript 禁用加载项时,引导加载项中的removeEventListener不工作

Javascript 禁用加载项时,引导加载项中的removeEventListener不工作,javascript,firefox,firefox-addon,firefox-addon-restartless,Javascript,Firefox,Firefox Addon,Firefox Addon Restartless,我注意到,禁用引导加载项后,removeEventListener似乎不会删除侦听器,我无法找出原因 let contextMenu = window.document.getElementById('contentAreaContextMenu'); if (contextMenu) { contextMenu.addEventListener('popupshowing', this.contextPopupShowing.bind(this), false);

我注意到,禁用引导加载项后,
removeEventListener
似乎不会删除侦听器,我无法找出原因

let contextMenu = window.document.getElementById('contentAreaContextMenu');
if (contextMenu) {
  contextMenu.addEventListener('popupshowing', 
            this.contextPopupShowing.bind(this), false);
  // added this to make sure they refer to the same function
  console.log(this.contextPopupShowing.toString()); 
} 
然后禁用插件

console.log(this.contextPopupShowing.toString()); // same as above
this.contextMenu.removeEventListener('popupshowing', 
            this.contextPopupShowing.bind(this), false);
// just showing that 'this' is working
this.contextMenu.removeChild(this.menuitem); 
console.log(this.contextPopupShowing.toString()); // same as above
this.contextMenu.removeEventListener('popupshowing', 
            this.contextPopupShowingBound, false);
// just showing that 'this' is working
this.contextMenu.removeChild(this.menuitem); 
最后

contextPopupShowing: function() { 
  // logs even after removeEventListener
  console.log('contextPopupShowing called'); 
  // more code
},
contextPopupShowing: function() { 
  // logs even after removeEventListener
  console.log('contextPopupShowing called'); 
  // more code
},

因为它是绑定的。你要做的是:

添加时:

let contextMenu = window.document.getElementById('contentAreaContextMenu');
if (contextMenu) {
  this.contextPopupShowingBound = this.contextPopupShowing.bind(this);
  contextMenu.addEventListener('popupshowing', 
            this.contextPopupShowingBinded, false);
  // added this to make sure they refer to the same function
  console.log(this.contextPopupShowing.toString()); 
} 
然后禁用插件

console.log(this.contextPopupShowing.toString()); // same as above
this.contextMenu.removeEventListener('popupshowing', 
            this.contextPopupShowing.bind(this), false);
// just showing that 'this' is working
this.contextMenu.removeChild(this.menuitem); 
console.log(this.contextPopupShowing.toString()); // same as above
this.contextMenu.removeEventListener('popupshowing', 
            this.contextPopupShowingBound, false);
// just showing that 'this' is working
this.contextMenu.removeChild(this.menuitem); 
最后

contextPopupShowing: function() { 
  // logs even after removeEventListener
  console.log('contextPopupShowing called'); 
  // more code
},
contextPopupShowing: function() { 
  // logs even after removeEventListener
  console.log('contextPopupShowing called'); 
  // more code
},
我敢肯定,您不能在
removeEventListener
中使用
bind


请参阅此主题的优秀主题:

,因为它是绑定的。您要做的是:

添加时:

let contextMenu = window.document.getElementById('contentAreaContextMenu');
if (contextMenu) {
  this.contextPopupShowingBound = this.contextPopupShowing.bind(this);
  contextMenu.addEventListener('popupshowing', 
            this.contextPopupShowingBinded, false);
  // added this to make sure they refer to the same function
  console.log(this.contextPopupShowing.toString()); 
} 
然后禁用插件

console.log(this.contextPopupShowing.toString()); // same as above
this.contextMenu.removeEventListener('popupshowing', 
            this.contextPopupShowing.bind(this), false);
// just showing that 'this' is working
this.contextMenu.removeChild(this.menuitem); 
console.log(this.contextPopupShowing.toString()); // same as above
this.contextMenu.removeEventListener('popupshowing', 
            this.contextPopupShowingBound, false);
// just showing that 'this' is working
this.contextMenu.removeChild(this.menuitem); 
最后

contextPopupShowing: function() { 
  // logs even after removeEventListener
  console.log('contextPopupShowing called'); 
  // more code
},
contextPopupShowing: function() { 
  // logs even after removeEventListener
  console.log('contextPopupShowing called'); 
  // more code
},
我敢肯定,您不能在
removeEventListener
中使用
bind


请参阅此主题的优秀主题:

,因为它是绑定的。您要做的是:

添加时:

let contextMenu = window.document.getElementById('contentAreaContextMenu');
if (contextMenu) {
  this.contextPopupShowingBound = this.contextPopupShowing.bind(this);
  contextMenu.addEventListener('popupshowing', 
            this.contextPopupShowingBinded, false);
  // added this to make sure they refer to the same function
  console.log(this.contextPopupShowing.toString()); 
} 
然后禁用插件

console.log(this.contextPopupShowing.toString()); // same as above
this.contextMenu.removeEventListener('popupshowing', 
            this.contextPopupShowing.bind(this), false);
// just showing that 'this' is working
this.contextMenu.removeChild(this.menuitem); 
console.log(this.contextPopupShowing.toString()); // same as above
this.contextMenu.removeEventListener('popupshowing', 
            this.contextPopupShowingBound, false);
// just showing that 'this' is working
this.contextMenu.removeChild(this.menuitem); 
最后

contextPopupShowing: function() { 
  // logs even after removeEventListener
  console.log('contextPopupShowing called'); 
  // more code
},
contextPopupShowing: function() { 
  // logs even after removeEventListener
  console.log('contextPopupShowing called'); 
  // more code
},
我敢肯定,您不能在
removeEventListener
中使用
bind


请参阅此主题的优秀主题:

,因为它是绑定的。您要做的是:

添加时:

let contextMenu = window.document.getElementById('contentAreaContextMenu');
if (contextMenu) {
  this.contextPopupShowingBound = this.contextPopupShowing.bind(this);
  contextMenu.addEventListener('popupshowing', 
            this.contextPopupShowingBinded, false);
  // added this to make sure they refer to the same function
  console.log(this.contextPopupShowing.toString()); 
} 
然后禁用插件

console.log(this.contextPopupShowing.toString()); // same as above
this.contextMenu.removeEventListener('popupshowing', 
            this.contextPopupShowing.bind(this), false);
// just showing that 'this' is working
this.contextMenu.removeChild(this.menuitem); 
console.log(this.contextPopupShowing.toString()); // same as above
this.contextMenu.removeEventListener('popupshowing', 
            this.contextPopupShowingBound, false);
// just showing that 'this' is working
this.contextMenu.removeChild(this.menuitem); 
最后

contextPopupShowing: function() { 
  // logs even after removeEventListener
  console.log('contextPopupShowing called'); 
  // more code
},
contextPopupShowing: function() { 
  // logs even after removeEventListener
  console.log('contextPopupShowing called'); 
  // more code
},
我敢肯定,您不能在
removeEventListener
中使用
bind


请看这个主题的精彩主题:

快告诉我吧:p但我猜这是个大傻瓜!!!!最后今天早上我从睡梦中醒来,无意中通过手机登录并看到了14分钟前刚刚发布的“如何拦截下载”,所以我甚至通过发表评论来谈论这个话题,我将在几秒钟内回答。我去我的桌面启动了它,然后你frikin发布了!:'(这是一个MDN副本。):(“但我猜它是绑定的”饶了我吧,你只是生气了,我打了你一次:不,我用了绑定,所以noobs比我更noob理解:PI指的是你的变量名,只是想帮你:pAw good好的,你抓到我了。我在“绑定”上编造了那个借口noobs很容易理解这件事。:P当你摆脱胖箭头时编辑!:P吃我吧。:P但我猜这是个傻瓜!!!!终于!!今天早上我从睡梦中醒来,碰巧通过手机登录并看到了“如何拦截下载”,它是14分钟前发布的,所以我甚至打电话给这个话题,发表了一个评论,我马上就要回答。我去我的桌面启动它,然后你frikin发布了!:'(这是一个MDN副本!:(“但我想它是绑定的”饶了我吧,你只是生气了,我打了你一次:不,不,我用了bind,所以noobs比我更noob明白:PI指的是你的变量名,只是想帮忙:pAw good好的,你抓住我了。我在“bind”上编造了那个借口noobs很容易理解这件事。:P当你摆脱胖箭头时编辑!:P吃我吧。:P但我猜这是个傻瓜!!!!终于!!今天早上我从睡梦中醒来,碰巧通过手机登录并看到了“如何拦截下载”,它是14分钟前发布的,所以我甚至打电话给这个话题,发表了一个评论,我马上就要回答。我去我的桌面启动它,然后你frikin发布了!:'(这是一个MDN副本!:(“但我想它是绑定的”饶了我吧,你只是生气了,我打了你一次:不,不,我用了bind,所以noobs比我更noob明白:PI指的是你的变量名,只是想帮忙:pAw good好的,你抓住我了。我在“bind”上编造了那个借口noobs很容易理解这件事。:P当你摆脱胖箭头时编辑!:P吃我吧。:P但我猜这是个傻瓜!!!!终于!!今天早上我从睡梦中醒来,碰巧通过手机登录并看到了“如何拦截下载”,它是14分钟前发布的,所以我甚至打电话给这个话题,发表了一个评论,我马上就要回答。我去我的桌面启动它,然后你frikin发布了!:'(这是一个MDN副本!:(“但我想它是绑定的”饶了我吧,你只是生气了,我打了你一次:不,不,我用了bind,所以noobs比我更noob明白:PI指的是你的变量名,只是想帮忙:pAw god好的,你抓住我了。我在“bind”上编造了一个借口,让noobs很容易理解。:P编辑为你去掉脂肪箭头!:P