forked from apiraino/link_cleaner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefs.js
78 lines (66 loc) · 1.98 KB
/
prefs.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
"use strict";
/*
get/set stored settings
ref: https://github.com/mdn/webextensions-examples/tree/master/forget-it
*/
/*
Default settings. If there is nothing in storage, use these values.
*/
const defaultSettings = {
dataTypes: [
{"clean_amp_links": false},
{"redirect_reddit_nojs": false}
]
};
/*
Settings actionable by the options datatypes
These values are used by the extension
*/
let settings = {
'clean_amp_links': false,
'redirect_reddit_nojs': false,
};
/*
Listener for settings update.
Sender is in options.js
*/
browser.runtime.onMessage.addListener(notify);
function notify(message) {
update_settings(message);
}
function onError(e) {
console.error(e);
}
/*
On startup, check whether we have stored settings.
If we don't, then store the default settings.
*/
function checkStoredSettings(storedSettings) {
console.debug("[checkstoredsettings] storedsettings: ");
// console.debug(storedSettings);
if (!storedSettings.dataTypes) {
console.debug("[checkStoredSettings] Could NOT read settings, will set defaults");
browser.storage.local.set(defaultSettings).then(function(res){
console.debug("[checkStoredSettings] defaults set");
}, onError);
} else {
console.debug("[checkStoredSettings] Settings successfully loaded");
update_settings(storedSettings.dataTypes);
}
// console.debug("[checkStoredSettings] new_settings: ");
// console.debug(settings);
// check_storage();
}
function check_storage() {
browser.storage.local.get().then(function(res){
console.debug("[check_storage] got storage");
console.debug(res);
}, onError);
}
// also invoked by the notification message
function update_settings(new_settings) {
// console.debug("[update_settings] updating with: ", new_settings);
settings = new_settings;
}
const gettingStoredSettings = browser.storage.local.get();
gettingStoredSettings.then(checkStoredSettings, onError);