-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptions.js
132 lines (97 loc) Β· 4.38 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
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
// Use of this source code is governed by a license that can be
// found in the LICENSE file.
'use strict';
let supportedUrls = [];
function getAuthHeader(apiKey) {
return { Authorization: "Bearer " + btoa(apiKey) };
}
function updateSettings() {
let server = document.getElementById('server');
let key = document.getElementById('key');
let catid = document.getElementById('catid');
let closecheckbox = document.getElementById('closeOnDL');
let status = document.getElementById('statusMsg');
chrome.storage.sync.set({
server: server.value,
api: key.value,
categoryID: catid.value,
supportedURLs: supportedUrls,
closeOnDL: closecheckbox.checked
}, function () {
status.textContent = "π Saved!"
});
}
function checkServer() {
let server = document.getElementById('server');
let key = document.getElementById('key');
let selectedID = document.getElementById('catid').value;
let serverInfo = "";
let apiCheck = "";
let urlRegexText = "";
document.getElementById('apiCheck').textContent = "β Checking...";
// Test base server connectivity
fetch(`${server.value}/api/info`, { method: "GET", headers: getAuthHeader(key.value) })
.then(response => response.ok ? response.json() : { error: "Server didn't reply." })
.then((data) => {
if (data.error)
throw new Error(data.error);
serverInfo = `β
"${data.name}", Version ${data.version}.`;
document.getElementById('saveBtn').disabled = false;
})
.catch(error => { serverInfo = `β ${error}` })
.finally(() => document.getElementById('serverMsg').textContent = serverInfo);
// Test authenticated endpoint by getting Download plugins
fetch(`${server.value}/api/plugins/download`, { method: "GET", headers: getAuthHeader(key.value) })
.then(response => response.ok ? response.json() : { error: "API Key seems to be invalid!" })
.then((data) => {
if (data.error)
throw new Error(data.error);
apiCheck = `β
API Key is valid.`;
supportedUrls = data.map(plug => plug.url_regex)
urlRegexText = supportedUrls.join('\r\n');
})
.catch(error => { apiCheck = urlRegexText = `π ${error}` })
.finally(() => {
document.getElementById('apiCheck').textContent = apiCheck;
document.getElementById('urlRegexes').textContent = urlRegexText;
});
// Get categories from the server
document.getElementById('categoryErr').textContent = "";
fetch(`${server.value}/api/categories`, { method: "GET", headers: getAuthHeader(key.value) })
.then(response => response.ok ? response.json() : { error: "API Key seems to be invalid!" })
.then((data) => {
if (data.error)
throw new Error(data.error);
// Clear combobox and fill it again with categories from the API
const catCombobox = document.getElementById('category');
catCombobox.options.length = 0;
// Add default
catCombobox.options[catCombobox.options.length] = new Option("-- No Category --", "", true, false);
// Add static categories, select if the ID matches the one we saved
data.forEach(c => {
if (c.search === "") {
catCombobox.options[catCombobox.options.length] = new Option(c.name, c.id, false, c.id === selectedID);
}
});
document.getElementById('categoryErr').textContent = "β
Pulled categories from server correctly.";
}).catch(error => document.getElementById('categoryErr').textContent = `π ${error}`);
}
function updateCategoryDetails() {
const categoryID = document.getElementById('category').value;
document.getElementById('catid').value = categoryID;
}
let button = document.getElementById('saveBtn');
button.addEventListener('click', updateSettings);
button = document.getElementById('checkBtn');
button.addEventListener('click', checkServer);
button = document.getElementById('category');
button.addEventListener('change', updateCategoryDetails);
// Prefill fields with settings
chrome.storage.sync.get(['server', 'api', 'categoryID', 'closeOnDL'], function (result) {
document.getElementById('server').value = result.server ?? "";
document.getElementById('key').value = result.api ?? "";
document.getElementById('catid').value = result.categoryID ?? "";
document.getElementById('closeOnDL').checked = result.closeOnDL ?? false;
checkServer();
});
document.getElementById('versionInfo').textContent = chrome.runtime.getManifest().version;