在我的网络扩展中,我根据用户指示扩展执行的操作将多个相同的监听器添加到不同的选项卡 ID。
browser.webRequest.onBeforeRequest.addListener(mycallback,
{urls: ["myurl"], tabId: varyingtabid},["blocking"]);
但是当我需要清理某个选项卡的监听器时,我不知道如何指定哪个监听器,文档说removeListener只接受一个参数,即回调。
browser.webRequest.onBeforeRequest.removeListener(mycallback);
//does this remove every listener, what does this do when there are multiple listeners?
最佳答案
根据 api_event_listeners.cc ( link ) 中的源代码,removeListener 会删除指定监听器的所有注册,无论用于添加该监听器的过滤器如何。
这是因为您无法使用不同的过滤器多次添加相同的监听器 ( link ):
// Note that we only consider the listener function here, and not the // filter. This implies that it's invalid to try and add the same // function for multiple filters. // TODO(devlin): It's always been this way, but should it be?
换句话说,每次使用相同的函数引用调用 addListener 时,它都是 no-op .
<小时/>注意,如果你在另一个函数中声明回调,每次回调引用都会不同,因为在 JS 中 function name() {}
相当于 var name = function ( ) {}
,并且在包含函数的第一个语句之前声明它更加方便。
关于javascript - webRequest 删除附加到 tabId 的监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46253389/