-
Notifications
You must be signed in to change notification settings - Fork 16
/
option-script.js
executable file
·352 lines (286 loc) · 11.6 KB
/
option-script.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
"use strict";
var api;
if (chrome == undefined) {
api = browser;
} else {
api = chrome;
}
var tmn_options = {};
var tmn_engines ={};
var tmn = api.extension.getBackgroundPage().TRACKMENOT.TMNSearch;
var options = {};
function loadHandlers() {
$("#apply-options").unbind().click(function() {
saveOptions();
updateEngineList();
});
$("#clear-options").unbind().click(function() {
clearOptions();
updateEngineList();
});
$("#trackmenot-opt-help").unbind().click(function() {
api.tabs.create({
url: "http://cs.nyu.edu/trackmenot/faq.html#options"
});
});
$("#trackmenot-opt-site").unbind().click(function() {
api.tabs.create({
url: "https://cs.nyu.edu/trackmenot"
});
});
$("#show-add").unbind().click(function() {
$("#add-engine-table").show();
});
$("#show-log").unbind().click(function() {
api.storage.local.get(["logs_tmn"],TMNShowLog);
});
$("#trackmenot-opt-showqueries").click(function() {
var tmn = api.extension.getBackgroundPage().TRACKMENOT.TMNSearch;
var queries = tmn._getQueries();
TMNShowQueries(queries);
});
$("#validate-feed").unbind().click(function() {
var feeds = $("#trackmenot-seed").val();
var param = {
"feeds": feeds
};
api.runtime.sendMessage({
'tmn': "TMNValideFeeds",
'param': param
});
});
$("#clear-log").unbind().click(function() {
api.storage.local.set({"logs_tmn":""});
});
$("#search-engine-list").on('click', 'button.smallbutton', function(event) {
var del_engine = event.target.id.split("_").pop();
delEngine(del_engine);
});
$("#trackmenot-opt-timeout").change(function() {
var timeout = $("#trackmenot-opt-timeout").val();
setFrequencyMenu(timeout);
});
$("#add-engine").unbind().click(function() {
var engine = {};
engine.name = $("#newengine-name").val();
engine.urlmap = $("#newengine-map").val();
if (engine.urlmap.indexOf('trackmenot') < 0) {
alert("To add a new search engine url, search 'trackmenot' (without the quotes) on your desired search engine. Then, copy and paste the search url in the URL text box below.");
return;
}
addEngine(engine);
});
}
function getEngIndexById(id) {
for (var i = 0; i < tmn_engines.list.length; i++) {
if (tmn_engines.list[i].id === id) return i;
}
return -1;
}
function updateEngineList() {
tmn_engines.list.forEach(function (x) {return x.enabled = false;});
$("#search-engine-list :checked").each(function() {
console.log(($(this).val()));
tmn_engines.list[getEngIndexById($(this).val())].enabled = true ;
});
api.storage.local.set({'engines_tmn':tmn_engines});
TMNShowEngines(tmn_engines);
}
function clearOptions() {
var tmn = api.extension.getBackgroundPage().TRACKMENOT.TMNSearch;
api.storage.local.clear();
tmn._resetSettings();
getStorage(["engines_tmn","options_tmn"],TMNLoadOptionWindow );
}
function addEngine(param) {
var new_engine = {};
new_engine.name = param.name;
new_engine.id = new_engine.name.toLowerCase();
new_engine.urlmap = param.urlmap.replace('trackmenot', '|');
var query_params = new_engine.urlmap.split('|');
var kw_param = query_params[0].split('?')[1].split('&').pop();
new_engine.regexmap = '^(' + new_engine.urlmap.replace(/\//g, "\\/").replace(/\./g, "\\.").split('?')[0] + "\\?.*?[&\\?]{1}" + kw_param + ")([^&]*)(.*)$";
new_engine.enabled = true;
tmn_engines.list.push(new_engine);
//cout("Added engine : " + new_engine.name + " url map is " + new_engine.urlmap);
TMNShowEngines(tmn_engines)
updateEngineList();
}
function delEngine(del_engine) {
tmn_engines.list = tmn_engines.list.filter(function(x) {return x.id !== del_engine;});
TMNShowEngines(tmn_engines)
updateEngineList();
}
function TMNSetOptionsMenu(item) {
options =item;
var feedList = options.feedList.join('|');
var kw_black_list = options.kwBlackList;
//console.log("Enabled: " +options.enabled)
$("#add-engine-table").hide();
$("#trackmenot-opt-enabled").prop('checked', options.enabled);
$("#trackmenot-opt-useTab").prop('checked', options.useTab);
$("#trackmenot-opt-burstMode").prop('checked', options.burstMode);
$("#trackmenot-opt-save-logs").prop('checked', options.saveLogs);
$("#trackmenot-opt-disable-logs").prop('checked', options.disableLogs);
$("#trackmenot-seed").val(feedList);
$("#trackmenot-blacklist").val(kw_black_list);
$("#trackmenot-use-blacklist").prop('checked', options.use_black_list);
$("#trackmenot-use-dhslist").prop('checked', options.use_dhs_list);
setFrequencyMenu(options.timeout);
}
function setFrequencyMenu(timeout) {
$('#trackmenot-opt-timeout option[value=' + timeout + ']').prop('selected', true);
}
function TMNShowLog(items) {
var logs = items.logs_tmn;
var htmlStr = '<table cellspacing=3>';
htmlStr += '<thead><tr align=left>';
htmlStr += '<th>Engine</th>';
htmlStr += '<th>Mode</th>';
htmlStr += '<th>URL</th>';
htmlStr += '<th>Query/Message</th>';
htmlStr += '<th>Date</th>';
htmlStr += '</tr></thead>';
for (var i = 0; i < 3000 && i < logs.length; i++) {
htmlStr += '<tr ';
if (logs[i].type === 'ERROR') htmlStr += 'style="color:Red">';
if (logs[i].type === 'query') htmlStr += 'style="color:Black">';
if (logs[i].type === 'URLmap') htmlStr += 'style="color:Brown">';
if (logs[i].type === 'click') htmlStr += 'style="color:Blue">';
if (logs[i].type === 'info') htmlStr += 'style="color:Green">';
htmlStr += logs[i].engine ? '<td><b>' + logs[i].engine + '</b></td>' : '<td></td>';
htmlStr += logs[i].mode ? '<td>' + logs[i].mode + '</td>' : '<td></td>';
htmlStr += logs[i].newUrl ? '<td>' + logs[i].newUrl.substring(0, 50) + '</td>' : '<td></td>';
htmlStr += logs[i].query ? '<td>' + logs[i].query + '</td>' : '<td></td>';
htmlStr += logs[i].date ? '<td>' + logs[i].date + '</td>' : '<td></td>';
htmlStr += '</font></tr>';
}
htmlStr += '</table>';
$('#tmn_logs_container').html(htmlStr);
$('#tmn_logs_container').css("visibility","visible");
//window.setTimeout(TMNShowLog, 1000,items);
}
function TMNShowEngines(item) {
tmn_engines= item;
var htmlStr = "<table>";
for (var i = 0; i < tmn_engines.list.length; i++) {
var engine = tmn_engines.list[i];
let is_checked = engine.enabled? " checked " : "";
htmlStr += '<tr >';
htmlStr += '<td><input type="checkbox" id="' + engine.id + '" value="' + engine.id + '" ' + is_checked +'">' + engine.name + '</td><td><button class="smallbutton" id="del_engine_' + engine.id + '" > - </button> </td>';
htmlStr += '</tr>';
}
htmlStr += '</table>';
$('#search-engine-list').html(htmlStr);
loadHandlers();
}
function TMNShowQueries(tmn_queries) {
var htmlStr = '<a href="#dhs">DHS</a> | <a href="#rss"> RSS </a> | <a href="#popular"> Popular </a>|<a href="#extracted"> Extracted</a>';
htmlStr += '<div style="height:1000px;overflow:auto;"><table witdh=500 cellspacing=3 bgcolor=white frame=border>';
if ( tmn_queries.dhs ) {
htmlStr += '<tr style="color:Black" bgcolor=#D6E0E0 align=center>';
htmlStr += '<td > DHS Monitored <td>';
htmlStr += '<a name="dhs"></a>';
htmlStr += '</tr>';
for (var i=0; i<tmn_queries.dhs.length ; i++) {
htmlStr += '<tr style="color:Black" bgcolor=#F0F0F0 align=center>';
htmlStr += '<td>' +tmn_queries.dhs[i].category_name+ '<td>';
htmlStr += '</tr>';
for (var j=0; j< tmn_queries.dhs[i].words.length ; j++) {
htmlStr += '<tr style="color:Black">';
htmlStr += '<td>' +tmn_queries.dhs[i].words[j]+ '<td>';
htmlStr += '</tr>';
}
}
}
if ( tmn_queries.rss ) {
htmlStr += '<tr style="color:Black" bgcolor=#D6E0E0 align=center>';
htmlStr += '<td > RSS <td>';
htmlStr += '<a name="rss"></a>';
htmlStr += '</tr>';
for (var i=0; i<tmn_queries.rss.length ; i++) {
htmlStr += '<tr style="color:Black" bgcolor=#F0F0F0 align=center>';
htmlStr += '<td>' +tmn_queries.rss[i].name+ '<td>';
htmlStr += '</tr>';
for (var j=0; j< tmn_queries.rss[i].words.length ; j++) {
htmlStr += '<tr style="color:Black">';
htmlStr += '<td>' +tmn_queries.rss[i].words[j]+ '<td>';
htmlStr += '</tr>';
}
}
}
if ( tmn_queries.zeitgeist ) {
htmlStr += '<tr style="color:Black" bgcolor=#D6E0E0 align=center>';
htmlStr += '<td > Popular <td>';
htmlStr += '<a name="popular"></a>';
htmlStr += '</tr>';
for (var i=0; i< tmn_queries.zeitgeist.length ; i++) {
htmlStr += '<tr style="color:Black">';
htmlStr += '<td>' +tmn_queries.zeitgeist[i]+ '<td>';
htmlStr += '</tr>';
}
}
if ( tmn_queries.extracted ) {
htmlStr += '<tr style="color:Black" bgcolor=#D6E0E0 align=center>';
htmlStr += '<td > Extracted <td>';
htmlStr += '<a name="extracted"></a>';
htmlStr += '</tr>';
for (var i=0; i<tmn_queries.extracted.length ; i++) {
htmlStr += '<tr style="color:Black" bgcolor=#F0F0F0 align=center>';
htmlStr += '<td>' +tmn_queries.extracted[i]+ '<td>';
htmlStr += '</tr>';
}
}
htmlStr += '</table></div>';
$('#tmn_logs_container').html(htmlStr);
$('#tmn_logs_container').css("visibility","visible");
}
function saveOptions() {
var options = {};
options.enabled = $("#trackmenot-opt-enabled").is(':checked');
console.log("Saved Enabled: " + options.enabled);
options.useTab = $("#trackmenot-opt-useTab").is(':checked');
options.burstMode = $("#trackmenot-opt-burstMode").is(':checked');
options.disableLogs = $("#trackmenot-opt-disable-logs").is(':checked');
options.saveLogs = $("#trackmenot-opt-save-logs").is(':checked');
options.timeout = $("#trackmenot-opt-timeout").val();
options.feedList = $("#trackmenot-seed").val().split('|');
options.use_black_list = $("#trackmenot-use-blacklist").is(':checked');
options.use_dhs_list = $("#trackmenot-use-dhslist").is(':checked');
options.kwBlackList = $("#trackmenot-blacklist").val().split(",");
api.storage.local.set({"options_tmn":options});
}
function handleRequest(request, sender, sendResponse) {
if (!request.options) return;
switch (request.options) {
case "TMNSendQueries":
TMNShowQueries(request.param);
sendResponse({});
break;
default:
sendResponse({}); // snub them.
}
}
function onError(){
console.log("Error");
}
function getStorage(keys,callback) {
try {
let gettingItem = api.storage.local.get(keys);
gettingItem.then(callback, onError);
} catch (ex) {
api.storage.local.get(keys,callback);
}
}
function TMNLoadOptionWindow(items) {
if (items["options_tmn"]) {
TMNSetOptionsMenu(items["options_tmn"]);
}
if (items["engines_tmn"]) {
TMNShowEngines(items["engines_tmn"]);
}
}
window.addEventListener('load', function() {
getStorage(["engines_tmn","options_tmn"],TMNLoadOptionWindow );
});
api.runtime.onMessage.addListener(handleRequest);