-
Notifications
You must be signed in to change notification settings - Fork 3
/
settings.js
109 lines (92 loc) · 3.26 KB
/
settings.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
/*
This file is part of run-a-script
Copyright (C) 2022-present Mihail Ivanchev
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
const MESSAGES = {
"deactivate.script": "Failed to deactivate current script, settings not " +
"persisted.",
"persist.settings": "Failed to persist settings, no script currently " +
"active.",
"activate.script": "Failed to activate new script but settings persisted.",
"query.settings": "Failed to retrieve persisted data, see the addon " +
"inspector.",
"validate.settings": "Failed to validate persistent settings, " +
"see the addon inspector."
};
var uuid = crypto.randomUUID();
var form = document.querySelector("form")
var apply_btn = document.getElementById("apply");
var status_lbl = document.getElementById("status");
var script_ed = document.getElementById("thescript");
var enabled_cb = document.getElementById("enabled");
var version_lbl = document.getElementById("version");
function send(id, data = null) {
browser.runtime.sendMessage({
"id": id,
"initiator": uuid,
"data": data
});
}
function notify(message) {
var {
id,
initiator,
data
} = message;
if (initiator != uuid) {
return;
}
switch (id) {
case "get-ok":
script_ed.value = data.script
enabled_cb.checked = data.enabled
setComponentsStatus(false, "");
form.removeEventListener("submit", restoreOptions);
form.addEventListener("submit", saveOptions);
break;
case "get-failed":
status_lbl.textContent = MESSAGES[data];
apply_btn.textContent = "Retry";
apply_btn.disabled = false;
break;
case "set-ok":
setComponentsStatus(false, "Settings applied successfully.");
break;
case "set-failed":
setComponentsStatus(false, MESSAGES[data]);
break;
}
}
function setComponentsStatus(disabled, msg) {
script_ed.disabled = disabled;
enabled_cb.disabled = disabled;
apply_btn.disabled = disabled;
status_lbl.textContent = msg;
}
function saveOptions(ev) {
ev.preventDefault();
setComponentsStatus(true, "Applying settings...");
var settings = {
"script": script_ed.value,
"enabled": enabled_cb.checked
};
send("set", settings);
}
async function restoreOptions() {
status_lbl.textContent = "Loading perstited settings..."
send("get");
}
browser.runtime.onMessage.addListener(notify);
document.addEventListener("DOMContentLoaded", restoreOptions);
form.addEventListener("submit", restoreOptions);
version_lbl.textContent = "v" + browser.runtime.getManifest().version;