-
-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathmanaged.js
More file actions
64 lines (59 loc) · 1.67 KB
/
Copy pathmanaged.js
File metadata and controls
64 lines (59 loc) · 1.67 KB
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
// update preferences from the managed storage or an external server
{
const configure = j => chrome.storage.local.get({
'json-guid': 'na'
}, prefs => {
if (prefs['json-guid'] !== j['json-guid'] || j['json-forced']) {
if (j['json-guid']) {
chrome.storage.local.set(j);
console.info('preferences are updated by an admin');
}
else {
console.info('update from server is rejected: no "json-guid" key.');
}
}
});
const run = () => {
chrome.storage.managed.get({
'json': ''
}, rps => {
if (!chrome.runtime.lastError) {
if (rps.json) {
try {
const j = JSON.parse(rps.json);
configure(j);
}
catch (e) {
console.error('MANAGED_JSON_PARSE_ERROR', e);
}
}
}
});
chrome.storage.local.get({
'remote-address': ''
}, prefs => {
if (prefs['remote-address']) {
fetch(prefs['remote-address']).then(r => r.json()).then(configure).catch(e => {
console.error('REMOTE_JSON_PARSE_ERROR', e);
});
}
});
};
chrome.storage.onChanged.addListener((ps, type) => {
if (type === 'managed' && ps.json) {
run();
}
if (type === 'local' && ps['remote-address']) {
run();
}
});
chrome.runtime.onInstalled.addListener(run);
chrome.runtime.onStartup.addListener(run);
chrome.runtime.onMessage.addListener((request, sender, response) => {
if (request.method === 'update-from-remote') {
fetch(request.href).then(r => r.json()).then(configure)
.then(() => response(true)).catch(e => response(e.message));
return true;
}
});
}