forked from Woundorf/foxreplace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toolbarbutton.js
85 lines (75 loc) · 3.47 KB
/
toolbarbutton.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
/** ***** BEGIN LICENSE BLOCK *****
*
* Copyright (C) 2019 Marc Ruiz Altisent. All rights reserved.
*
* This file is part of FoxReplace.
*
* FoxReplace is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* FoxReplace is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with FoxReplace. If not, see <http://www.gnu.org/licenses/>.
*
* ***** END LICENSE BLOCK ***** */
function onLoad() {
window.removeEventListener("DOMContentLoaded", onLoad);
document.getElementById("replace").addEventListener("click", showReplaceBar);
document.getElementById("replaceWithList").addEventListener("click", replaceWithList);
document.getElementById("autoReplaceOnLoad").addEventListener("click", toggleAutoReplaceOnLoad);
document.getElementById("options").addEventListener("click", showOptions);
document.getElementById('help').addEventListener('click', showHelp);
storage.getPrefs().then(prefs => {
if (prefs.autoReplaceOnLoad) document.getElementById("autoReplaceOnLoadCheck").classList.add("fa-check");
else document.getElementById("autoReplaceOnLoadCheck").classList.remove("fa-check");
});
Promise.all([browser.commands.getAll(), browser.runtime.getPlatformInfo()]).then(([commands, info]) => {
for (let command of commands) {
let elementId;
switch (command.name) {
case "_execute_sidebar_action": elementId = "replaceShortcut"; break;
case "apply-substitution-list": elementId = "replaceWithListShortcut"; break;
}
let shortcut = command.shortcut;
if (info.os == "mac") shortcut = shortcut.replace("Ctrl", browser.i18n.getMessage("keys.cmd"));
shortcut = shortcut.replace("Ctrl", browser.i18n.getMessage("keys.ctrl"));
shortcut = shortcut.replace("Shift", browser.i18n.getMessage("keys.shift"));
document.getElementById(elementId).textContent = shortcut;
}
});
}
function onUnload() {
window.removeEventListener("unload", onUnload);
document.getElementById("replace").removeEventListener("click", showReplaceBar);
document.getElementById("replaceWithList").removeEventListener("click", replaceWithList);
document.getElementById("autoReplaceOnLoad").removeEventListener("click", toggleAutoReplaceOnLoad);
document.getElementById("options").removeEventListener("click", showOptions);
document.getElementById('help').removeEventListener('click', showHelp);
}
function showReplaceBar() {
browser.sidebarAction.open()
.then(() => window.close());
}
function replaceWithList() {
browser.runtime.sendMessage({ key: "replaceWithList" })
.then(() => window.close());
}
function toggleAutoReplaceOnLoad() {
storage.getPrefs().then(prefs => {
storage.setPrefs({
autoReplaceOnLoad: !prefs.autoReplaceOnLoad
}).then(() => window.close());
});
}
function showOptions() {
browser.runtime.openOptionsPage()
.then(() => window.close());
}
function showHelp() {
browser.tabs.create({
url: 'https://github.com/Woundorf/foxreplace/wiki/FAQ'
}).then(() => window.close());
}
window.addEventListener("DOMContentLoaded", onLoad);
window.addEventListener("unload", onUnload);