简体   繁体   English

chrome.webRequest.onBeforeRequest仅重定向最后一个URL

[英]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.

相关问题 Chrome扩展程序如何删除chrome.webRequest.onBeforeRequest上的侦听器 - Chrome extension How to remove a Listener on chrome.webRequest.onBeforeRequest Chrome扩展程序:使用chrome.webRequest.onBeforeRequest进行DNS解析 - Chrome Extensions: DNS resolve with chrome.webRequest.onBeforeRequest chrome.webRequest.onBeforeRequest触发不止一次? - chrome.webRequest.onBeforeRequest firing more than once? Chrome.webrequest.onBeforeRequest多次调用自身 - Chrome.webrequest.onBeforeRequest calling itself multiple times 使用chrome.webRequest.onBeforeRequest进行同步XHR请求 - Making synchoronous XHR request with chrome.webRequest.onBeforeRequest chrome.webRequest.onBeforeRequest导致网页的某些功能无法加载 - chrome.webRequest.onBeforeRequest causes some features of web pages to fail to load Chrome webRequest onBeforeRequest 在收到函数返回后不会重定向 - Chrome webRequest onBeforeRequest won't redirect after receiving function return Chrome扩展程序webRequest.onBeforeRequest - Chrome extension webRequest.onBeforeRequest chrome.webRequest.onBeforeRequest.addListener 无法读取未定义的属性“onBeforeRequest” - chrome.webRequest.onBeforeRequest.addListener Cannot read property 'onBeforeRequest' of undefined Firefox Addon中的OnBeforeRequest URL重定向(从Chrome扩展转换) - OnBeforeRequest URL redirect in Firefox Addon (Conversion from Chrome Extension)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM