-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
339 lines (292 loc) · 10.2 KB
/
index.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<!doctype html>
<html lang="en">
<head>
<script type="text/javascript">
/*
* This is the backend worker of the extension.
* All communication in/out is handled via html5 web messaging.
*/
// Core extensions
// http://stackoverflow.com/questions/280634/endswith-in-javascript
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
// Hash of all connected clients by type.
// Eg. {options: [client], userjs: [client1, client2]}
var clients = {};
// A local variable cache of all options in the extensions local storage
var options = {};
// When this extension has loaded, listen for connections
window.addEventListener('load', setup_connection, false);
/*
* Initializes & logs all connections & disconnections
*/
function setup_connection(){
// When the injected script is activated, it connects with the background process.
opera.extension.onconnect = function(event){
event.source.postMessage({what: 'id', name: 'background'});
}
opera.extension.ondisconnect = function(event) {
// remove client from list
for(var type in clients) {
var i = clients[type].indexOf(event.source);
if(i >= 0) {
//opera.postError('back: removing client ' + event.origin);
clients[type].splice(i,1);
} else {
// Unfortunatelly opera doesn't provide event.source when a
// client disconnects. This makes it hard to keep track of
// connected clients.
//opera.postError('back: disconnect without remove ' + event.origin + ', ' + event.source);
}
}
}
opera.extension.onmessage = handle_message;
}
/*
* Handles all incoming messages from both options & userjs scripts
*/
function handle_message(event) {
switch(event.data['what']) {
case 'id':
var type = event.data['name'];
// add client to list of clients
if(!clients[type]) clients[type] = [];
// make sure client isn't already in list
if(clients[type].indexOf(event.source) == -1) {
clients[type].push(event.source);
}
break;
case 'get blocked hosts':
event.source.postMessage({
what: 'all blocked hosts',
list: blocked_hosts_list()
});
break;
case 'check hosts':
var marked_hosts = mark_hosts(event.data['hosts']);
// notify userjs about which hosts to block
event.source.postMessage({
what: 'marked hosts',
marked_hosts: marked_hosts
});
break;
case 'block hosts':
var list = block_hosts(event.data['list']);
// tell userjs something changed
message_clients('userjs',{
what: 'block list changed'
});
// update any open options pages
message_clients('options', {
what: 'all blocked hosts',
list: blocked_hosts_list()
});
break;
case 'unblock hosts':
unblock_hosts(event.data['list']);
// update any open options pages
message_clients('options', {
what: 'all blocked hosts',
list: blocked_hosts_list()
});
// tell userjs something changed
message_clients('userjs', {
what: 'block list changed'
});
break;
case 'get option':
event.source.postMessage({
what: 'option',
name: event.data['name'],
value: options[event.data['name']]
});
break;
case 'get userjs options':
event.source.postMessage({
what: 'options',
value: userjs_options()
});
break;
case 'get full options':
event.source.postMessage({
what: 'options',
value: full_options()
});
break;
case 'set option':
var name = event.data['name'];
set_option(name, event.data['value']);
save_option(name);
switch(name) {
case 'confirm_block':
case 'remove_redirects':
// alert userjs of the change
message_clients('userjs',{
what: 'options',
value: userjs_options()
});
break;
}
break;
}
}
/*
* Sends a message to all clients of a particular type
*
* type: client type, one of ['options','userjs]
* data: javascript hash-like object containing the data
*/
function message_clients(type, data) {
if(clients[type]) {
clients[type].forEach(function(source) {
try {
source.postMessage(data);
} catch(e) {
// The ondisconnect event should provide us with a source
// however opera sets source to null.
// This is an ugly work around.
delete clients[type][source];
}
});
}
}
function block_hosts(hosts_in) {
// Filter out invalid hosts.
// Valid hosts: a.b.c.d, b.c.d, c.d, *.b.c.d, *.d, ...
var hosts = normalize_hosts(hosts_in).filter(function(host) {
var index = host.search(/^(\*\.)?([^\*\.\/]+\.)*([^\*\.\/]+)$/);
return (host.length > 2) &&
index == 0 &&
!is_host_blocked(host)
});
hosts.forEach(function(host) {
options['blocked_hosts'][host] = true;
// purge hosts which are now covered by a wildcard
if(host.substr(0,2) == '*.') {
var wildcard_host = host.substr(2)
var key;
for(key in options['blocked_hosts']) {
if(key != host && key.endsWith(wildcard_host)) {
delete options['blocked_hosts'][key];
}
}
}
});
// persist changed settings
save_option('blocked_hosts');
return hosts;
}
function unblock_hosts(hosts_in) {
normalize_hosts(hosts_in).forEach(function(host) {
delete options['blocked_hosts'][host];
});
save_option('blocked_hosts');
}
// Mark each host as blocked/not
function mark_hosts(hosts_in) {
var marked = {};
hosts_in.forEach(function(host) {
marked[host] = is_host_blocked(host);
});
return marked;
}
function is_host_blocked(host) {
if(options['blocked_hosts'][host]) {
return true;
}
var key;
var parts = host.split('.');
// For host 'thing.bcd.example.com',
// check the presence of the following wildcard hosts in this order:
// *.com, *.example.com, *.bcd.example.com
if(parts.length > 1) {
var a, b;
for(a = 1; a < parts.length; a++) {
var arr = ['*'];
for(b = parts.length - a; b < parts.length; b++) {
arr.push(parts[b]);
}
wild_host = arr.join('.');
if(options['blocked_hosts'][wild_host]) {
return true;
}
}
}
return false;
}
function normalize_hosts(hosts_in) {
return hosts_in.map(function(host) {
return host.toLowerCase();
});
}
/*
* Returns a list of blocked hosts in an array.
* Normally it's stored as a hash for speed.
*/
function blocked_hosts_list() {
var host, list = [];
for(host in options['blocked_hosts']) {
list.push(host);
}
return list;
}
/*
* Sets the value of an option
*/
function set_option(name, value) {
options[name] = value;
}
/*
* Saves a particular option to the widgets local storage
*/
function save_option(name) {
widget.preferences.setItem(name, JSON.stringify(options[name]));
}
/*
* UserJS specific options
*/
function userjs_options() {
return {
confirm_block: options['confirm_block'],
remove_redirects: options['remove_redirects']
};
}
/*
* Full options
*/
function full_options() {
return {
blocked_hosts: blocked_hosts_list(),
confirm_block: options['confirm_block'],
remove_redirects: options['remove_redirects']
};
}
/*
* Loads all options into a local variable from the widgets local storage.
*/
function load_options() {
var a, key;
for(a = 0; a < widget.preferences.length; a++) {
key = widget.preferences.key(a);
options[key] = JSON.parse(widget.preferences.getItem(key));
}
// Incase options are not imported properly
if(typeof options['blocked_hosts'] == 'undefined') options['blocked_hosts'] = {};
if(typeof options['confirm_block'] == 'undefined') options['confirm_block'] = true;
if(typeof options['remove_redirects'] == 'undefined') options['remove_redirects'] = false;
}
/*
* Saves all options to local storage
*/
function save_options() {
for(key in options) {
widget.preferences.setItem(key,JSON.stringify(options[key]));
}
}
load_options();
</script>
</head>
<body>
</body>
</html>