`chrome.webRequest.onBeforeRequest.removeListener`? - 如何停止Chrome网页监听器 [英] `chrome.webRequest.onBeforeRequest.removeListener`? -- How to stop a chrome web listener

查看:384
本文介绍了`chrome.webRequest.onBeforeRequest.removeListener`? - 如何停止Chrome网页监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在检查 chrome.webrequest 的CatBlock扩展示例的示例代码,并且我看到它使用

$ b $打开了侦听器b

  chrome.webRequest.onBeforeRequest.addListener 

因此,当我想关闭它时,我可以只做
$ b $ p $ ch $ .webRequest.onBeforeRequest.removeListener ?

如果不是,我将如何摆脱它?



我认为这与Javascript的本地事件监听器,但我知道在Chrome的扩展中使用的是一个有点不同。



谢谢!



evamvid

解决方案

首先,完整的文档在这里可用



addListener 函数正常 1 < sup>有一个参数,这个函数将在事件触发时作为回调执行。



 函数callback_named(parameters)参数可以是一个名称函数,一个变量中引用的函数或一个匿名函数。 ){/ * ... * /} 

callback_variable = function(parameters){/ * ... * /};

chrome.webRequest.onBeforeRequest.addListener(callback_named);
chrome.webRequest.onBeforeRequest.addListener(callback_variable);
chrome.webRequest.onBeforeRequest.addListener(function(parameters){/ * ... * /});

要移除侦听器,可以调用 removeListener 具有相同的功能参考。在匿名函数中显然是不可能的。所以,只有前两个可以删除:

  chrome.webRequest.onBeforeRequest.removeListener(callback_named); 
chrome.webRequest.onBeforeRequest.removeListener(callback_variable);

请注意,您也可以测试特定侦听器:

  if(chrome.webRequest.onBeforeRequest.hasListener(callback_named)){
// callback_named正在监听
}

或者,测试是否有侦听器:

  if(chrome.webRequest.onBeforeRequest.hasListeners()){
// something is listening
}






1 有些API甚至允许过滤和/或附加选项,这些选项位于回调参数之后。事实上, webRequest API就是这样一个实例,它使上面的例子不完全正确(过滤器对于这个API是强制性的)。但他们回答了这个问题的本质。


So I was checking out the sample code for the CatBlock extension sample for chrome.webrequest, and I saw that it opened the listener with

chrome.webRequest.onBeforeRequest.addListener

So when I want to close it, can I just do

chrome.webRequest.onBeforeRequest.removeListener?

If not, how would I get rid of it?

I think this is similar to Javascript's native event listener, but I know the one used in Chrome's extensions is a little different.

Thanks!

evamvid

解决方案

First off, full documentation is available here.

addListener function normally1 has one argument, the function that will be executed as a callback when the event fires.

One can pass a named function, a function referenced in a variable, or an anonymous function:

function callback_named (parameters) { /* ... */ }

callback_variable = function (parameters) { /* ... */ };

chrome.webRequest.onBeforeRequest.addListener(callback_named);
chrome.webRequest.onBeforeRequest.addListener(callback_variable);
chrome.webRequest.onBeforeRequest.addListener(function (parameters) { /* ... */ });

To remove a listener, you call removeListener with the same function reference. It's obviously impossible in case on an anonymous function. So, only the first two can be removed:

chrome.webRequest.onBeforeRequest.removeListener(callback_named);
chrome.webRequest.onBeforeRequest.removeListener(callback_variable);

Note that you can also test for a particular listener:

if(chrome.webRequest.onBeforeRequest.hasListener(callback_named)){
  // callback_named is listening
}

Or, test if there are listeners at all:

if(chrome.webRequest.onBeforeRequest.hasListeners()) {
  // something is listening
}


1 Some APIs allow for even filtering and/or additional options, which come after the callback argument. In fact, webRequest API is one of such instances, which make the above examples not entirely correct (filters are mandatory for this API). But they answer the essence of the question.

这篇关于`chrome.webRequest.onBeforeRequest.removeListener`? - 如何停止Chrome网页监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆