-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathoptions.js
44 lines (40 loc) · 1.43 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
$(function() {
// this might trigger a bunch of superfluous sync.set directly after
// installation - but that's probably not worth bothering with.
$("input").on("change", function(){
var key=$(this).attr("id");
var type = $(this).attr("type");
var value;
if (type == "checkbox")
value = $(this).get(0).checked;
else
value = $(this).get(0).value;
//chrome.runtime.sendMessage({action: "log", msg: `Element: changed ${key}:${value}`});
chrome.runtime.sendMessage(
{action: "option", key: key, value: value});
chrome.storage.sync.set({[key]: value});
});
});
function restoreOptions() {
$("input").each(function () {
var key = $(this).attr("id");
var type = $(this).attr("type");
// to add keys without default value of false check a lookup table here
var value = false;
chrome.storage.sync.get(key, function (setting) {
if (Object.keys(setting).length) {
value = setting[key];
if (type == "checkbox")
$("#"+key).get(0).checked = value ? "checked" : undefined;
else
$("#"+key).get(0).value = value;
} else {
chrome.storage.sync.set({[key]: value});
}
//chrome.runtime.sendMessage({action: "log", msg: `Option: ${key}:${value} (${type})`});
chrome.runtime.sendMessage(
{action: "option", key: key, value: value});
});
});
}
document.addEventListener("DOMContentLoaded", restoreOptions);