-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
370 lines (310 loc) · 12.1 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
//Javascript que va detras del manifestjson
//############################################## GLOBAL VARIABLES ##############################################
//Boolean that indicates if extension's filter is activated or not
var filter = true;
//Boolean to check is allowed sites should be saved between sessions
var save_allowed = true;
//Variables needed for the deep learning model to work
var model;
var dict;
//Info about current open tabs will be handled in this variable
var tabsInfo = new Map();
//User allowed urls/hosts are saved here. Set is used to avoid repeated appearences of an element
var user_allowed_urls = new Set();
var user_allowed_hosts = new Set();
//Whitelisted elements to avoid some false positives that affect some websites functioning, stored in whitelist.json
var whitelisted_urls;
var whitelisted_hosts;
var whitelisted_matches;
//function to create a new entry for tabsInfo
function newInfo (tabId){
chrome.tabs.get(tabId,
function(tab) {
let aux_url, aux_host;
try {
aux_url = new URL(tab.url);
aux_host = aux_url.host;
let info = {
id: tabId,
url: tab.url,
blocked_index: [],
blocked: [],
host: aux_host
};
tabsInfo.set(tabId,info);
} catch (e) {
//if you load something that's not a website, error, like local files
console.log(e);
}
}
);
}
//change badge color (badge shows the number of suspicious url blocked on a website)
chrome.browserAction.setBadgeBackgroundColor({color:'#FF5733'});
//############################################## LISTENERS ##############################################
// esta funcion se ejecuta al ser instalado
chrome.runtime.onInstalled.addListener(
function(){
//meter aqui una url de bienvenida o algo sabes
loadModel();
load_dict();
loadWL();
}
);
//cargar modelo al iniciar navegador
chrome.runtime.onStartup.addListener(
function() {
loadModel();
load_dict();
loadWL();
chrome.storage.sync.get(['allowed_urls'], function(result){
result.allowed_urls.forEach(item => user_allowed_urls.add(item));
console.log("URLs recovered from memory: ", result.allowed_urls, user_allowed_urls);
});
chrome.storage.sync.get(['allowed_hosts'], function(result){
result.allowed_hosts.forEach(item => user_allowed_hosts.add(item));
console.log("Hosts recovered from memory: ", result.allowed_hosts, user_allowed_hosts);
});
}
);
// ############################################## WHITELIST FUNCTIONS ##############################################
// purpose of this is to avoid false positive that affects website's usability and correct functioning
async function loadWL(){
let aux;
await jQuery.getJSON("whitelist.json", function(result) {
aux = result;
for (var key in aux) {
switch (key) {
case "whitelisted_urls":
whitelisted_urls = aux[key];
break;
case "whitelisted_hosts":
whitelisted_hosts = aux[key];
break;
case "whitelisted_matches":
whitelisted_matches = aux[key];
break;
}
}
});
}
// ############################################## FUNCIONES PARA EL MODELO ##############################################
//Load model
async function loadModel(){
model = await tf.loadLayersModel('./model_tfjs-DNN/model.json');
//model.summary();
}
//load dictionary for preprocessing
async function load_dict(){
await jQuery.getJSON("dict_url_raw.json", function(jsonDict) {
dict = jsonDict;
//al caracter que tiene el 0 asignado como traduccion se lo cambiamos para que no interfiera con el padding,
//se le da el valor de dict.length que es el immediatamente mas peque siguiente
for (var key in dict) {
if (dict.hasOwnProperty(key) && dict[key] == 0) {
dict[key] = Object.keys(dict).length;
}
}
});
}
//######################### URL PREPROCESSING #########################
function url_preprocessing(url){
//convertimos la url de string a array de caracteres
const url_array = Array.from(url);
//traducimos la url de caracteres a numeros segun el diccionario creado por la notebook (esta depende de la base de datos que utiliza para el training)
for (i=0; i < url_array.length; i++){
if(dict != undefined && dict.hasOwnProperty(url_array[i]))
url_array[i]=dict[url_array[i]];
}
//padding a la izquierda
return Array(200).fill(0).concat(url_array).slice(url_array.length);
}
//######################### INFERENCE TASK #########################
//With a processed url returns an int to say if it has to be blocked or not
function processResult(prepro_url){
let result = model.predict(tf.tensor(prepro_url,[1, 200]));
result = result.reshape([2]);
result = result.argMax(); //aqui tiene el valor que toca pero sigue siendo un tensor
return result.arraySync(); //Returns the tensor data as a nested array, as it is one value, it returns one int
}
function updateTabInfo (idTab, aux_URL){
chrome.tabs.get(idTab,
function(tab) {
let check_value;
if(user_allowed_hosts.has(aux_URL.host) || whitelisted_hosts.includes(aux_URL.host) || whitelisted_urls.includes(aux_URL.href)){
check_value = true;
}
else{
check_value = user_allowed_urls.has(aux_URL.href);
}
let blocked_info = {
url: aux_URL.href,
host: aux_URL.host,
check: check_value,
}
tabsInfo.get(idTab).blocked_index.push(aux_URL.href);
tabsInfo.get(idTab).blocked.push(blocked_info);
chrome.browserAction.setBadgeText(
{tabId: idTab, text: ((tabsInfo.get(idTab).blocked.length).toString())}
);
}
);
}
// ############################################## REQUEST PROCESSING ##############################################
chrome.webRequest.onBeforeRequest.addListener(
function(details){ //this is a callback function executed when details of the webrequest are available
//check if extension is enabled
if(!filter){
return;
}
const request_url = details.url;
const idTab = details.tabId;
if(idTab >= 0 && !tabsInfo.has(idTab)){
newInfo(idTab);
}
//allow requests that have same host or are present in excepcions list
let aux_url = new URL(request_url);
if(tabsInfo.has(idTab)){
if(aux_url.host == tabsInfo.get(idTab).host){
//console.log(request_url, " and ", tabsInfo.get(idTab).url, " have same host, allowed connection" );
return;
}
}
let suspicious = 0;
let prepro_url = url_preprocessing(request_url);
if(model != undefined) {
suspicious = processResult(prepro_url);
}
//if it is classified as tracking, is added to tab info
if (suspicious && tabsInfo.has(idTab)){
//console.log("Classified as suspicous", request_url, aux_url.host, " Web host:", tabsInfo.get(idTab).host);
//console.log(aux_url);
//checks whitelist
for(var key in whitelisted_matches){
if(request_url.includes(whitelisted_matches[key])){
console.log("Allowed by matches whitelist: ", request_url);
return;
}
}
if(whitelisted_hosts.includes(aux_url.host) || whitelisted_urls.includes(request_url)){
console.log("Allowed by whitelist: ", request_url);
return;
}
//if its not whitelisted, show it on popup
updateTabInfo(idTab,aux_url);
//if user has allowed it, don't cancel request
if (user_allowed_hosts.has(aux_url.host) || user_allowed_urls.has(request_url)) {
console.log("Allowed by excepcions list: ", request_url);
return;
}
return {cancel: true};
};
},
{urls: ["<all_urls>"]},
["blocking"]
);
// ############################################## TABS LISTENERS ##############################################
var current_tab;
//on activated tab, creates new tabInfo if tab visited is not registered
chrome.tabs.onActivated.addListener(
function(activeInfo){
current_tab = activeInfo.tabId;
if(tabsInfo.has(activeInfo.tabId)){
return;
}
newInfo(activeInfo.tabId);
console.log(tabsInfo);
}
);
//on updated tab, creates new tabInfo when page is reloaded or url is changed
chrome.tabs.onUpdated.addListener(
function(tabId, changeInfo){
if(changeInfo.status == "loading" && tabsInfo.has(tabId)){
newInfo(tabId);
}
else{
return;
};
}
);
//on removed, remove tabInfo when a tab is closed
chrome.tabs.onRemoved.addListener(
function(tabId){
if(!tabsInfo.has(tabId)){
return;
}
//console.log(tabsInfo);
tabsInfo.delete(tabId);
}
);
//it save the allowed sites in storage when a window is closed
chrome.windows.onRemoved.addListener(function (windowid){
if (save_allowed) {
let arrayURLs = Array.from(user_allowed_urls.values());
let arrayHosts = Array.from(user_allowed_hosts.values());
chrome.storage.sync.set({ ['allowed_urls'] : arrayURLs }, function(){
console.log('URLs saved succesfully: ', arrayURLs);
});
chrome.storage.sync.set({ ['allowed_hosts'] : arrayHosts }, function(){
console.log('Hosts saved succesfully', arrayHosts);
});
}
});
// ############################################## CONNECTIONS WITH POPUP ##############################################
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
switch (request.method)
{
case 'get_enabled':
sendResponse(filter);
break;
case 'filterCheck':
filter = request.data;
break;
case 'get_enabled_SA':
sendResponse(save_allowed);
break;
case 'save_allowed_changed':
save_allowed = request.data;
break;
// URL excepction management
case 'add_url_exception':
user_allowed_urls.add(request.data);
//console.log("message received ", request.data);
if(tabsInfo.has(current_tab)){
let i = tabsInfo.get(current_tab).blocked_index.indexOf(request.data);
tabsInfo.get(current_tab).blocked[i].check =true;
}
break;
case 'delete_url_exception':
if(user_allowed_urls.has(request.data)){
user_allowed_urls.delete(request.data);
if(tabsInfo.has(current_tab)){
let i = tabsInfo.get(current_tab).blocked_index.indexOf(request.data);
tabsInfo.get(current_tab).blocked[i].check =false;
}
}
break;
// host excepction management
case 'add_host_exception':
user_allowed_hosts.add(request.data);
//console.log("message received ", request.data);
break;
case 'delete_host_exception':
if(user_allowed_hosts.has(request.data)){
user_allowed_hosts.delete(request.data);
}
break;
case 'get_allowed_hosts':
sendResponse(Array.from(user_allowed_hosts));
break;
case 'get_blocked_urls':
if(tabsInfo.has(current_tab)){
//console.log("Request received, sending data...", tabsInfo.get(current_tab).blocked, "user_allowed_urls ", user_allowed_urls);
sendResponse(tabsInfo.get(current_tab).blocked);
}
else {
sendResponse();
}
break;
}
});