This repository has been archived by the owner on Oct 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
options.js
55 lines (48 loc) · 1.47 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
var default_options = ['click', 'zh_definitions'];
function setOptions(options) {
options = options.toString();
var checkboxs = document.getElementsByTagName('input');
for (var i = 0; i < checkboxs.length; i++) {
checkboxs[i].checked = false;
if (options.search(checkboxs[i].id) != -1)
checkboxs[i].checked = true;
}
}
function showOptions() {
// var options = storage.get('options', log);
var options = localStorage.getItem('options');
if (options != null)
setOptions(options);
else {
setOptions(default_options);
}
}
function getOptions() {
var checked = [];
var options = document.getElementsByTagName('input');
for (var i = 0; i < options.length; i++) {
if (options[i].checked)
checked.push(options[i].id);
}
return checked;
}
function message() {
var message = document.getElementById('message');
message.innerHTML = '设置已保存!';
setTimeout(function () { message.innerHTML = '';}, 1500);
}
function saveOptions() {
var options = getOptions();
localStorage.setItem('options', options.toString());
message();
}
function resetOptions() {
setOptions(default_options);
localStorage.setItem('options', default_options.toString());
message();
}
document.addEventListener('DOMContentLoaded', function () {
showOptions();
document.querySelector('#save').addEventListener('click', saveOptions);
document.querySelector('#reset').addEventListener('click', resetOptions);
});