简体   繁体   中英

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. I store the black list in a file "list.txt" locally and use XMLHttpRequest to retrieve the list of URL. The problem I am having is that the chrome.webRequest.onBeforeRequest only redirect if the URL is the last one in the list. 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:

*://*.domain1.com/*
*://*.domain2.com/*
*://*.domain3.com/*
*://*.domain4.com/*

Is only redirect if the URL is 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.

I expect you are using windows to save the .txt file, which terminates line with \\r\\n . So all except the last url ends with a \\r that won't match the url. I suggest:

list = xhr.responseText.split(/\r?\n/);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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