-
Notifications
You must be signed in to change notification settings - Fork 1
/
scrap-service.js
65 lines (58 loc) · 1.62 KB
/
scrap-service.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
// A console-like API that prints its output using notifications.
var konsole = {
log: (...args) => {
browser.notifications.create({
"type": "basic",
"title": "Scrap",
"message": args.join(" ")
});
},
error: (...args) => {
browser.notifications.create({
"type": "basic",
"title": "Scrap Error!",
"message": args.join(" ")
});
}
}
konsole.log("Scrap is starting.", "All your JS will be downloaded.");
// A set of downloaded data. Used to avoid downloading the same file
// more than once.
var downloaded = new Set();
var pathPrefix = (function() {
var date = new Date();
var year = "" + date.getFullYear();
var month = "" + (date.getMonth() + 1);
if (month.length < 2) {
month = "0" + month;
}
var day = "" + date.getDate();
if (day.length < 2) {
day = "0" + day;
}
var yyyymmdd = year + month + day;
return "scrap/" + yyyymmdd + "/";
})();
// We wish to be informed of the load of all scripts.
browser.webRequest.onCompleted.addListener(onResourceLoadComplete,
{
urls: [ "*://*/*" ],
types: ["script"]
},
);
function onResourceLoadComplete(details) {
let url = details.url;
if (downloaded.has(url)) {
// Skip duplicates.
return;
}
downloaded.add(url);
konsole.log("scrap", "Downloading", url);
browser.downloads.download({
filename: pathPrefix + escape(url),
method: details.method,
saveAs: false,
url
})
.catch(e => konsole.error("Download could not start", e));
}