Reputation: 107
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.
Upvotes: 0
Views: 2108
Reputation: 614
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/);
Upvotes: 1