-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
58 lines (46 loc) · 1.47 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// declaration part
const redirectionURL = "about:blank";
function redirect(requestDetails) {
// console.log("You should not visit " + requestDetails.url);
chrome.tabs.query({ currentWindow: true, active: true },
function(tabs) {
var tab = tabs[0];
chrome.tabs.update(tab.id, { url: redirectionURL })
}
);
}
function getAJAXPromise(url) {
return new Promise((resolve, reject) => {
let request = new XMLHttpRequest();
request.open('GET', url);
request.onload = () => {
if (request.status === 200) {
resolve(request.response);
} else {
reject(Error(request.statusText));
}
}
// Handle network errors
request.onerror = function() {
reject(Error("Network Error"));
};
// Make the request
request.send();
});
}
function addFilter(filter) {
}
function updateFilterList() { // update filterList.json when idle, locked, or when chrome exits
}
// execution part
var JsonURL = chrome.runtime.getURL('filterList.json');
// console.log(JsonURL);
getAJAXPromise(JsonURL).then((response) => {
let filterList = JSON.parse(response);
console.log("Filter successfully loaded: ", filterList);
return filterList;
}, (error) => {
console.error("Loading filter list failed: ", error);
}).then((filterList) => {
chrome.webRequest.onBeforeRequest.addListener(redirect, filterList);
});