-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
94 lines (85 loc) · 2.9 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
import { log } from './log.js';
import { saveSetting, getSetting } from './utils/storage.js';
import { showError } from './utils/ui.js';
import { isDuplicate, removeWebsiteFromBlockList } from './utils/blockListManager.js';
log("Options script loaded and running.");
document.addEventListener('DOMContentLoaded', function ()
{
getSetting('blockList').then(existingBlockList =>
{
if (!existingBlockList)
{
saveSetting('blockList', []); // Initialize with an empty array if not present
log('Initialized block list');
}
else
{
log("Block list already exists: ", existingBlockList);
}
});
// Current list of recently visited and blocked sites
getSetting('lastVisitList').then(existingLastVisitList =>
{
if (!existingLastVisitList)
{
saveSetting('lastVisitList', []); // Initialize with an empty array if not present
log('Initialized last visit list');
}
else
{
log("Last visit list already exists: ", existingLastVisitList);
}
});
getSetting('blockList', []).then(blockList =>
{
populateBlockList(blockList);
});
const form = document.getElementById('blocklist-form');
form.addEventListener('submit', addWebsiteToBlockList);
});
function populateBlockList(blockList) {
log("Populating block list: ", blockList);
const listContainer = document.getElementById('block-list');
listContainer.innerHTML = '';
blockList.forEach((item) => {
const listItem = document.createElement('div');
listItem.className = 'block-list-item';
listItem.textContent = `${item.url} - ${item.duration} minutes`;
const removeButton = document.createElement('button');
removeButton.textContent = 'Remove';
removeButton.onclick = function() {
const updatedBlockList = removeWebsiteFromBlockList(item.url, blockList, item.type);
saveSetting('blockList', updatedBlockList);
populateBlockList(updatedBlockList);
};
listItem.appendChild(removeButton);
listContainer.appendChild(listItem);
});
}
function addWebsiteToBlockList(event) {
event.preventDefault();
const urlInput = document.getElementById('website-url');
const durationInput = document.getElementById('block-duration');
const blockType = document.getElementById('block-type').value;
if (!urlInput.value || !durationInput.value) {
showError('Both URL and Duration are required fields!');
return;
}
getSetting('blockList', []).then(blockList => {
if (isDuplicate(urlInput.value, blockList, blockType)) {
showError('This website is already on the block list.');
return;
}
log("Adding website to block list: ", urlInput.value, " for ", durationInput.value, " minutes");
blockList.push({
url: urlInput.value,
duration: parseInt(durationInput.value, 10),
type: blockType
});
saveSetting('blockList', blockList);
populateBlockList(blockList);
urlInput.value = '';
durationInput.value = '60'; // Reset duration input to default
});
}
// TODO: Remove page from last visit list when it's removed from block list