-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
524 lines (473 loc) · 17.4 KB
/
Copy pathbackground.js
File metadata and controls
524 lines (473 loc) · 17.4 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
'use strict'
import { loadDatafromOldExtension, loadDatafromCache, getPreference, saveDatatoStorage, create_UUID, getDownloadWindowId } from "./common.js";
import { MIMETYPES } from "./mimetypes.js";
// List of Download IDs in progress. Used to play sound when all downloads complete.
var downloadsInProgress = [];
// Download completion sound.
var audio = document.getElementById("myAudio");
var cachedData = [];
var AntiContainerHelperInstalled = false;
function myDownloadObject(id, url, filename, referrer, mimetype, cleaners, lastmod, dlstart, dlend) {
this.id = id;
this.url = url;
this.filename = filename;
this.referrer = referrer;
this.mimetype = mimetype;
this.cleaners = cleaners;
this.lastmod = lastmod;
this.dlstart = dlstart;
this.dlend = dlend;
}
var counter = 0;
var browsername="Chrome";
try {
var gettingInfo = await browser.runtime.getBrowserInfo();
browsername=gettingInfo.name;
}
catch(err) {
}
if (browsername == "Chrome") {
browser.contextMenus.onClicked.addListener(onMenuClicked);
}
else {
browser.menus.onClicked.addListener(onMenuClicked);
browser.menus.onShown.addListener(async function() {cachedData = JSON.parse(await loadDatafromCache());});
}
browser.downloads.onChanged.addListener(downloadHandleChanged);
browser.alarms.onAlarm.addListener(handleAlarm);
browser.browserAction.onClicked.addListener(showSettings);
browser.runtime.onInstalled.addListener(onInstalledNotification);
function onInstalledNotification(details) {
init();
}
function showSettings() {
var openingPage = browser.runtime.openOptionsPage();
}
function init() {
let gettingItem = browser.storage.local.get("AnticontainerData");
gettingItem.then(onGot, onError);
function onGot(data) {
if(JSON.stringify(data) === '{}') {
console.log("First Run.");
loadDatafromOldExtension();
}
}
function onError(data) {
console.log("Error is: ${error}");
}
}
///////////////////
// CONTEXT MENUS //
///////////////////
// Create a context menu
let options={
id: "blackadderkateMenu",
title: "Quick D&ownload",
contexts: ["image","link","selection"]
};
if (browsername=="Firefox") {
options.icons= {
"16": "icons/page_save_16x16.png",
"32": "icons/page_save_32x32.png"
}
browser.menus.create(options, onMenuCreated);
}
else {
browser.contextMenus.create(options, onMenuCreated);
}
// Menu clicked - begin the download process
function onMenuCreated() {
// console.clear();
// preload audio by playing it just once, but really quietly.
audio.volume = 0.20;
audio.muted = true;
audio.addEventListener("canplaythrough", () => audio.play());
audio.play();
browser.menus.update("blackadderkateMenu", {title: browser.i18n.getMessage("blackadderkateMenu")});
var tmp = {filename: "", LastModTime: 0};
var sending = browser.runtime.sendNativeMessage('com.blackadderkate.anticontainerhelper',tmp);
sending.then(onResponse, onError);
init();
if (browser.runtime.lastError) {
console.log(`Error: ${browser.runtime.lastError}`);
}
else {
}
function onResponse(response) {
if (response == "OK") {
AntiContainerHelperInstalled = true;
}
}
function onError(error) {
console.log(`Error is: ${error}`);
AntiContainerHelperInstalled = false;
}
}
async function onMenuClicked(info, tab) {
if (info.menuItemId = "blackadderkateMenu") {
if (browsername=="Chrome") {
cachedData = JSON.parse(await loadDatafromCache()); }
let now = new Date();
if (typeof(info.selectionText) !== "undefined") {
const text1 = JSON.parse(await(browser.tabs.executeScript(tab.id,{
file: "selection.js"
})));
text1.forEach( elem => {
var downloadObject = new myDownloadObject;
downloadObject.id = counter++;
downloadObject.lastmod = 0;
downloadObject.referrer = "";
downloadObject.filename = "";
downloadObject.mimetype = "";
downloadObject.cleaners = "";
downloadObject.dlstart = Date.parse(now);
downloadObject.dlend = 0;
downloadObject.url=elem;
processRules(downloadObject);
});
}
else {
var downloadObject = new myDownloadObject;
downloadObject.id = counter++;
downloadObject.lastmod = 0;
downloadObject.referrer = "";
downloadObject.filename = "";
downloadObject.mimetype = "";
downloadObject.cleaners = "";
downloadObject.dlstart = Date.parse(now);
downloadObject.dlend = 0;
if (info.linkUrl != null) {
downloadObject.url = info.linkUrl;
} else if (info.srcUrl != null) {
downloadObject.url = info.srcUrl;
}
if (downloadObject.url != "") {
processRules(downloadObject);
}
}
}
};
// Process AntiContainer Rules
function processRules(downloadObject) {
var ruleNumber = 0;
var loop = 0;
downloadObject.referrer = downloadObject.url;
do {
loop++;
var ruleNumber = CheckAntiContainerRules(downloadObject.url);
if (loop > 999) {
ruleNumber = -2;
console.log("Error - infinite loop detected. Not proceeding.");
}
if (ruleNumber > -1) {
var container = cachedData[ruleNumber];
if (container.omitReferrer == true) { downloadObject.referrer = ""; }
if (container.keepReferrer == true) { downloadObject.referrer = tab.url; }
if (container.cleaners != null) {
if (container.cleaners.length > 0) {
downloadObject.cleaners = container.cleaners;
}
}
switch (container.type) {
case "redirector":
if (container.pattern != null) {
var results = downloadObject.url.match(container.pattern);
if (results != null) {
downloadObject.url = downloadObject.url.replace(RegExp(container.pattern,"g"),container.replacement);
}
}
break;
case "resolver":
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", parseResolverData);
oReq.finder = JSON.stringify(container.finder);
oReq.builder = JSON.stringify(container.builder);
oReq.namer = JSON.stringify(container.namer);
oReq.dlObject = JSON.stringify(downloadObject);
oReq.open("GET", downloadObject.url, true);
oReq.send();
ruleNumber = -2;
break;
}
}
}
while (ruleNumber > -1);
if (ruleNumber != -2) { getHeaders(downloadObject); }
// We have successfully downloaded a webpage into blob.responseText
// Process the FINDER, BUILDER and NAMER
// and store the results in dlObject
// Then pass the new dlObject to the next stage (ProcessHeaders).
async function parseResolverData() {
var finder = JSON.parse(this.finder);
var builder = JSON.parse(this.builder);
if (this.namer != null) {
var namer = JSON.parse(this.namer);
} else {
var namer = "";
}
var dlObject = JSON.parse(this.dlObject);
var url = dlObject.url;
var blob = this.responseText;
blob = blob.replace(/(\r\n)+|\r+|\n+|\t+/gm, "");
var results = blob.match(RegExp(finder));
if (results != null) {
for (var f = 1; f<results.length; f++) {
var builder = builder.replace("{"+f+"}",results[f]);
var namer = namer.replace('{'+f+'}',results[f]);
}
if (builder.match(RegExp('^(?:[a-z]+:)?//', 'i')) == null) {
builder = new URL(url).origin+builder;
}
dlObject.url = builder;
dlObject.filename = namer;
getHeaders(dlObject);
} else {
console.log("Resolver error - the Finder pattern could not be found.");
const notify = await getPreference("shownotification","true");
if (notify == "true") {
let options={
"type": "basic",
"title": "Error",
"message": "Resolver error - the Finder pattern could not be found."
};
let delayInMinutes = (1/10);
if (browsername=="Chrome") {
const delayInMinutes = 1;
options.iconUrl="icons/accept.png";
}
browser.notifications.create("AnticontainerNotification", options);
browser.alarms.create("AnticontainerNotification",{delayInMinutes});
}
}
}
}
// Sort rules by Priority
// loop through the rules, looking for a match
// if redirector, rewrite the URL
// if resolver, download the page, and analyse it in parseResolverData().
function CheckAntiContainerRules(url) {
let sortedData = cachedData.sort(function(a, b) {
return (b.priority - a.priority);
});
var match=0;
for (var f = 0; f<sortedData.length; f++) {
if (sortedData[f].prefix.length > 0) {
match = url.search(RegExp(sortedData[f].match));
} else {
match = -1;
}
if (match > -1) { break; }
}
var index = -1;
if (f < sortedData.length) {
index = cachedData.findIndex((element) => element.prefix == sortedData[f].prefix);
}
return index;
}
// Validate the URL and Filename
// do a HEAD request to sniff the download details.
// Parse the "Content-Type" to make sure the file extension is correct.
// Parse the "Last Modified" Header (if available)
// Parse the "content-disposition attachment; Header (if available) to get the filename.
function getHeaders(downloadObject) {
if (isValidUrl(downloadObject.url)) {
var oReq = new XMLHttpRequest();
oReq.open("HEAD", downloadObject.url, true);
oReq.send();
oReq.onreadystatechange = function() {
if (this.readyState == this.HEADERS_RECEIVED) {
if ((oReq.getResponseHeader('Content-Type').length > 0) && (downloadObject.mimetype.length == 0) && (oReq.status >= 200) && (oReq.status <= 299)) {
downloadObject.mimetype = oReq.getResponseHeader('Content-Type');}
if (downloadObject.lastmod == 0) {
if (oReq.getResponseHeader('Last-Modified') != null) {
downloadObject.lastmod = Date.parse(oReq.getResponseHeader('Last-Modified'));
} else {
downloadObject.lastmod = Math.floor(Date.now() / 1000) * 1000;
}
}
if (oReq.getResponseHeader('Content-Disposition') != null) {
let header = oReq.getResponseHeader('Content-Disposition');
let p1 = header.toLowerCase().indexOf("filename*=utf-8''");
if (p1>0) {
p1 = p1+17;
let p2 = header.length-p1;
downloadObject.filename = decodeURIComponent(header.substr(p1,p2));
} else {
downloadObject.filename = header.match(/filename=\"(.+)\"/)[1];
}
}
}
if (oReq.readyState == 4) {
if (downloadObject.filename.length == 0) { downloadObject.filename = extractFilenameFromURL(downloadObject.url);}
if (downloadObject.cleaners != "") {
downloadObject.cleaners.forEach(elem => {
downloadObject.filename = processCleaners(downloadObject.filename,elem);
});
}
downloadObject.filename = validateFilename(downloadObject.filename,downloadObject.mimetype);
doDownload(downloadObject);
}
}
}
}
function processCleaners(filename,cleaner) {
let results = filename.match(cleaner.pattern);
var newfilename = cleaner.replacement;
if (results.length > 0) {
for (var f = 1; f<results.length; f++) {
newfilename = newfilename.replace('{'+f+'}',results[f]);
}
}
return newfilename;
}
function isValidUrl(url) {
return RegExp("^(https?|ftp):\/\/(localhost|(.*\.[a-zA-Z- = 0]{2,30}))\/.*").test(url);
}
function hasUnicode(s) {
return /[^\u0000-\u007f]/.test(s);
}
// remove invalid characters from filename
// check for File Extension <--> MimeType mismatch
function validateFilename(filename,mimetype) {
if (hasUnicode(filename)) {
filename = decodeURIComponent(filename);
}
filename = filename.replace(/&#(\d{1,3});/g, function(match, dec) { return String.fromCharCode(dec);});
filename = filename.replace(/%([0-9a-f]{2})/gi, function(match, hex) { return String.fromCharCode(parseInt(match.substr(1,2),16));});
filename = filename.replaceAll("+"," ");
filename = filename.replaceAll(""","\"");
filename = filename.replace(/[/\\?%*:|<>]/gm, "_");
const comma = mimetype.indexOf(";");
if (comma > 0) {
mimetype = mimetype.substr(0,comma);
}
var defaultExtension = MIMETYPES.m[mimetype];
if (defaultExtension != null) {
// APPEND EXTENSION IF MISSING (TWITTER)
var fileExtensionPos = filename.lastIndexOf(".");
if (fileExtensionPos == -1) {
filename += "." + defaultExtension;
} else {
// REPLACE FILE EXTENSION IF WRONG.
var extension = filename.substr(fileExtensionPos+1,filename.length);
if (defaultExtension.indexOf(extension) == -1) {
filename = filename.replace(extension,defaultExtension);
}
}
}
return filename;
}
// Get a default filename, if the server doesn't provide one in the headers
function extractFilenameFromURL(url) {
var f = url.lastIndexOf("/");
url = (f > -1 ) ? url.substring(f+1,url.length) : url;
f = url.indexOf("?");
var filename = (f > -1 ) ? url.substring(0,f) : url;
filename = decodeURI(filename);
return filename;
}
// Do the actual download.
// make sure we handle PHP "thumbnail" scripts
// by comparing the file extension with the MIME type
function doDownload(downloadObject) {
var downloadOptions = {filename: downloadObject.filename, url: downloadObject.url, conflictAction : 'uniquify',saveAs: false, headers: []};
if (browser == "Firefox") { downloadOptions.headers.push({ name: "Referer", value: downloadObject.referrer});}
var downloading = browser.downloads.download(downloadOptions) ;
downloading.then(onStartedDownload, onDownloadFailed);
function onStartedDownload(id) {
downloadObject.id = id;
downloadsInProgress.push(downloadObject);
}
function onDownloadFailed(error) {
console.log(`Error: ${error}`);
}
}
// Post Download Routines
// Check if our Download has completed.
// Play Sound and show notifications, if preferences set - but only for OUR downloads!
// Finally get the actual filename as saved to disk from DownloadManager.
// And send a message to our AntiContainerHelper app to change the modification date.
async function downloadHandleChanged(delta) {
if (delta.state && ((delta.state.current === "complete") || (delta.state.current === "interrupted"))) {
const MyDownload = downloadsInProgress.find(({id}) => id === delta.id);
if (MyDownload != undefined ) {
let now = new Date();
MyDownload.dlend = Date.parse(now);
const playsounds = await getPreference("playsounds","true");
const notify = await getPreference("shownotification","true");
const volume = await getPreference("volume",0.4);
let tmp = downloadsInProgress.findIndex(item => item.id === delta.id);
downloadsInProgress.splice(downloadsInProgress.findIndex(item => item.id === delta.id), 1)
if (downloadsInProgress.length == 0) {
if (playsounds == "true") {
audio.volume = volume;
audio.muted = false;
audio.play();
}
if (notify == "true") {
let options={
"type": "basic",
"title": "All Done!",
"message": "All AntiContainer Downloads complete!"
};
let delayInMinutes = (1/10);
if (browsername=="Chrome") {
const delayInMinutes = 1;
options.iconUrl="icons/accept.png";
}
browser.notifications.create("AnticontainerNotification", options);
browser.alarms.create("AnticontainerNotification",{delayInMinutes});
}
}
if ((delta.state.current === "complete")) {
var downloaddetails = await browser.downloads.search({id: delta.id});
let tmp = {filename: encodeURIComponent(downloaddetails[0].filename), LastModTime: MyDownload.lastmod};
MyDownload.filename = downloaddetails[0].filename;
if ((MyDownload.lastmod > 0) && (AntiContainerHelperInstalled === true)) {
var sending = browser.runtime.sendNativeMessage("com.blackadderkate.anticontainerhelper",tmp);
sending.then(onResponse.bind(null, MyDownload), logDownloadError);
} else {
saveDownloadLog(MyDownload);
}
}
}
}
async function onResponse(downloadObject,message) {
let tmp = message.match(RegExp("Renamed \"(.+)\" to \"(.+)\"\."));
if (tmp != null) {
downloadObject.filename = tmp[2];
}
saveDownloadLog(downloadObject);
}
function logDownloadError(error) {
console.log(`Error: ${error}`);
}
async function saveDownloadLog(downloadObject) {
downloadObject.id = create_UUID();
console.log("Successfully downloaded \""+downloadObject.url+"\" to \""+downloadObject.filename+"\".");
var completedDownloads = []; // TO BE IMPLEMENTED
let preference = JSON.parse(await getPreference("completedDownloads","{}"));
if (JSON.stringify(preference).length <= 4) {
completedDownloads = [downloadObject]; }
else {
completedDownloads = preference;
completedDownloads.push(downloadObject);
}
saveDatatoStorage("completedDownloads",completedDownloads);
let open=await getDownloadWindowId();
if (open > -1) {
var sending = browser.runtime.sendMessage({greeting: "Append Downloads",download: downloadObject});
}
}
}
function handleAlarm(alarmInfo) {
browser.notifications.clear(alarmInfo.name);
}
function logStorageChange(changes, area) {
let changedItems = Object.keys(changes);
for (let item of changedItems) {
console.log(item + " has changed:");
console.log("Old value: " + JSON.parse(changes[item].oldValue).length);
console.log("New value: " + JSON.parse(changes[item].newValue).length);
}
}