webRequest 删除附加到 tabId 的侦听器

webRequest removing a listener attached to a tabId

在我的网络扩展程序中,我根据用户告诉扩展程序做什么,将多个相同的侦听器添加到不同的选项卡 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 () {} 并且更方便地声明它之前包含函数的第一个语句。