-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
46 lines (41 loc) · 1.67 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const SEPARATOR = '\n';
const DEBUG = false;
function logDebug(...args) {
if (DEBUG) {
console.log(...args);
}
}
document.addEventListener('DOMContentLoaded', function () {
loadBlocklist();
document.getElementById('save').addEventListener('click', saveBlocklist);
document.getElementById('blocklistSelect').addEventListener('change', loadBlocklist);
});
function getSelectedBlocklist() {
return document.getElementById('blocklistSelect').value;
}
function loadBlocklist() {
let blocklistType = getSelectedBlocklist();
chrome.storage.sync.get(blocklistType, function (data) {
if (data[blocklistType]) {
// Sort the blocklist alphabetically
const sortedBlocklist = data[blocklistType].sort();
document.getElementById('blocklist').value = sortedBlocklist.join(SEPARATOR);
} else {
document.getElementById('blocklist').value = '';
}
});
}
function saveBlocklist() {
logDebug('Save button has been pressed'); // log button press
const blocklistType = getSelectedBlocklist();
const blocklist = document.getElementById('blocklist').value.split(SEPARATOR).map(s => s.trim()).filter(Boolean);
// Sort the blocklist alphabetically
const sortedBlocklist = blocklist.sort();
let data = {};
data[blocklistType] = sortedBlocklist;
chrome.storage.sync.set(data, function () {
logDebug('Blocklist is set to ', sortedBlocklist);
document.getElementById('status').innerText = 'Updated. Reload the page or scroll down to load new videos to take effect';
setTimeout(function () { document.getElementById('status').innerText = ''; }, 3000);
});
}