-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbackground.js
96 lines (76 loc) · 2.2 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
92
93
94
95
96
// Github: https://github.com/pmsosa
//Blacklist
var blacklist = [ ];
var KEY = "HIDD3N"
//Add a new item to blacklist
function addblacklist(item){
blacklist.push(item.toLowerCase());
blacklist.sort();
toSave = {};
toSave[KEY] = JSON.stringify(blacklist);
chrome.storage.sync.set(toSave,function(){console.log("saved new case")});
}
//Remove item from blacklist
function removeblacklist(item,callback){
blacklist.splice(blacklist.indexOf(item.toLowerCase()),1);
blacklist.sort();
toSave = {};
toSave[KEY] = JSON.stringify(blacklist);
chrome.storage.sync.set(toSave,callback);
}
//Is a URL blacklisted
function blacklisted(url){
for (var i = 0; i < blacklist.length; i++){
if (url.includes(blacklist[i])){
return true;
}
}
return false;
}
//Callback Function to Hide a new requested site!
//Note: Chrome.tabs.get will throw a bunch of errors because when it pre-fetches websites, it does so from a tab that does not exist.
function hide(info){
//Yes I know there is a race condition here, but I'm purpously "risking" it for now.
if (blacklisted(info.url)){
chrome.tabs.get(info.tabId, function(tab){
if (info.type=="main_frame" & tab != undefined){
url = info.url; //.toLowerCase();
console.debug(info);
chrome.tabs.remove(info.tabId);
chrome.windows.create({url: url , incognito: true});
} //Endif
}); //EndTabs
return {cancel: true};
} //EndBlacklist
} //EndHide
function cleanlist(){
var tempBlacklist = []
for (var i = 0; i < blacklist.length; i++){
if (blacklist[i].length > 2){
tempBlacklist.push(blacklist[i])
}
}
blacklist = tempBlacklist;
}
//Listener
chrome.webRequest.onBeforeRequest.addListener(hide,{urls:["<all_urls>"]},["blocking"]);
initBlacklist();
//Initialize Blacklist
function initBlacklist(){
chrome.storage.sync.get(KEY,
function(item){
console.log(item);
//Blacklist does not exist!
if (Object.keys(item).length === 0){
console.log("not found!");
blacklist = [ ];
}
//Blacklist was found!
else{
blacklist = JSON.parse(item[KEY]);
cleanlist();
console.log("found!")
}
}
);
}