forked from aminomancer/uc.css.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clearDownloadsButton.uc.js
84 lines (82 loc) · 3.36 KB
/
clearDownloadsButton.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
// ==UserScript==
// @name Clear Downloads Panel Button
// @version 1.4.2
// @author aminomancer
// @homepageURL https://github.com/aminomancer/uc.css.js
// @description Place a "Clear Downloads" button in the downloads panel, right next to the "Show all downloads" button.
// @downloadURL https://cdn.jsdelivr.net/gh/aminomancer/uc.css.js@master/JS/clearDownloadsButton.uc.js
// @updateURL https://cdn.jsdelivr.net/gh/aminomancer/uc.css.js@master/JS/clearDownloadsButton.uc.js
// @license This Source Code Form is subject to the terms of the Creative Commons Attribution-NonCommercial-ShareAlike International License, v. 4.0. If a copy of the CC BY-NC-SA 4.0 was not distributed with this file, You can obtain one at http://creativecommons.org/licenses/by-nc-sa/4.0/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
// ==/UserScript==
class ClearDLPanel {
constructor() {
this.makeButton();
this.setCountHandler();
Services.obs.addObserver(this, "downloads-panel-count-changed");
}
async genStrings() {
this.strings = await new Localization(["browser/downloads.ftl"], true);
const messages = await this.strings.formatMessages([
"downloads-cmd-clear-downloads",
]);
this.label = messages[0].attributes[0].value;
this.accessKey = messages[0].attributes[1].value;
return [this.label, this.accessKey];
}
async makeButton() {
this.clearPanelButton = document.createXULElement("button");
let strings = await this.genStrings();
let labelString = this.sentenceCase(strings[0]);
for (const [key, val] of Object.entries({
id: "clearDownloadsPanel",
class:
DownloadsView.downloadsHistory.className ||
"downloadsPanelFooterButton subviewbutton panel-subview-footer-button toolbarbutton-1",
oncommand: `goDoCommand('downloadsCmd_clearList'); DownloadsPanel.hidePanel();`,
label: labelString,
accesskey: strings[1],
flex: "1",
pack: "start",
})) {
this.clearPanelButton.setAttribute(key, val);
}
DownloadsView.downloadsHistory.after(this.clearPanelButton);
this.clearPanelButton.hidden = !DownloadsView._visibleViewItems?.size > 0;
this.clearPanelButton
?.closest("#downloadsFooter")
.prepend(document.createXULElement("toolbarseparator"));
this.clearPanelButton?.parentElement.setAttribute("uc-hbox", "true");
}
sentenceCase(str) {
return str
.toLocaleLowerCase()
.replace(RTL_UI ? /.$/i : /^./i, function(letter) {
return letter.toLocaleUpperCase();
})
.trim();
}
setCountHandler() {
eval(
`DownloadsView._itemCountChanged = function ${DownloadsView._itemCountChanged
.toSource()
.replace(
/hiddenCount \> 0\;\n/,
`hiddenCount > 0;\n Services.obs.notifyObservers(null, "downloads-panel-count-changed", String(count));\n`
)}`
);
}
observe(_sub, _top, data) {
this.clearPanelButton.hidden = parseInt(data) < 1;
}
}
if (gBrowserInit.delayedStartupFinished) {
new ClearDLPanel();
} else {
let delayedListener = (subject, topic) => {
if (topic == "browser-delayed-startup-finished" && subject == window) {
Services.obs.removeObserver(delayedListener, topic);
new ClearDLPanel();
}
};
Services.obs.addObserver(delayedListener, "browser-delayed-startup-finished");
}