chrome webrequest api网址数组? [英] chrome webrequest api array of urls?
问题描述
这是一个非常简单的chrome扩展程序的代码,该扩展程序阻止了手动指定的网站.
Here is the code for a very simple chrome extension that blocks manually specified websites.
chrome.webRequest.onBeforeRequest.addListener(function(details) {
return {cancel: true};
},
{urls: ["*://*.google.com/*", "*://*.facebook.com/*"]},
["blocking"]);
我想知道是否可以使此url列表等于一个网站数组,如果可以,语法是什么.我的目标是从websocket服务器检索网站列表,然后让我的chrome扩展程序阻止它们.这是我的代码.
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. My goal is to retrieve a list of websites from a websocket server, then have my chrome extension block them. Here is my code.
var testSocket = new WebSocket("http://www.example.com/socketserver");
testSocket.onmessage = function (e) {
var websites = e.split(',');
console.log(e);
}
我是Java语言的新手,但是我认为这应该允许我连接到"example.com" Websocket服务器,检索由逗号分隔的网址字符串,并将该字符串拆分为一个名为网站"的数组.
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."
假设上面的代码执行了我刚才描述的操作(如果没有,请告诉我),如何将chrome扩展名中的网址"设置为等于网站"?
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"?
推荐答案
- 使webRequest侦听器具有全局功能,并在收到列表后重新注册它.
- 此外,将列表保存/加载到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']);
}
};
这篇关于chrome webrequest api网址数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!