Chrome extension to efficiently block domains

后端 未结 1 1033
陌清茗
陌清茗 2021-02-04 13:25

I\'m making a very simple Chrome extension to block requests to some domains (got tired of slow page loads on many websites, waiting on facebook junk). My question is about how

相关标签:
1条回答
  • 2021-02-04 13:55

    Use something like this:

    function blockRequest(details) {
      return {cancel: true};
    }
    
    function updateFilters(urls) {
      if(chrome.webRequest.onBeforeRequest.hasListener(blockRequest))
        chrome.webRequest.onBeforeRequest.removeListener(blockRequest);
      chrome.webRequest.onBeforeRequest.addListener(blockRequest, {urls: urls}, ['blocking']);
    }
    

    You can provide an options page to let the user to specify domains to block (and preferably save them with chrome.storage API). In your background page, update filters by re-registering the listener when it is initialized or the setting is changed.

    By the way, you should use the Declarative Web Request API when it is stable since it is much more efficient and doesn't require a persistent background page.

    0 讨论(0)
提交回复
热议问题