问题
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