[英]chrome.webRequest.onBeforeRequest Only Redirect the last URL
I am building an extension that redirect to google.com if the URL is in a black list. 我正在构建一个扩展程序,如果URL在黑名单中,它将重定向到google.com。 I store the black list in a file "list.txt" locally and use XMLHttpRequest to retrieve the list of URL.
我将黑名单存储在本地的文件“ list.txt”中,并使用XMLHttpRequest检索URL列表。 The problem I am having is that the chrome.webRequest.onBeforeRequest only redirect if the URL is the last one in the list.
我遇到的问题是,如果URL是列表中的最后一个,则chrome.webRequest.onBeforeRequest仅重定向。 Here is my code:
这是我的代码:
var list = ["something.something/something"];
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
list = xhr.responseText.split(/\n/);
abc();
}
}
xhr.open("GET", chrome.extension.getURL('text/list.txt'), true);
xhr.send(null);
function abc() {
chrome.webRequest.onBeforeRequest.addListener(
function (details) {
return {redirectUrl: "https://google.com"};
},
{urls: list, types: []},
["blocking"]);
}
File "list.txt" is: 文件“ list.txt”为:
*://*.domain1.com/*
*://*.domain2.com/*
*://*.domain3.com/*
*://*.domain4.com/*
Is only redirect if the URL is domain4.com. 仅当URL为domain4.com时才重定向。
Please help. 请帮忙。
Note that I declare 请注意,我声明
var list = ["something.something/something"];
Because chrome.webRequest.onBeforeRequest will redirect every URL if the list is empty. 因为chrome.webRequest.onBeforeRequest将在列表为空时重定向每个URL。
I expect you are using windows to save the .txt file, which terminates line with \\r\\n
. 我希望您使用Windows保存.txt文件,该文件以
\\r\\n
结尾。 So all except the last url ends with a \\r
that won't match the url. 因此,除最后一个URL之外的所有URL均以
\\r
结尾。 I suggest: 我建议:
list = xhr.responseText.split(/\r?\n/);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.