-
Notifications
You must be signed in to change notification settings - Fork 3
/
background.js
188 lines (161 loc) · 5.26 KB
/
background.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
/*
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 VERSION = browser.runtime.getManifest().version
const ALL_VERSIONS = ["1.0.0", "1.0.1", VERSION]
const VALIDATORS = [
validateFieldPresent("version"),
validateFieldPresent("script"),
validateFieldPresent("enabled"),
validateFieldTypeAndValue("version", "string",
val => ALL_VERSIONS.includes(val)),
validateFieldTypeAndValue("script", "string"),
validateFieldTypeAndValue("enabled", "boolean")
]
var registration = null;
var queue = null;
function send(id, initiator, data = null) {
return browser.runtime.sendMessage({
"id": id,
"initiator": initiator,
"data": data
});
}
function notify(message) {
switch (message.id) {
case "get":
queue.then(() => handleGet(message.initiator));
break;
case "set":
queue.then(() => handleSet(message.initiator, message.data));
break;
}
}
async function handleGet(initiator) {
try {
await send("get-ok", initiator, await query());
} catch (err) {
await send("get-failed", initiator, err.message);
}
}
async function handleSet(initiator, settings) {
if (registration) {
try {
await registration.unregister();
} catch (err) {
console.log(`Error while unregistering script: ${err}`);
await send("set-failed", initiator, "deactivate.script");
return;
}
registration = null;
}
try {
settings.version = VERSION
await browser.storage.local.set(settings)
} catch (err) {
console.log(`Error while writing data to storage: ${err}`);
await send("set-failed", initiator, "persist.settings");
return;
}
try {
await register(settings);
} catch (err) {
console.log(`Error while registering script: ${err}`);
await send("set-failed", initiator, "activate.script");
return;
}
await send("set-ok", initiator);
}
function validateFieldPresent(field) {
return [
obj => field in obj,
`The field "${field}" was not found in the settings. The data was ` +
"probably tampered with. Please fix the problems before you can use " +
"the plugin. An easy fix is to just delete everything and start anew."
]
}
function validateFieldTypeAndValue(field, type, validator = null) {
validation_fn = function(obj) {
var val = obj[field]
return typeof val === type && (validator ? validator(val) : true)
}
return [
validation_fn,
`The field "${field}" has an invalid type or an an invalid value. ` +
"The data was probably tampered with. Please fix the problems before " +
"you can use the plugin. An easy fix is to just delete everything " +
"and start anew."
]
}
function validateSettings(settings, ui_msg) {
VALIDATORS.forEach(([validator, msg]) => {
if (!validator(settings)) {
console.log(msg);
throw new Error("validate.settings");
}
});
}
async function query() {
var settings;
try {
settings = await browser.storage.local.get()
if (Object.keys(settings).length === 0) {
// Compatiblity with 1.0.0, the settings might be in sync storage.
settings = await browser.storage.sync.get()
if (Object.keys(settings).length === 0) {
settings = {
"version": "1.0.0",
"script": "",
"enabled": false
}
} else if (!("version" in settings)) {
settings.version = "1.0.0";
}
}
} catch (err) {
console.log("Failed to retrieve data from persistent storage: " +
`${err}`);
throw new Error("query.settings");
}
validateSettings(settings);
delete settings.version
return settings;
}
async function register(settings) {
if (settings.enabled) {
var options = {
"js": [{
"file": "jquery-3.7.1.min.js"
}, {
"code": settings.script
}],
"matches": ["http://*/*", "https://*/*"],
"runAt": "document_start"
};
try {
registration = await browser.userScripts.register(options);
} catch (err) {
console.log(`Error while registering script: ${err}`)
throw err
}
}
}
browser.runtime.onMessage.addListener(notify);
queue = (async function() {
try {
await register(await query());
} catch (err) {
/* Ignore error. */
}
})();