Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"]
    );
like image 770
Said Bouigherdaine Avatar asked Dec 12 '25 05:12

Said Bouigherdaine


1 Answers

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);
like image 56
Said Bouigherdaine Avatar answered Dec 13 '25 19:12

Said Bouigherdaine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!