-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
28 lines (25 loc) · 1.11 KB
/
popup.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
const blockList = document.querySelector('.block-list');
function buildHTMLFrom({ username, blockLevel }) {
const options = Object.keys(levelName).map(level =>
`<option value="${level}" ${(+level) === blockLevel ? 'selected' : ''}>${levelName[level]}</option>`).join('');
const id = `block-${username}`;
return `<li class="list-item"><label for="${id}">${username}</label><select id="${id}" data-username="${username}">${options}</select></li>`;
}
function handleChangeBlockLevel(event) {
const select = event.target;
const username = select.dataset.username;
const blockLevel = Number.parseInt(select.value, 10);
if (blockLevel === BLOCK_NONE) {
chrome.storage.sync.remove(username);
} else {
chrome.storage.sync.set({ [username]: blockLevel });
}
}
chrome.storage.sync.get(null, (items) => {
const listItems = Object.keys(items).map(username => {
return buildHTMLFrom({ username, blockLevel: items[username] });
}).join('');
blockList.innerHTML = listItems;
[...blockList.querySelectorAll('select')].forEach(select =>
select.addEventListener('input', handleChangeBlockLevel, false));
});