-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
117 lines (99 loc) · 4.25 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
document.getElementById('blockButton').addEventListener('click', function() {
const site = document.getElementById('siteInput').value.trim();
if (site) {
chrome.storage.sync.get("blockedSites", function (data) {
const blockedSites = data.blockedSites || [];
if (!blockedSites.includes(site)) {
blockedSites.push(site);
chrome.storage.sync.set({ blockedSites: blockedSites }, function () {
alert(`${site} has been blocked.`);
updateBlockingRulesInBackground(blockedSites);
document.getElementById('siteInput').value = '';
});
} else {
alert(`${site} is already blocked.`);
}
});
}
});
function updateBlockingRulesInBackground(blockedSites) {
chrome.runtime.sendMessage({ action: "updateBlockingRules", blockedSites: blockedSites });
}
document.getElementById('adminButton').addEventListener('click', function() {
const password = prompt("Enter admin password:");
if (password) {
chrome.storage.sync.get("adminPassword", function(data) {
if (data.adminPassword === password) {
showBlockedSites();
} else {
alert("Incorrect password.");
}
});
}
});
document.getElementById('setPasswordButton').addEventListener('click', function() {
const newPassword = document.getElementById('adminPasswordInput').value.trim();
if (newPassword) {
chrome.storage.sync.set({ adminPassword: newPassword }, function() {
alert("Admin password has been set.");
document.getElementById('passwordContainer').style.display = 'none';
});
} else {
alert("Please enter a valid password.");
}
});
chrome.storage.sync.get("adminPassword", function(data) {
if (data.adminPassword === "admin") {
document.getElementById('passwordContainer').style.display = 'block';
} else {
document.getElementById('passwordContainer').style.display = 'none';
}
});
function showBlockedSites() {
chrome.storage.sync.get("blockedSites", function(data) {
const blockedSites = data.blockedSites || [];
const siteContainer = document.getElementById('blockedSitesContainer');
siteContainer.innerHTML = '';
const header = document.createElement('h1');
header.textContent = "Blocked Sites";
const topBar = document.createElement('div');
topBar.className = 'bar';
const bottomBar = document.createElement('div');
bottomBar.className = 'bar';
siteContainer.appendChild(topBar);
siteContainer.appendChild(header);
siteContainer.appendChild(bottomBar);
const siteList = blockedSites.map(site => {
const div = document.createElement('div');
div.textContent = site;
const removeButton = document.createElement('button');
removeButton.textContent = 'Remove';
removeButton.addEventListener('click', function() {
removeSite(site);
});
div.appendChild(removeButton);
return div;
});
siteList.forEach(div => siteContainer.appendChild(div));
});
}
function removeSite(site) {
const password = prompt("Enter admin password to remove the site:");
if (password) {
chrome.storage.sync.get("adminPassword", function(data) {
if (data.adminPassword === password) {
chrome.storage.sync.get("blockedSites", function(data) {
let blockedSites = data.blockedSites || [];
blockedSites = blockedSites.filter(s => s !== site);
chrome.storage.sync.set({ blockedSites: blockedSites }, function() {
alert(`${site} has been removed from the block list.`);
updateBlockingRulesInBackground(blockedSites);
showBlockedSites();
});
});
} else {
alert("Incorrect password. Site not removed.");
}
});
}
}