-
Notifications
You must be signed in to change notification settings - Fork 3
/
popup.js
81 lines (64 loc) · 2.33 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
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
document.addEventListener('DOMContentLoaded',() =>{
console.log('DOM fully loaded and parsed');
const addButton=document.getElementById('addSitesConfirm');
const deleteButton=document.getElementById('deleteSitesConfirm');
const addSiteInput=document.getElementById('addSites');
const deleteSiteInput=document.getElementById('deleteSites');
const siteListDiv=document.getElementById('listSites');
console.log('Elements:',addButton,addSiteInput,siteListDiv);
displaySites();
addButton.addEventListener('click',() =>{
const site=addSiteInput.value.trim();
if(site){
addSite(site);
addSiteInput.value="";
displaySites();
}
});
deleteButton.addEventListener('click', () => {
const siteToDelete = deleteSiteInput.value.trim();
if (siteToDelete) {
deleteSite(siteToDelete);
deleteSiteInput.value = "";
displaySites();
}
});
});
function addSite(site){
console.log('Adding site:',site);
// Get sites from local storage
const sites=JSON.parse(localStorage.getItem('protectedSites')) || [];
console.log('Existing sites:',sites);
sites.push(site);
// Overwrite local storage
localStorage.setItem('protectedSites',JSON.stringify(sites));
}
function displaySites(){
const siteListDiv=document.getElementById('listSites');
if(!siteListDiv){
console.error('Element with id "siteList" not found.');
return;
}
console.log('Displaying sites');
siteListDiv.innerHTML='';
// Get sites from localStorage
const sites=JSON.parse(localStorage.getItem('protectedSites')) || [];
console.log('Sites to display:',sites);
// Create a list of sites
const ul=document.createElement('ul');
sites.forEach(site =>{
const li=document.createElement('li');
li.textContent=site;
ul.appendChild(li);
});
siteListDiv.appendChild(ul);
}
function deleteSite(siteToDelete){
// Get existing sites from localStorage
const sites=JSON.parse(localStorage.getItem('protectedSites')) || [];
// Filter out the site to delete
const updatedSites=sites.filter(site => site !== siteToDelete);
// Update local storage
localStorage.setItem('protectedSites',JSON.stringify(updatedSites));
displaySites();
}