-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.html
131 lines (113 loc) · 3.47 KB
/
background.html
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<script type="text/javascript">
var RESET_BLACKLIST_TIME = 1000 * 60 * 20; // in milliseconds, last number is minutes
var RESET_BLACKLIST_TIME = 3000; // For test, 1 second reset
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
var loc = request.location;
var host = loc.host;
console.log("Processing location: %s", loc.host);
if (host === "alexbeutel.com" && GETParamFromURL("bewap", loc) === "done") {
console.log("Matched bewap = done");
var uid = parseInt(GETParamFromURL("uid", loc));
// Mark site as recently accessed
markAsAccessed(uid);
// Send response to redirect to original site
sendResponse({
finishedTask: true,
destination: getDestFromUID(uid),
shouldBlock: false
});
} else {
console.log("NOT Matched bewap = done");
sendResponse({
shouldBlock: shouldBlock(host,loc.href),
uid: getUID(host),
service: localStorage["bewap_service"],
weight: localStorage["weight:"+host]
});
}
}
);
// Returns a boolean on whether the URL should be blocked.
function shouldBlock(host,url) {
var s = localStorage["blacklist_sites"].split(" ");
var found = false;
for(var j = 0; j < s.length; j++) {
if(s[j] == host) found=true;
}
if(!found) return false;
var i = 0;
while (localStorage["blacklist." + i + ".url"] !== undefined) {
var blacklistURL = localStorage["blacklist." + i + ".url"];
if (blacklistURL === host) {
// Returns true if not recently accessed and passed a test
console.log(url);
localStorage["blacklist."+i+".dest"] = url;
return !recentlyAccessed(i);
}
i++;
}
console.log(url);
localStorage["blacklist."+i+".url"] = host;
localStorage["blacklist."+i+".dest"] = url;
localStorage["blacklist."+i+".lastAccessed"] = null;
return !recentlyAccessed(i);
}
// Returns the UID of that host
// returns -1 if none found
function getUID(host) {
var index = 0;
var storageKey = _storageURLKey(index);
while (localStorage[storageKey]) {
if (localStorage[storageKey] === host) return index;
index++;
storageKey = _storageURLKey(index);
}
return -1;
}
// Returns a host URL goven the UID
function getHostFromUID(uid) {
return localStorage[_storageURLKey(uid)];
}
function getDestFromUID(uid) {
return localStorage[_storageDestKey(uid)];
}
// Returns the String used to store the URL for a given UID
// e.g. "blacklist.0.url"
function _storageURLKey(uid) {
return "blacklist." + uid + ".url";
}
function _storageDestKey(uid) {
return "blacklist." + uid + ".dest";
}
function _storageDateKey(uid) {
return "blacklist." + uid + ".lastAccessed";
}
function recentlyAccessed(index) {
var lastAccessedTime = localStorage["blacklist." + index + ".lastAccessed"];
console.log("last accessed time: %s", lastAccessedTime);
if (lastAccessedTime === null) return false;
// elapsed in milliseconds
var elapsed = Date.now() - lastAccessedTime;
console.log("Recently accessed? %s", elapsed < RESET_BLACKLIST_TIME);
return elapsed < RESET_BLACKLIST_TIME;
}
// Marks the site as recently accessed
function markAsAccessed(uid) {
localStorage[_storageDateKey(uid)] = Date.now();
}
function GETParamFromURL(param, loc) {
var query = loc.search.substring(1);
var vars = query.split("&");
for (var i=0; i<vars.length; i++) {
var pair = vars[i].split("=");
if (pair[0] == param) {
console.log("GET Param %s = %s", param, pair[1]);
return pair[1];
}
}
}
function trim(stringToTrim) {
return String(stringToTrim).replace(/^\s+|\s+$/g,"");
}
</script>