Iordanis Iordanis - 6 months ago 17
Javascript Question

Negating "urls" pattern with onBeforeRequest

chrome.webRequest.onBeforeRequest.addListener(function(details) {
if (localStorage.on == '1') {
return {cancel:true};
}
}, {urls: ["*://*.domain1.net/*","*://*.domain2.com/*","*://*.domain3.com/*"], types: ["script","xmlhttprequest","other"]}, ["blocking"]);


I don't see any way I can negate the URL pattern. Basically I want to match and block everything except those 3 domains. I thought of returning cancel:true for and the exact content for the 3 domains. First of will that work or will one listener "over-take" the other? How can I return the exact content (So no more cancel:true).

Answer

The following example illustrates how to block all requests, except requests to domain1.net and domain2.net:

    chrome.webRequest.onBeforeRequest.addListener(
       function(details) {
         return {cancel: details.url.indexOf(".domain2.net/") == -1 && details.url.indexOf(".domain1.net/") == -1};
       },
    {urls: ["<all_urls>"]},
    ["blocking"]);

I can't see any way to negate the URL pattern in the documentation

Comments