forked from ywzhaiqi/userChromeJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdownloadPlus.uc.js
372 lines (338 loc) · 18.3 KB
/
downloadPlus.uc.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
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
// ==UserScript==
// @name downloadPlus.uc.js
// @description 新建下载,删除文件,下载窗口(下载重命名 + 双击复制链接 + 另存为 + 保存并打开),完成下载提示音,自动关闭下载产生的空白标签
// @note ywzhaiqi 修改整合(Alice0775、紫云飞)
// @include chrome://browser/content/browser.xul
// @include chrome://browser/content/places/places.xul
// @include chrome://mozapps/content/downloads/unknownContentType.xul
// @include chrome://mozapps/content/downloads/downloads.xul
// @charset utf-8
// @homepageURL https://github.com/ywzhaiqi/userChromeJS/blob/master/downloadPlus.uc.js
// ==/UserScript==
(function() {
let { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
if (!window.Services) Cu.import("resource://gre/modules/Services.jsm");
var ns = {
get appVersion() Services.appinfo.version.split(".")[0],
get mainwin() Services.wm.getMostRecentWindow("navigator:browser"),
init: function(){
switch(location.href){
case "chrome://browser/content/browser.xul":
ns.newDownload(); // 下载按钮右键点击新建下载
ns.autoClose_blankTab(); // 自动关闭下载产生的空白标签
downloadSoundPlay(); // 下载完成提示音
ns.saveAndOpen_on_main(); // 跟下面的 saveAndOpen 配合使用
// 不完美?
// ns.downloadsPanel_removeFile(); // DownloadsPanel 右键新增移除下载文件功能
break;
case "chrome://mozapps/content/downloads/unknownContentType.xul":
ns.download_dialog_changeName(); // 下载改名
ns.download_dialog_saveAs(); // 下载另存为
ns.download_dialog_showCompleteURL(); // 下载弹出窗口双击链接复制完整链接
ns.saveAndOpen();
break;
case "chrome://browser/content/places/places.xul":
// newDownload_places(); // 书签窗口新增 "新建下载" 按钮
break;
}
},
newDownload: function(){
let parent = $('downloads-button').parentNode;
parent.addEventListener('click', ns.downloadsBtn_clicked, false);
},
newDownload_on_places: function () {
var button = $("placesToolbar").insertBefore(document.createElement("toolbarbutton"), $("clearDownloadsButton"));
button.id = "createNewDownload";
button.label = "新建下载";
button.style.paddingRight = "9px";
button.addEventListener("command", ns.open_newDownload_dialog, false);
window.addEventListener("click", function(e) {
button.style.display = ($("searchFilter").attributes.getNamedItem("collection").value == "downloads") ? "-moz-box" : "";
}, false);
},
downloadsBtn_clicked: function(event){
if(event.target.id == "downloads-button" || event.target.id == "downloads-indicator"){
if(event.button == 2 && !event.ctrlKey && !event.altKey && !event.shiftKey && !event.metaKey){
event.stopPropagation();
event.preventDefault();
ns.open_newDownload_dialog();
}
}
},
newDownloadDialogXUL: '<?xml version="1.0" encoding="utf-8"?>\
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>\
<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" width="500" height="300" title="新建下载任务">\
<hbox align="center" tooltiptext="http://www.example.com/[1-100-3] ([开始-结束-位数])">\
<label value="批量任务"></label>\
<textbox flex="1"/>\
</hbox>\
<textbox id="urls" multiline="true" flex="1"/>\
<hbox dir="reverse">\
<button label="开始下载"/>\
</hbox>\
<script>\
<![CDATA[\
function ParseURLs() {\
var batchurl = document.querySelector("textbox").value;\
if (/\\[\\d+-\\d+(-\\d+)?\\]/.test(batchurl)) {\
for (let match = batchurl.match(/\\[(\\d+)-(\\d+)-?(\\d+)?\\]/), i = match[1], j = match[2], k = match[3], urls = []; i <= j; i++) {\
urls.push(batchurl.replace(/\\[\\d+-\\d+(-\\d+)?\\]/, (i + "").length < k ? (eval("10e" + (k - (i + "").length)) + "").slice(2) + i : i));\
}\
document.querySelector("#urls").value = urls.join("\\n");\
} else {\
document.querySelector("#urls").value = batchurl;\
}\
}\
document.querySelector("textbox").addEventListener("keyup", ParseURLs, false);\
document.querySelector("button").addEventListener("command", function () {\
document.querySelector("#urls").value.split("\\n").forEach(function (url) {\
opener.saveURL(url , null, null, null, true, null, document);\
});\
close()\
}, false);\
document.querySelector("textbox").value = (opener.opener || window.opener).readFromClipboard();\
ParseURLs();\
]]>\
</script>\
</window>\
',
open_newDownload_dialog: function(){
window.openDialog("data:application/vnd.mozilla.xul+xml;charset=UTF-8," + encodeURIComponent(ns.newDownloadDialogXUL),
"name", "top=" + (window.screenY + 50) + ",left=" + (window.screenX + 50));
},
downloadsPanel_removeFile: function() {
window.removeDownloadfile = {
removeStatus: function() {
var RMBtn = $("removeDownload");
var state = $("downloadsListBox").selectedItems[0].getAttribute('state');
RMBtn.setAttribute("disabled", "true");
if (state != "0" && state != "4" && state != "5")
RMBtn.removeAttribute("disabled");
},
removeMenu: function() {
try {
this.removeStatus();
} catch (e) {};
let menuitem = $("removeDownload");
if (!menuitem){
menuitem = document.createElement("menuitem"),
rlm = document.querySelector('.downloadRemoveFromHistoryMenuItem');
menuitem.setAttribute("label", rlm.getAttribute("label").indexOf("History") != -1 ? "Delete File" : "从硬盘中删除");
menuitem.setAttribute("id", "removeDownload");
menuitem.onclick = function() {
var path = decodeURI(DownloadsView.richListBox.selectedItem.image)
.replace(/moz\-icon\:\/\/file\:\/\/\//, "").replace(/\?size\=32$/, "")
.replace(/\?size\=32\&state\=normal$/, "").replace(/\//g, "\\\\");
if (DownloadsView.richListBox.selectedItem.getAttribute('state') == "2") {
path = path + ".part";
}
var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
file.initWithPath(path);
file.exists() && file.remove(0);
new DownloadsViewItemController(DownloadsView.richListBox.selectedItem).doCommand("cmd_delete");
};
$("downloadsContextMenu").appendChild(rlm);
$("downloadsContextMenu").insertBefore(menuitem, rlm.nextSibling);
}
this.removeStatus();
},
Start: function() {
$("downloadsContextMenu").addEventListener("popupshowing", this.removeMenu, false);
}
}
try {
eval("DownloadsPanel.showPanel = " + DownloadsPanel.showPanel.toString()
.replace(/DownloadsPanel\.\_openPopupIfDataReady\(\)/, "{$&;removeDownloadfile\.Start\(\);}"));
} catch (e) {}
},
autoClose_blankTab: function () {
eval("gBrowser.mTabProgressListener = " + gBrowser.mTabProgressListener.toString().replace(/(?=var location)/, '\
if (aWebProgress.DOMWindow.document.documentURI == "about:blank"\
&& aRequest.QueryInterface(nsIChannel).URI.spec != "about:blank") {\
aWebProgress.DOMWindow.setTimeout(function() {\
!aWebProgress.isLoadingDocument && aWebProgress.DOMWindow.close();\
}, 100);\
}\
'));
},
saveAndOpen: function(){
let acceptBtn = document.documentElement.getButton("accept");
let saveBtn = $("save");
var saveAndOpen = document.getAnonymousElementByAttribute(document.querySelector("*"), "dlgtype", "extra2");
saveAndOpen.parentNode.insertBefore(saveAndOpen, acceptBtn.nextSibling.nextSibling);
saveAndOpen.setAttribute("hidden", "false");
saveAndOpen.setAttribute("label", "保存并打开");
saveAndOpen.addEventListener("command", function(event){
ns.mainwin.saveAndOpen.urls.push(dialog.mLauncher.source.asciiSpec);
saveBtn.click();
acceptBtn.disabled = 0;
acceptBtn.click()
}, false);
},
// 作用于 main 窗口
saveAndOpen_on_main: function(){
let downloadManager = Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager);
window.saveAndOpen = {
urls: [],
onStateChange: function (prog, req, flags, status, dl) {
if (flags == 327696 && !! ~this.urls.indexOf(dl.source.spec)) {
this.urls[this.urls.indexOf(dl.source.spec)] = "";
downloadManager.getDownload(downloadManager.DBConnection.lastInsertRowID).targetFile.launch();
}
},
onSecurityChange: function (prog, req, state, dl) {},
onProgressChange: function (prog, req, prog, progMax, tProg, tProgMax, dl) {},
onDownloadStateChange: function (state, dl) {}
}
downloadManager.addListener(saveAndOpen);
},
download_dialog_changeName: function () {
if (location != "chrome://mozapps/content/downloads/unknownContentType.xul") return;
let downLocation = $("location");
let locationText = $("locationtext");
$("mode").addEventListener("select", function() {
if (dialog.dialogElement("save").selected) {
if (!locationText) {
locationText = downLocation.parentNode.insertBefore(document.createElement("textbox"), downLocation);
locationText.id = "locationtext";
locationText.setAttribute("style", "margin-top:-2px;margin-bottom:-3px");
locationText.value = downLocation.value;
}
downLocation.hidden = true;
locationText.hidden = false;
} else {
locationText.hidden = true;
downLocation.hidden = false;
}
}, false)
dialog.dialogElement("save").selected && dialog.dialogElement("save").click();
window.addEventListener("dialogaccept", function() {
if ((locationText.value != downLocation.value) && dialog.dialogElement("save").selected) {
if(ns.appVersion >= 23){
ns.mainwin.eval("(" + ns.mainwin.internalSave.toString()
.replace("let ", "")
.replace("var fpParams", "fileInfo.fileExt=null;fileInfo.fileName=aDefaultFileName;var fpParams") + ")")
(dialog.mLauncher.source.asciiSpec, null, locationText.value, null, null, null, null, null, null,
ns.mainwin.document, Services.prefs.getBoolPref("browser.download.useDownloadDir"), null);
}else{
dialog.mLauncher.saveToDisk(dialog.promptForSaveToFile(dialog.mLauncher, window, locationText.value), 1);
dialog.onCancel = null;
}
document.documentElement.removeAttribute("ondialogaccept");
}
}, false);
},
download_dialog_saveAs: function () {
var saveas = document.documentElement.getButton("extra1");
saveas.setAttribute("hidden", "false");
saveas.setAttribute("label", "另存为");
saveas.addEventListener("command", function(event){
let locationText = $("locationtext");
if(ns.appVersion >= 23){
ns.mainwin.eval("(" + ns.mainwin.internalSave.toString().replace("let ", "").replace("var fpParams", "fileInfo.fileExt=null;fileInfo.fileName=aDefaultFileName;var fpParams") + ")")
(dialog.mLauncher.source.asciiSpec, null, (locationText ? locationText.value : dialog.mLauncher.suggestedFileName), null, null, null, null, null, null, ns.mainwin.document, 0, null);
}else{
var file = (dialog.promptForSaveToFileAsync || dialog.promptForSaveToFile).call(dialog, dialog.mLauncher, window, dialog.mLauncher.suggestedFileName, "", true);
if (file) {
dialog.mLauncher.saveToDisk(file, 1);
dialog.onCancel = function() {};
}
}
close();
}, false);
},
download_dialog_showCompleteURL: function () {
var s = $("source");
s.value = dialog.mLauncher.source.spec;
s.setAttribute("crop", "center");
s.setAttribute("tooltiptext", dialog.mLauncher.source.spec);
s.addEventListener("dblclick", function() {
Cc["@mozilla.org/widget/clipboardhelper;1"].getService(Ci.nsIClipboardHelper)
.copyString(dialog.mLauncher.source.spec)
}, false);
}
};
ns.init();
// From downloadSoundPlay.uc.js By Alice0775
function downloadSoundPlay(){
if(window.downloadPlaySound){
window.downloadPlaySound.uninit();
delete window.downloadPlaySound;
}
window.downloadPlaySound = {
DL_START: "",
DL_DONE: "file:///C:/WINDOWS/Media/chimes.wav", //设置响铃
DL_CANCEL: "",
DL_FAILED: "",
observerService: null,
init: function sampleDownload_init() {
window.addEventListener("unload", this, false);
this.observerService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
this.observerService.addObserver(this, "dl-start", false);
this.observerService.addObserver(this, "dl-done", false);
this.observerService.addObserver(this, "dl-cancel", false);
this.observerService.addObserver(this, "dl-failed", false);
},
uninit: function() {
window.removeEventListener("unload", this, false);
this.observerService.removeObserver(this, "dl-start");
this.observerService.removeObserver(this, "dl-done");
this.observerService.removeObserver(this, "dl-cancel");
this.observerService.removeObserver(this, "dl-failed");
},
observe: function(subject, topic, state) {
var oDownload = subject.QueryInterface(Components.interfaces.nsIDownload);
var oFile = null;
try {
oFile = oDownload.targetFile;
} catch (e) {
oFile = oDownload.target;
}
if (topic == "dl-start") {
if (this.DL_START)
this.playSoundFile(this.DL_START);
}
if (topic == "dl-cancel") {
if (this.DL_CANCEL) this.playSoundFile(this.DL_CANCEL);
} else if (topic == "dl-failed") {
if (this.DL_FAILED) this.playSoundFile(this.DL_FAILED);
} else if (topic == "dl-done") {
if (this.DL_DONE) this.playSoundFile(this.DL_DONE);
}
},
playSoundFile: function(aFilePath) {
var ios = Components.classes["@mozilla.org/network/io-service;1"]
.createInstance(Components.interfaces["nsIIOService"]);
try {
var uri = ios.newURI(aFilePath, "UTF-8", null);
} catch (e) {
return;
}
var file = uri.QueryInterface(Components.interfaces.nsIFileURL).file;
if (!file.exists()) return;
this.play(uri);
},
play: function(aUri) {
var sound = Components.classes["@mozilla.org/sound;1"]
.createInstance(Components.interfaces["nsISound"]);
sound.play(aUri);
},
handleEvent: function(event) {
switch (event.type) {
case "load":
this.init();
break;
case "unload":
this.uninit();
break;
}
}
};
window.downloadPlaySound.init();
}
function $(id, doc){
doc = doc || document;
return doc.getElementById(id);
}
})();