简体   繁体   English

使用 chrome.webRequest api 时,网址不会被阻止

[英]urls are not blocking while working with chrome.webRequest api

I have stored some urls in chrome.storage.sync like below......我在 chrome.storage.sync 中存储了一些网址,如下所示......

sitesToBeBlocked: {
   "https://www.google.com/":"https://www.google.com/" ,
   "https://www.example.com/": "https://www.example.com/"
}

Now i am trying to block these urls using the code below.....现在我正在尝试使用下面的代码阻止这些网址.....

Manifest.json清单.json

{
  "name": "chrome extension",
  "description": ".............",
  "version": "0.0.1",
  "manifest_version": 2,

  "background": {
    "scripts": ["/scripts/background/background.js"]
  },

  "content_scripts": [
    {
      "matches": ["https://*/*","http://*/*"] ,
      "js": ["/scripts/content/jquery-3.6.0.js","/scripts/content/content-script.js"]
    }
  ],

  "permissions": ["storage","unlimitedStorage","webRequest","webRequestBlocking","*://*/*"],

  "browser_action": {
    "default_popup": "/popup/popup.html",
    "default_icon": {
      ............
    }
  },

  "options_ui": {
      "page": "/options/options.html",
      "open_in_tab": true
  },
  
 }

background.js背景.js

function isRequestCancelled(sitesArray, url){
    return sitesArray.includes(url);
}

function blockListener (details) {
    chrome.storage.sync.get(null, (items)=>{
        var sitesArray = Object.keys(items['sitesToBeBlocked']);
        
        return { cancel: isRequestCancelled(sitesArray, details.url ) };
    });  
}
chrome.webRequest.onBeforeRequest.addListener( blockListener ,{ urls: ["<all_urls>"], types: [ 'main_frame' ] }, ['blocking'] );   

But URLs are not blocked , I don't know what is the matter... please help me to get the exact problem that i am facing............但是 URL 没有被阻止,我不知道是怎么回事......请帮我解决我面临的确切问题............

I figured out the problem in my code myself..我自己在我的代码中发现了问题..

Actually the problem here is that chrome.storage.sync 's callback is asynchronous fucntion.实际上这里的问题是chrome.storage.sync的回调是异步函数。 Due to which chrome.webRequest 's callback is terminated before chrome.storage.sync 's callback return.由于chrome.webRequest的回调在chrome.storage.sync的回调返回之前终止。

The solution can be,解决方案可以是,

Put everything inside chrome.storage.sync 's callback, so that every function will return after chrome.storage.sync 's callback executes.将所有内容放在chrome.storage.sync的回调中,这样每个 function 都会在chrome.storage.sync的回调执行后返回。

Finally I have fixed this issue with the modified code below....最后我用下面的修改代码解决了这个问题......

chrome.storage.sync.get(null,(items)=>{

  function isRequestCancelled(sitesArray, url){
    return sitesArray.includes(url);
  }

  function blockListener (details) {
     var sitesArray = Object.keys(items['sitesToBeBlocked']);
     return { cancel: isRequestCancelled(sitesArray, details.url ) };
  }
  chrome.webRequest.onBeforeRequest.addListener( blockListener ,{ urls: [" 
  <all_urls>"], types: [ 'main_frame' ] }, ['blocking'] ); 

});

Actual clue is got from related query实际线索来自相关查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM