Chrome extension How to remove a Listener on chrome.webRequest.onBeforeRequest

丶灬走出姿态 提交于 2019-12-10 11:25:14

问题


i am trying to remove this Listener in a google chrome extension for blocking urls, but i don't know how!

chrome.webRequest.onBeforeRequest.addListener(
              function(info) {
                console.log("Chat intercepted: " + info.url);
                return {cancel: true}; },
                {urls: ["https://sampleUrl/*"]},
                ["blocking"]
    );

回答1:


The solution to the problem is to create a named function instead of an anonymous function

var myfunction= function (info) {
  //Instructions 
  return {cancel: true}; }; 

and replace it as a variable in the code :

chrome.webRequest.onBeforeRequest.addListener(
                 myfunction,
                {urls: ["https://sampleUrl/*"]},
                ["blocking"]
    );

if i want to remove that listener i use :

chrome.webRequest.onBeforeRequest.removeListener(myfunction);


来源:https://stackoverflow.com/questions/40888038/chrome-extension-how-to-remove-a-listener-on-chrome-webrequest-onbeforerequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!