-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
135 lines (119 loc) · 3.98 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
function isValidURL(url) {
const pattern = /^(https?:\/\/)[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(\/[^\s]*)?$/;
return pattern.test(url);
}
document.addEventListener('DOMContentLoaded', () => {
const whitelistInput = document.getElementById('whitelistInput');
const addWhitelistBtn = document.getElementById('addWhitelist');
const whitelistContainer = document.getElementById('whitelistItemContainer');
const whitelistModeToggle = document.getElementById('whitelistModeToggle');
const blacklistInput = document.getElementById('blacklistInput');
const addBlacklistBtn = document.getElementById('addBlacklist');
const blacklistContainer = document.getElementById('blacklistItemContainer');
const sidebarButtons = document.querySelectorAll('.sidebar-btn');
const contentSections = document.querySelectorAll('.content-section');
sidebarButtons.forEach(button => {
button.addEventListener('click', () => {
sidebarButtons.forEach(btn => btn.classList.remove('active'));
contentSections.forEach(section => section.classList.remove('active'));
button.classList.add('active');
const targetId = button.getAttribute('data-target');
document.getElementById(targetId).classList.add('active');
});
});
function createListItem(website, isWhitelist = true) {
const listItem = document.createElement('div');
listItem.classList.add('list-item');
listItem.innerHTML = `
<span>${website}</span>
<button class="remove-btn">Remove</button>
`;
listItem.querySelector('.remove-btn').addEventListener('click', () => {
const action = isWhitelist ? 'removeFromWhitelist' : 'removeFromBlacklist';
chrome.runtime.sendMessage({
action: action,
website: website
}, () => {
listItem.remove();
});
});
return listItem;
}
async function loadStoredLists() {
try {
whitelistContainer.innerHTML = '';
blacklistContainer.innerHTML = '';
const whitelist = (await getStorage('whitelist')) || [];
whitelist.forEach(website => {
const listItem = createListItem(website, true);
whitelistContainer.appendChild(listItem);
});
const blacklist = (await getStorage('blacklist')) || [];
blacklist.forEach(website => {
const listItem = createListItem(website, false);
blacklistContainer.appendChild(listItem);
});
const isWhitelistModeOn = await getStorage('isWhitelistModeOn');
if (isWhitelistModeOn !== undefined) {
whitelistModeToggle.checked = isWhitelistModeOn;
}
} catch (error) {
console.error("Error loading stored lists:", error);
}
}
// change
async function getStorage(key) {
return new Promise((resolve, reject) => {
chrome.storage.local.get(key, (result) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError.message);
} else {
resolve(result[key]);
}
});
});
}
whitelistModeToggle.addEventListener('change', () => {
const isWhitelistModeOn = whitelistModeToggle.checked;
chrome.runtime.sendMessage({
action: 'storeWhitelistMode',
isWhitelistModeOn: isWhitelistModeOn
});
});
addWhitelistBtn.addEventListener('click', () => {
const website = whitelistInput.value.trim();
if (website) {
if (!isValidURL(website)) {
alert('Please enter a valid URL.');
return;
}
console.log(website)
chrome.runtime.sendMessage({
action: 'addToWhitelist',
website: website
}, () => {
const listItem = createListItem(website, true);
whitelistContainer.appendChild(listItem);
whitelistInput.value = '';
});
}
});
addBlacklistBtn.addEventListener('click', () => {
const website = blacklistInput.value.trim();
if (website) {
if (!isValidURL(website)) {
alert('Please enter a valid URL.');
return;
}
chrome.runtime.sendMessage({
action: 'addToBlacklist',
website: website
}, () => {
const listItem = createListItem(website, false);
blacklistContainer.appendChild(listItem);
blacklistInput.value = '';
});
}
});
loadStoredLists();
});