forked from gapop/pinboard-webextension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreferences_page.js
53 lines (47 loc) · 1.8 KB
/
preferences_page.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
// Form getter & setter functions by element type
const bind_functions = {
SELECT: {
get: (input) => input.value,
set: (input, value) => {input.value = value}
},
INPUT: {
get: (input) => !!input.checked,
set: (input, value) => {input.checked = !!value}
}
}
async function bind_preference(option) {
const input = document.getElementById(option);
const bind_fn = bind_functions[input.tagName];
// Set the form input value to current value for the preference
bind_fn.set(input, await Preferences.get(option));
// Create an event listener for saving the preference value
input.addEventListener('change', async event => {
Preferences.set(option, bind_fn.get(event.target));
});
}
async function init() {
for (let option in Preferences.defaults) {
bind_preference(option);
}
// Add remove links for keyboard shortcuts
const shortcuts = await Preferences.get_keyboard_shortcuts();
const container = document.getElementById('kb-shortcuts');
const tpl = document.getElementById('kb-shortcut-tpl');
shortcuts.forEach(shortcut => {
let new_shortcut = tpl.cloneNode(true);
new_shortcut.id = null;
new_shortcut.innerHTML = new_shortcut.innerHTML
.replace('{description}', shortcut.description)
.replace('{shortcut}', shortcut.shortcut);
let link = new_shortcut.getElementsByTagName('a')[0];
link.addEventListener('click', async event => {
event.preventDefault();
Preferences.remove_keyboard_shortcut(shortcut.name);
link.parentNode.remove();
});
new_shortcut.style.display = 'block';
container.appendChild(new_shortcut);
});
}
// Bind all preferences
document.addEventListener('DOMContentLoaded', init);