forked from defpt/userChromeJs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDownloadPlus.uc.js
184 lines (164 loc) · 10.1 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
// ==UserScript==
// @name downloadPlus.uc.js
// @description 新建下载,下载重命名 + 双击复制链接、另存为...,完成下载提示音,自动关闭下载产生的空白标签
// @note 微改自用 ywzhaiqi 整合版
// @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
// ==/UserScript==
(function() {
switch (location.href) {
case "chrome://browser/content/browser.xul":
newDownload_button();// 下载按钮右键点击新建下载
download_sound_play();//下载提示音
autoClose_blankTab();//自动关闭下载产生的空白标签
break;
case "chrome://mozapps/content/downloads/unknownContentType.xul":
download_dialog_changeName();// 下载改名
download_dialog_saveas();//另存为...
download_dialog_showCompleteURL();// 下载弹出窗口双击链接复制完整链接
break;
}
function download_sound_play() {
var downloadPlaySound = {
// -- config --
DL_START : null,
DL_DONE : "file:///C:/WINDOWS/Media/chimes.wav",
DL_CANCEL: null,
DL_FAILED: null,
// -- config --
_list: null,
init: function sampleDownload_init() {
XPCOMUtils.defineLazyModuleGetter(window, "Downloads",
"resource://gre/modules/Downloads.jsm");
//window.removeEventListener("load", this, false);
window.addEventListener("unload", this, false);
//**** ダウンロード監視の追加
if (!this._list) {
Downloads.getList(Downloads.ALL).then(list => {
this._list = list;
return this._list.addView(this);
}).then(null, Cu.reportError);
}
},
uninit: function() {
window.removeEventListener("unload", this, false);
if (this._list) {
this._list.removeView(this);
}
},
onDownloadAdded: function (aDownload) {
//**** ダウンロード開始イベント
if (this.DL_START);
this.playSoundFile(this.DL_START);
},
onDownloadChanged: function (aDownload) {
//**** ダウンロードキャンセル
if (aDownload.canceled && this.DL_CANCEL)
this.playSoundFile(this.DL_CANCEL)
//**** ダウンロード失敗
if (aDownload.error && this.DL_FAILED)
this.playSoundFile(this.DL_FAILED)
//**** ダウンロード完了
if (aDownload.succeeded && this.DL_DONE)
this.playSoundFile(this.DL_DONE)
},
playSoundFile: function(aFilePath) {
if (!aFilePath)
return;
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 "unload":
this.uninit();
break;
}
}
}
downloadPlaySound.init();
}
function newDownload_button() {
document.getElementById('downloads-button').parentNode.addEventListener('click', function(e){
if(e.target.id == "downloads-button" || e.target.id == "downloads-indicator"){
if(e.button == 2 && !e.ctrlKey && !e.altKey && !e.shiftKey && !e.metaKey){
open_newDownload_dialog();
e.stopPropagation();
e.preventDefault();
}
}
}, false);
}
function open_newDownload_dialog() {
window.openDialog("data:application/vnd.mozilla.xul+xml;charset=UTF-8;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPD94bWwtc3R5bGVzaGVldCBocmVmPSJjaHJv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OwogICAgICAgIF1dPgogICAgPC9zY3JpcHQ+Cjwvd2luZG93Pg==",
"name", "top=" + (window.screenY + 50) + ",left=" + (window.screenX + 50));
}
function download_dialog_showCompleteURL() {
var s = document.querySelector("#source");
s.value = dialog.mLauncher.source.spec;
s.setAttribute("crop", "center");
s.setAttribute("tooltiptext", dialog.mLauncher.source.spec);
s.addEventListener("dblclick", function() {
Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper).copyString(dialog.mLauncher.source.spec)
}, false);
}
function download_dialog_changeName() {
if (location != "chrome://mozapps/content/downloads/unknownContentType.xul") return;
document.querySelector("#mode").addEventListener("select", function () {
if (dialog.dialogElement("save").selected) {
if (!document.querySelector("#locationtext")) {
var locationtext = document.querySelector("#location").parentNode.insertBefore(document.createElement("textbox"), document.querySelector("#location"));
locationtext.id = "locationtext";
locationtext.setAttribute("style", "margin-top:-2px;margin-bottom:-3px");
locationtext.value = document.querySelector("#location").value;
}
document.querySelector("#location").hidden = true;
document.querySelector("#locationtext").hidden = false;
} else {
document.querySelector("#locationtext").hidden = true;
document.querySelector("#location").hidden = false;
}
}, false)
dialog.dialogElement("save").click();
window.addEventListener("dialogaccept", function () {
if ((document.querySelector("#locationtext").value != document.querySelector("#location").value) && dialog.dialogElement("save").selected) {
var mainwin = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow("navigator:browser");
mainwin.eval("(" + mainwin.internalSave.toString().replace("let ", "").replace("var fpParams", "fileInfo.fileExt=null;fileInfo.fileName=aDefaultFileName;var fpParams") + ")")(dialog.mLauncher.source.asciiSpec, null, document.querySelector("#locationtext").value, null, null, null, null, null, null, mainwin.document, Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch).getBoolPref("browser.download.useDownloadDir"), null);
document.documentElement.removeAttribute("ondialogaccept");
}
}, false);
}
function download_dialog_saveas() {
var saveas = document.documentElement.getButton("extra1");
saveas.setAttribute("hidden", "false");
saveas.setAttribute("label", "\u53E6\u5B58\u4E3A");
saveas.setAttribute("oncommand", 'var mainwin = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow("navigator:browser"); mainwin.eval("(" + mainwin.internalSave.toString().replace("let ", "").replace("var fpParams", "fileInfo.fileExt=null;fileInfo.fileName=aDefaultFileName;var fpParams") + ")")(dialog.mLauncher.source.asciiSpec, null, (document.querySelector("#locationtext") ? document.querySelector("#locationtext").value : dialog.mLauncher.suggestedFileName), null, null, null, null, null, null, mainwin.document, 0, null);close()');
}
function autoClose_blankTab() {
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);\
}\
'));
}
})();