-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.js
64 lines (54 loc) · 1.96 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
document.addEventListener('DOMContentLoaded', function() {
var blockedURLInput = document.getElementById("blockedURL");
var listURL = document.getElementById("listURL");
const key = "productivity under the sea";
// Function to add a new blocked URL to the list
function addBlockedURL(url) {
var li = document.createElement("li");
li.textContent = url;
listURL.appendChild(li);
}
function addToLocal(url) {
let items = JSON.parse(localStorage.getItem(key) || '[]');
if (!items.includes(url)) {
items.push(url);
}
localStorage.setItem(key, JSON.stringify(items));
}
function populateFromLocal() {
let storage = localStorage.getItem(key);
if(storage) {
const items = JSON.parse(storage);
if(items) {
for(const url of items) {
addBlockedURL(url);
}
}
}
}
populateFromLocal();
function removeFromLocal(url) {
let items = JSON.parse(localStorage.getItem(key) || '[]');
items = items.filter(item => item !== url);
localStorage.setItem(key, JSON.stringify(items));
}
blockedURLInput.addEventListener('keypress', function(event) {
if (event.keyCode === 13) {
event.preventDefault();
var newURLtext = blockedURLInput.value.trim();
console.log("New URL text:", newURLtext);
if (newURLtext !== '') {
addBlockedURL(newURLtext);
addToLocal(newURLtext);
blockedURLInput.value = '';
}
}
});
listURL.addEventListener('click', function(event) {
var clickedItem = event.target;
if (clickedItem.tagName === 'LI') { // Ensure that the clicked element is a list item
clickedItem.remove(); // Remove the clicked list item
removeFromLocal(clickedItem.innerText);
}
});
});