This repository has been archived by the owner on Mar 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathoptions.js
58 lines (49 loc) · 1.74 KB
/
options.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
const DEFAULT_OPTIONS = {
profileViewerURL: 'https://profiler.firefox.com',
};
let currentOptions;
async function saveOptions(e) {
e.preventDefault();
const errorsNode = document.getElementById('errors');
errorsNode.innerHTML = '';
const optionsObj = {};
for (let key of Object.keys(DEFAULT_OPTIONS)) {
optionsObj[key] = document.getElementById(key).value;
switch (key) {
case 'profileViewerURL':
try {
if (optionsObj[key] !== currentOptions[key]) {
const granted = await browser.permissions.request({
origins: [getPermissionFromProfileViewerURL(optionsObj[key])],
});
await browser.permissions.remove({
origins: [getPermissionFromProfileViewerURL(currentOptions[key])],
});
if (!granted) {
throw new Error('Permission for provided URL not granted.');
}
currentOptions[key] = optionsObj[key];
}
} catch (e) {
optionsObj[key] = currentOptions[key];
const errorDiv = document.createElement('div');
errorDiv.textContent = 'Failed to set permission: ' + key;
errorsNode.appendChild(errorDiv);
console.error(e);
}
break;
}
}
browser.storage.local.set(optionsObj);
}
async function restoreOptions() {
currentOptions = await browser.storage.local.get(DEFAULT_OPTIONS);
for (let [key, value] of Object.entries(currentOptions)) {
document.getElementById(key).value = value;
}
}
function getPermissionFromProfileViewerURL(url) {
return url.replace(/:[0-9]+/, '') + '/*';
}
document.addEventListener('DOMContentLoaded', restoreOptions);
document.querySelector('form').addEventListener('submit', saveOptions);