-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathoptions.js
122 lines (87 loc) · 2.83 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
const browser = chrome || browser
const tabQuery = (options, params = {}) => new Promise(res => {
if (!options.countPinnedTabs) params.pinned = false // only non-pinned tabs
browser.tabs.query(params, tabs => res(tabs))
})
const windowRemaining = options =>
tabQuery(options, { currentWindow: true })
.then(tabs => options.maxWindow - tabs.length)
const totalRemaining = options =>
tabQuery(options)
.then(tabs => options.maxTotal - tabs.length)
const updateBadge = options => {
if (!options.displayBadge) {
browser.browserAction.setBadgeText({ text: "" })
return;
}
Promise.all([windowRemaining(options), totalRemaining(options)])
.then(remaining => {
// console.log(remaining)
// remaining = [remainingInWindow, remainingInTotal]
browser.browserAction.setBadgeText({
text: Math.min(...remaining).toString()
})
})
}
// ----------------------------------------------------------------------------
let $inputs;
// Saves options to browser.storage
const saveOptions = () => {
const values = {};
for (let i = 0; i < $inputs.length; i++) {
const input = $inputs[i];
const value =
input.type === "checkbox" ?
input.checked :
input.value;
values[input.id] = value;
}
const options = values;
browser.storage.sync.set(options, () => {
// Update status to let user know options were saved.
const status = document.getElementById('status');
status.className = 'notice';
status.textContent = 'Options saved.';
setTimeout(() => {
status.className += ' invisible';
}, 100);
updateBadge(options)
});
}
// Restores select box and checkbox state using the preferences
// stored in browser.storage.
const restoreOptions = () => {
browser.storage.sync.get("defaultOptions", (defaults) => {
browser.storage.sync.get(defaults.defaultOptions, (options) => {
for (let i = 0; i < $inputs.length; i++) {
const input = $inputs[i];
const valueType =
input.type === "checkbox" ?
"checked" :
"value";
input[valueType] = options[input.id];
};
});
});
}
document.addEventListener('DOMContentLoaded', () => {
restoreOptions();
$inputs = document.querySelectorAll('#options input');
const onChangeInputs =
document.querySelectorAll('#options [type="checkbox"], #options [type="number"]');
const onKeyupInputs =
document.querySelectorAll('#options [type="text"], #options [type="number"]');
for (let i = 0; i < onChangeInputs.length; i++) {
onChangeInputs[i].addEventListener('change', saveOptions);
}
for (let i = 0; i < onKeyupInputs.length; i++) {
onKeyupInputs[i].addEventListener('keyup', saveOptions);
}
// show special message
if (!localStorage.getItem('readMessage') && (new Date() < new Date('09-20-2020'))) {
document.querySelector('.message').classList.remove('hidden')
setTimeout(() => {
localStorage.setItem('readMessage', true)
}, 2000);
}
});