-
Notifications
You must be signed in to change notification settings - Fork 5
/
menus.js
207 lines (183 loc) · 6.71 KB
/
menus.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
var downloadInfo = {};
function saveImageTo(info, tab) {
var url = info.srcUrl;
chrome.storage.local.get({
entries: []
}, function(items) {
var index = items.entries.findIndex(function(e, i) {
return e.title == info.menuItemId;
});
downloadInfo.directory = items.entries[index].directory.trim();
var dialog = items.entries[index].dialog;
var ifExists = items.entries[index].ifExists;
if ("rename" in items.entries[index]) {
downloadInfo.rename = true;
downloadInfo.renameName = items.entries[index].rename.name;
downloadInfo.renamePrefix = items.entries[index].rename.prefix;
downloadInfo.renameSuffix = items.entries[index].rename.suffix;
} else {
downloadInfo.rename = false;
}
downloadInfo.conflictAction = "uniquify";
switch (ifExists) {
case "rename":
downloadInfo.conflictAction = "uniquify";
break;
case "overwrite":
downloadInfo.conflictAction = "overwrite";
break;
case "dialog":
downloadInfo.conflictAction = "prompt";
break;
default:
break;
}
chrome.downloads.download({
url: url,
saveAs: dialog
}, function(downloadId) {
downloadInfo.id = downloadId;
});
});
}
function saveToDefault(info, tab) {
var url = info.srcUrl;
chrome.downloads.download({
url: url,
conflictAction: "uniquify",
saveAs: false
}, function(downloadId) {
downloadInfo.id = downloadId;
downloadInfo.directory = '_default_';
});
}
function createMenus() {
chrome.contextMenus.create({
"id": "SIT",
"title": "Save Image to...",
"contexts": ["image", "video"]
});
chrome.storage.local.get({
entries: []
}, function(items) {
createEntriesMenus(items);
});
chrome.storage.local.get({
defaultEntry: true
}, function(items) {
if (items.defaultEntry === true)
{
chrome.contextMenus.create({
"parentId": "SIT",
"contexts": ["image", "video"],
"id": "_____internal_saveimagerouter_default_____",
"title": "_default_",
"onclick": saveToDefault
});
}
});
}
createMenus();
chrome.downloads.onDeterminingFilename.addListener(function(item, suggest) {
if (item.id === downloadInfo.id) {
if (downloadInfo.directory === '_default_') {
// do not suggest anything, use defaults.
} else {
// get host name and file path
var a = document.createElement('a');
a.href = item.url;
var hostname = a.hostname;
var path_ = a.pathname.replace(new RegExp('/', 'g'), '_');
// get filename determined by Chrome
var filename = item.filename;
// separate extension from the filename
var ext = filename.substr(filename.lastIndexOf('.') + 1);
filename = filename.substring(0, filename.lastIndexOf('.'));
// get directory from the saved download info
var directory = downloadInfo.directory;
// do renaming of the filename if required
if (downloadInfo.rename) {
var date = new Date();
var dateStr = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + "-" + date.getHours() + "-" + date.getMinutes() + "-" + date.getSeconds();
switch (downloadInfo.renameName) {
case 'keep original name':
break;
case '"SaveImageRouter"':
filename = "SaveImageRouter";
break;
case 'empty name':
filename = "";
break;
default:
break;
}
switch (downloadInfo.renamePrefix) {
case 'no prefix':
break;
case 'timestamp prefix':
filename = dateStr + '_' + filename;
break;
case 'host prefix':
filename = hostname + '_' + filename;
break;
case 'URL path':
filename = path_ + '_' + filename;
break;
default:
break;
}
switch (downloadInfo.renameSuffix) {
case 'no suffix':
break;
case 'timestamp suffix':
filename = filename + '_' + dateStr;
break;
case 'host suffix':
filename = filename + '_' + hostname;
break;
case 'URL path':
filename = filename + '_' + path_;
break;
default:
break;
}
}
// make sure renaming didn't erase the filename completely (no prefix + empty name + no suffix)
if (filename == "") {
filename = "SaveImageRouter";
}
// add back the extension to the filename
filename = filename + "." + ext;
// prepend the directory to the modified filename
if (directory === "") {
// base directory, do not add slash
filename = directory + filename;
} else {
filename = directory + "/" + filename;
}
// make sure the modified filename doesn't contain any illegal characters
filename = filename.replace(/[\#\?].*$/,'');
// suggest the new filename to Chrome
suggest({ filename: filename, conflictAction: downloadInfo.conflictAction });
}
}
// reset the download info for the next download item
downloadInfo = {};
});
// update menus when new entries are saved from the options
chrome.storage.onChanged.addListener(function(changes, namespace) {
chrome.contextMenus.removeAll(function() {
createMenus();
});
});
function createEntriesMenus(items) {
items.entries.forEach(function(entry) {
chrome.contextMenus.create({
"parentId": "SIT",
"contexts": ["image", "video"],
"id": entry.title,
"title": entry.title,
"onclick": saveImageTo
});
});
};