繁体   English   中英

webRequest删除附加到tabId的侦听器

[英]webRequest removing a listener attached to a tabId

在我的Web扩展程序中,我根据用户告诉扩展程序的操作,将多个相同的侦听器添加到不同的选项卡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 () {} ,并且具有在声明之前声明的便利性。包含功能的第一个陈述。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM