-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
200 lines (177 loc) · 6.21 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
const DEFAULT_FOLDER = "defaultFolder";
const TAB_URL = "/dowload-sorter.html";
browser.storage.onChanged.addListener((changes) => {
for (key in changes) {
var storageChange = changes[key];
if (key == DEFAULT_FOLDER) {
localStorage.defaultFolder = storageChange.newValue;
} else {
localStorage.rules = storageChange.newValue;
}
}
});
browser.runtime.onInstalled.addListener((details) => {
if (details.reason == "install") {
browser.storage.sync.get(DEFAULT_FOLDER, (items) => {
if (typeof items.defaultFolder == 'undefined') {
localStorage.defaultFolder = "default";
browser.storage.sync.set({
DEFAULT_FOLDER: "default"
});
} else {
localStorage.defaultFolder = items.defaultFolder;
}
});
browser.storage.sync.get('rules', (items) => {
if (typeof items.rules == 'undefined') {
localStorage.rules = JSON.stringify([{
"extension": "jpg,jpeg,gif,png",
"foldername": "images"
}, {
"extension": "zip,z7,tar,gz",
"foldername": "compression"
}, {
"extension": "exe",
"foldername": "exe"
}, {
"extension": "pdf,hwp,doc,docx",
"foldername": "document"
}, {
"extension": "z*",
"foldername": "z_start_all_file"
}]);
browser.storage.sync.set({
'rules': JSON.stringify([{
"extension": "jpg,jpeg,gif,png",
"foldername": "images"
}, {
"extension": "zip,z7,tar,gz",
"foldername": "compression"
}, {
"extension": "exe",
"foldername": "exe"
}, {
"extension": "pdf,hwp,doc,docx",
"foldername": "document"
}, {
"extension": "z*",
"foldername": "z_start_all_file"
}])
});
} else {
localStorage.rules = items.rules;
}
browser.tabs.create({
url: TAB_URL
});
});
} else if (details.reason == "update") {
browser.storage.sync.get(DEFAULT_FOLDER, (items) => {
if (typeof items.defaultFolder == 'undefined') {
browser.storage.sync.set({
DEFAULT_FOLDER: localStorage.defaultFolder
});
} else {
localStorage.defaultFolder = items.defaultFolder;
}
});
browser.storage.sync.get('rules', (items) => {
if (typeof items.rules == 'undefined') {
browser.storage.sync.set({
'rules': localStorage.rules
});
} else {
localStorage.rules = items.rules;
}
browser.tabs.create({
url: TAB_URL
});
});
}
});
function matches(extension, filename) {
if (extension == "") {
return false;
}
index = -1;
index = filename.lastIndexOf('.');
if (index != -1) {
type = filename.substring(index + 1, filename.len);
}
extensionLower = extension.toLowerCase();
extensionLower = extensionLower.replace(/ /gi, '');
if (extensionLower.indexOf("*") != -1 ||
extensionLower.indexOf("|") != -1 ||
extensionLower.indexOf(",") != -1) {
extensionLower = extensionLower.replace(/,/gi, '|');
extensionSplit = extensionLower.split('|');
temp = "";
index = 0;
extensionSplit.forEach((element) => {
if (index != 0)
temp += "|^" + element + "$";
else
temp += "^" + element + "$";
index++;
});
extensionLower = temp;
extensionLower = extensionLower.replace(/\*/gi, "[a-z0-9]*");
var pattern = new RegExp(extensionLower);
if (pattern.test(type.toLowerCase())) {
return true;
}
}
if (type.toLowerCase() == extensionLower) {
return true;
}
return false;
}
const CONTEXT_ID = "save-file";
browser.contextMenus.create({
id: CONTEXT_ID,
title: "Save file to location",
contexts: ["all"],
});
browser.contextMenus.onClicked.addListener((info) => {
if (info.menuItemId == CONTEXT_ID) {
function suggestDownload(filename, foldername) {
browser.downloads.download({
'filename': foldername + "/" + filename,
'saveAs': true,
'url': safeUrl
})
}
var safeUrl = "";
if (info.linkUrl != 'undefined') {
safeUrl = escapeHTML(info.linkUrl);
} else {
safeUrl = escapeHTML(info.srcUrl);
}
var rules = localStorage.rules;
try {
rules = JSON.parse(rules);
} catch (e) {
localStorage.rules = JSON.stringify([]);
}
var localFilename = safeUrl.replace(/^.*[\\\/]/, '');
var found = false;
for (var index = 0; index < rules.length; ++index) {
var rule = rules[index];
if (matches(rule.extension, localFilename)) {
suggestDownload(localFilename, rule.foldername);
found = true;
break;
}
}
if (!found) {
suggestDownload(localFilename, localStorage.defaultFolder);
}
}
});
// https://gist.github.com/Rob--W/ec23b9d6db9e56b7e4563f1544e0d546
function escapeHTML(str) {
return String(str)
.replace(/&/g, "&")
.replace(/"/g, """).replace(/'/g, "'")
.replace(/</g, "<").replace(/>/g, ">");
}