-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
92 lines (76 loc) · 2.41 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
var mBlockList = {};
var mTabRequest = {};
function getHostFromUrl(url){
var arrUrl = url.split("//");
if(arrUrl.length == 1){
return url;
}
var start = arrUrl[1].indexOf("/");
var relUrl = arrUrl[1].substring(0,start);
return relUrl;
}
function addUrlToBlockList(url){
console.log("add url to block list " + url);
var host = getHostFromUrl(url);
console.log("add url to block list " + host + ", " + mBlockList[host]);
if(mBlockList[host] == undefined){
//console.log("add url to block list " + black_list[host].block);
mBlockList[host] = {
count:0,
block:true
};
} else {
mBlockList[host].block = true;
}
chrome.storage.local.set({"block_list":mBlockList},function(){
console.log("addUrlToBlockList save ");
});
}
function removeHostFromBlockList(host){
console.log("removeHostFromBlockList " + host);
if(mBlockList[host] == undefined){
} else {
mBlockList[host].block = false;
}
console.log("removeHostFromBlockList " + mBlockList[host].block);
chrome.storage.local.set({"block_list":mBlockList},function(){
console.log("removeHostFromBlockList save ");
});
}
chrome.storage.local.get(["block_list"],function(result){
if(result.block_list == undefined){
mBlockList = {};
}else{
mBlockList = result.block_list;
}
});
chrome.tabs.onUpdated.addListener(function(tabId,changeInfo,tab){
if(changeInfo.status == 'loading'){
mTabRequest[tabId] = [];
}else if(changeInfo.status == 'complete'){
chrome.storage.local.set({"block_list":mBlockList},function(){
console.log("onUpdated save ");
});
}
});
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
var host = getHostFromUrl(details.url);
if(mBlockList[host] != undefined && mBlockList[host].block){
console.log("屏蔽 " + host);
mBlockList[host].count +=1;
return {cancel:true};
}
return {cancel:false};
},
{urls: ["<all_urls>"],types: ["script"]},
["blocking"]
);
//
chrome.webRequest.onCompleted.addListener(function(request){
if(mTabRequest[request.tabId] == undefined){
mTabRequest[request.tabId] = [];
}
mTabRequest[request.tabId].push(request.url);
}, {urls: ["<all_urls>"],types:["script"]});