Javascript chrome.webRequest.onBeforeRequest仅重定向最后一个URL
如果URL在黑名单中,我正在构建一个重定向到google.com的扩展。 我将黑名单存储在本地文件“list.txt”中,并使用XMLHttpRequest检索URL列表。 我遇到的问题是,chrome.webRequest.onBeforeRequest仅在URL是列表中的最后一个URL时重定向。 这是我的密码:Javascript chrome.webRequest.onBeforeRequest仅重定向最后一个URL,javascript,redirect,google-chrome-extension,xmlhttprequest,Javascript,Redirect,Google Chrome Extension,Xmlhttprequest,如果URL在黑名单中,我正在构建一个重定向到google.com的扩展。 我将黑名单存储在本地文件“list.txt”中,并使用XMLHttpRequest检索URL列表。 我遇到的问题是,chrome.webRequest.onBeforeRequest仅在URL是列表中的最后一个URL时重定向。 这是我的密码: var list = ["something.something/something"]; var xhr = new XMLHttpRequest(); xhr.onready
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"]);
}
文件“list.txt”是:
仅当URL为domain4.com时才重定向
请帮忙
注意,我声明
var list = ["something.something/something"];
因为如果列表为空,chrome.webRequest.onBeforeRequest将重定向每个URL。我希望您正在使用windows保存.txt文件,该文件以
\r\n
结束行。因此,除最后一个url外,所有url都以与url不匹配的\r
结尾。我建议:
list = xhr.responseText.split(/\r?\n/);
这就是问题所在,或者说
.split(/\r?\n/)
\r\n。我将.txt文件的格式更改为://.domain1.com/*;*://*。domain2.com/*;*:/*。domain3.com/*和list=xhr.responseText.split(“;”);这很有效,非常感谢。
list = xhr.responseText.split(/\r?\n/);