简体   繁体   English

chrome webrequest api网址数组?

[英]chrome webrequest api array of urls?

Here is the code for a very simple chrome extension that blocks manually specified websites. 这是一个非常简单的chrome扩展程序的代码,该程序阻止手动指定的网站。

chrome.webRequest.onBeforeRequest.addListener(function(details) { 
    return {cancel: true}; 
},
{urls: ["*://*.google.com/*", "*://*.facebook.com/*"]},
["blocking"]);

I'm wondering if it's possible to make this url list equal to an array of websites, and if so what the syntax is. 我想知道是否有可能使此url列表等于网站的数组,如果是这样,语法是什么。 My goal is to retrieve a list of websites from a websocket server, then have my chrome extension block them. 我的目标是从websocket服务器检索网站列表,然后让我的chrome扩展程序阻止它们。 Here is my code. 这是我的代码。

var testSocket = new WebSocket("http://www.example.com/socketserver");
testSocket.onmessage = function (e) {
     var websites = e.split(',');
     console.log(e);
}

I'm new to javascript, but I believe this should allow me to connect to an "example.com" websocket server, retrieve a string of urls separated by commas, and split the string into an array named "websites." 我是Java语言的新手,但我认为这应该允许我连接到“ example.com” websocket服务器,检索由逗号分隔的网址字符串,并将该字符串拆分为一个名为“网站”的数组。

Assuming the above code does what I just described (and please tell me if it does not), how do I set "urls" from the chrome extension equal to "websites"? 假设上面的代码是我刚才描述的(如果没有,请告诉我),我如何将chrome扩展名中的“ urls”设置为等于“ websites”?

  • Make the webRequest listener a global function and re-register it upon receiving the list. 使webRequest侦听器成为全局函数,并在收到列表后重新注册它。
  • Also, save/load the list in chrome.storage.local 另外,将列表保存/加载到chrome.storage.local中

function blockUrl(details) { 
  return {cancel: true}; 
}

// load the list on extension startup
chrome.storage.local.get('urls', data => {
  if (data.urls && data.urls[0]) {
    chrome.webRequest.onBeforeRequest.addListener(blockUrl, data, ['blocking']);
  }
});

testSocket.onmessage = function (e) {
  const urls = e.split(',');
  chrome.storage.local.set({urls});
  chrome.webRequest.onBeforeRequest.removeListener(blockUrl);
  if (urls[0]) {
    chrome.webRequest.onBeforeRequest.addListener(blockUrl, {urls}, ['blocking']);
  }
};

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

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