Said Bouigherdaine
Said Bouigherdaine

Reputation: 647

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

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"]
    );

Upvotes: 3

Views: 2958

Answers (1)

Said Bouigherdaine
Said Bouigherdaine

Reputation: 647

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);

Upvotes: 8

Related Questions