forked from unintended/download-organizer-chrome-extension
-
Notifications
You must be signed in to change notification settings - Fork 1
/
manager.js
129 lines (106 loc) · 4.01 KB
/
manager.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
const EXT_MIME_MAPPINGS = {
'mp3': 'audio/mpeg',
'pdf': 'application/pdf',
'zip': 'application/zip',
'png': 'image/png',
'jpg': 'image/jpeg',
'exe': 'application/exe',
'avi': 'video/x-msvideo',
'torrent': 'application/x-bittorrent'
};
const RULE_FIELDS = ['mime', 'referrer', 'url', 'finalUrl', 'filename'];
const DATE_FIELD = 'date';
const DEFAULT_CONFLICT_ACTION = 'uniquify';
chrome.downloads.onDeterminingFilename.addListener(function (downloadItem, suggest) {
console.log("Downloading item %o", downloadItem);
var rulesets = JSON.parse(localStorage.getItem('rulesets'));
var item = {
'mime': downloadItem.mime,
'referrer': decodeURI(downloadItem.referrer),
'url': decodeURI(downloadItem.url),
'finalUrl': decodeURI(downloadItem.finalUrl),
'filename': downloadItem.filename,
'startTime': new Date(downloadItem.startTime)
};
// Octet-stream workaround
if (downloadItem.mime == 'application/octet-stream') {
var matches = downloadItem.filename.match(/\.([0-9a-z]+)(?:[\?#]|$)/i);
var extension = matches && matches[1];
if (EXT_MIME_MAPPINGS[extension]) {
item.mime = EXT_MIME_MAPPINGS[extension];
}
}
rulesets.every(function (rule) {
if (!rule.enabled) {
console.log("Rule disabled: %o", rule);
return true; // continue to the next rule
}
var substitutions = {};
var success = RULE_FIELDS.every(function (field) {
if (!rule[field]) {
substitutions[field] = [item[field]];
return true; // skip this and continue to the next field
}
var regex = new RegExp(rule[field], 'i');
var matches = regex.exec(item[field]);
if (!matches) {
return false; // rule failed, break
}
matches.shift();
substitutions[field] = [item[field]].concat(matches);
return true;
});
if (!success) {
console.log("Rule didn't match: %o", rule);
return true; // continue to the next rule
}
console.log("Rule matched: %o", rule);
var result = true;
var filename = rule['pattern'].replace(/\$\{(\w+)(?::(.+?))?\}/g, function (orig, field, idx) {
if (field === DATE_FIELD) {
if (idx) {
return moment(item.startTime).format(idx);
} else {
return moment(item.startTime).format("YYYY-MM-DD");
}
}
if (!substitutions[field]) {
console.log('Invalid field %s', field);
result = false;
return orig;
}
if (idx) {
if (!substitutions[field][idx]) {
console.log('Invalid index %s for field %s', idx, field);
result = false;
return orig;
}
return substitutions[field][idx];
}
return substitutions[field][0];
});
// if no exact filename specified use the original one
if (/\/$/.test(filename)) {
filename = filename + substitutions.filename[0];
}
// remove trailing slashes
filename = filename.replace(/^\/+/, '');
var conflictAction = rule['conflict-action'];
if (!conflictAction) {
conflictAction = DEFAULT_CONFLICT_ACTION;
}
if (result) {
suggest({
filename: filename,
conflictAction: conflictAction
});
}
});
});
var version = localStorage.getItem('version');
if (!version || version != chrome.runtime.getManifest().version) {
// Open the options page directly after installing or updating the extension
localStorage.setItem('version', chrome.runtime.getManifest().version);
localStorage.setItem('showChangelog', true);
chrome.tabs.create({ url: "options.html" });
}