-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
392 lines (344 loc) · 16 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
MIT License with Additional Restrictions and Disclaimer
Copyright (c) 2024 Preslav Kunov, repuddle.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
1. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
2. The Software, or any derivative works thereof, may not be used for commercial purposes, including but not limited to the sale of the Software, integration into commercial products, or for any use that generates monetary gain.
3. The Software, or any derivative works thereof, may not be patented or used in any way that infringes on the intellectual property rights of the original author.
4. Any modifications made to the Software must be clearly indicated, and the original copyright notice and permission notice shall be retained in the derivative works.
5. Redistributions of the Software, modified or unmodified, must reproduce the above copyright notice, this list of conditions, and the following disclaimer in the documentation and/or other materials provided with the distribution.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
document.addEventListener('DOMContentLoaded', () => {
const toggleFocusButton = document.getElementById('toggleFocus');
const addSiteButton = document.getElementById('addSite');
const addCurrentSiteButton = document.getElementById('addCurrentSite');
const siteInput = document.getElementById('siteInput');
const blockedSitesList = document.getElementById('blockedSites');
// initialize UI and event listeners
chrome.storage.local.get(['focusMode', 'blockedSites', 'blockMode'], (data) => {
if (typeof data.focusMode === 'undefined') {
chrome.storage.local.set({ focusMode: false });
}
if (typeof data.blockMode === 'undefined') {
chrome.storage.local.set({ blockMode: 1 });
}
if (!Array.isArray(data.blockedSites)) {
chrome.storage.local.set({ blockedSites: [] });
}
updateUI(data.focusMode, data.blockedSites);
// console.log(data.blockMode);
// console.log(data.focusMode);
// console.log(data.blockedSites);
});
//
//EVENT LISTENERS
siteInput.addEventListener('input', () => {
if (siteInput.value.trim() !== '') {
addSiteButton.classList.remove('hidden');
} else {
addSiteButton.classList.add('hidden');
}
});
siteInput.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
handleInputUrls(siteInput, addSiteButton);
}
})
addSiteButton.addEventListener('click', () => {
handleInputUrls(siteInput, addSiteButton);
});
toggleFocusButton.addEventListener('click', () => {
chrome.storage.local.get(['focusMode', 'blockMode'], (data) => {
const newFocusMode = !data.focusMode;
const blockMode = data.blockMode;
chrome.storage.local.set({ focusMode: newFocusMode }, () => {
updateUI(newFocusMode, null); // update UI
// send message to all tabs to toggle focus mode
chrome.tabs.query({}, (tabs) => {
tabs.forEach((tab) => {
chrome.tabs.sendMessage(tab.id, { action: 'toggleFocusMode', mode: blockMode, focusMode: newFocusMode }, (response) => {
if (chrome.runtime.lastError) {
// console.error('Error sending message: ' + chrome.runtime.lastError.message);
}
});
});
});
});
});
});
addCurrentSiteButton.addEventListener('click', () => {
chrome.storage.local.get(['focusMode', 'blockMode'], (data) => {
const newFocusMode = data.focusMode;
const blockMode = data.blockMode;
// console.log(data);
let returnedUrl = '';
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, { action: "getCurrentUrl" }, (response) => {
if (response) {
returnedUrl = response.url;
addNewSite(returnedUrl);
if (newFocusMode) {
chrome.tabs.query({}, (tabs) => {
tabs.forEach((tab) => {
chrome.tabs.sendMessage(tab.id, { action: "addUrl", mode: blockMode, focusMode: newFocusMode, site: returnedUrl }, (response) => {
if (chrome.runtime.lastError) {
// console.error('Error sending message: ' + chrome.runtime.lastError.message);
}
});
});
});
}
}
if (chrome.runtime.lastError) {
// console.error('Error sending message: ' + chrome.runtime.lastError.message);
}
});
});
});
});
blockedSitesList.addEventListener('click', (event) => {
if (event.target.classList.contains('remove-site')) {
const siteToRemove = event.target.dataset.site;
chrome.storage.local.get('blockedSites', (data) => {
const blockedSites = data.blockedSites || [];
const updatedBlockedSites = blockedSites.filter(site => site !== siteToRemove);
chrome.storage.local.set({ blockedSites: updatedBlockedSites }, () => {
updateUI(null, updatedBlockedSites);
chrome.storage.local.get(['focusMode', 'blockMode'], (data) => {
const newFocusMode = data.focusMode;
const blockMode = data.blockMode;
if (newFocusMode) {
chrome.tabs.query({}, (tabs) => {
tabs.forEach((tab) => {
chrome.tabs.sendMessage(tab.id, { action: "removeUrl", focusMode: newFocusMode, mode: blockMode, site: siteToRemove }, (response) => {
if (chrome.runtime.lastError) {
// console.error('Error sending message: ' + chrome.runtime.lastError.message);
}
});
});
});
}
});
});
});
}
});
//
// SET BLOCKING MODE (CLOSE TABS / DISPLAY OVERLAY)
const mode1Radio = document.getElementById('mode1Radio');
const mode2Radio = document.getElementById('mode2Radio');
chrome.storage.local.get('blockMode', (data) => {
if (data.blockMode === 2) {
mode2Radio.checked = true;
} else {
mode1Radio.checked = true;
}
});
mode1Radio.addEventListener('change', () => {
if (mode1Radio.checked) {
chrome.storage.local.set({ blockMode: 1 });
applyBlockMode(1);
// console.log('mode 1 checked');
}
});
mode2Radio.addEventListener('change', () => {
if (mode2Radio.checked) {
chrome.storage.local.set({ blockMode: 2 });
applyBlockMode(2);
// console.log('mode 2 checked');
}
});
//
// FUNCTIONS
function handleInputUrls(input, addbtn) {
const newSite = input.value.trim().toLowerCase();
if (newSite) {
addNewSite(newSite);
addbtn.classList.add('hidden');
chrome.storage.local.get(['focusMode', 'blockMode'], (data) => {
const newFocusMode = data.focusMode;
const blockMode = data.blockMode;
if (newFocusMode) {
chrome.tabs.query({}, (tabs) => {
tabs.forEach((tab) => {
chrome.tabs.sendMessage(tab.id, { action: "addUrl", mode: blockMode, focusMode: newFocusMode, site: newSite }, (response) => {
if (chrome.runtime.lastError) {
// console.error('Error sending message: ' + chrome.runtime.lastError.message);
}
});
});
});
}
});
}
siteInput.value = '';
}
function addNewSite(newSite) {
chrome.storage.local.get('blockedSites', (data) => {
const blockedSites = data.blockedSites || [];
if (!blockedSites.includes(newSite)) {
blockedSites.push(newSite);
chrome.storage.local.set({ blockedSites: blockedSites }, () => {
updateUI(null, blockedSites);
});
}
});
}
function applyBlockMode(mode) {
chrome.storage.local.get('focusMode', (data) => {
const newFocusMode = data.focusMode;
chrome.tabs.query({}, (tabs) => {
tabs.forEach((tab) => {
chrome.tabs.sendMessage(tab.id, { action: 'applyBlockMode', mode: mode, focusMode: newFocusMode }, (response) => {
if (chrome.runtime.lastError) {
// console.error('Error: ' + chrome.runtime.lastError.message);
}
});
});
});
});
}
function updateUI(focusMode, blockedSites) {
const toggleFocusButton = document.getElementById('toggleFocus');
if (focusMode !== null) {
const innerBtn = toggleFocusButton.querySelector('.inner-btn');
const statusSpan = toggleFocusButton.querySelector('.status');
if (focusMode) {
// If focusMode is true, set classes and text for ON state
innerBtn.classList.remove('false');
innerBtn.classList.add('true');
toggleFocusButton.classList.remove('off');
toggleFocusButton.classList.add('on');
statusSpan.textContent = 'ON';
// document.body.classList.add('body-on');
} else {
// If focusMode is false, set classes and text for OFF state
innerBtn.classList.remove('true');
innerBtn.classList.add('false');
toggleFocusButton.classList.remove('on');
toggleFocusButton.classList.add('off');
statusSpan.textContent = 'OFF';
// document.body.classList.remove('body-on');
}
}
if (blockedSites !== null) {
blockedSitesList.innerHTML = '';
blockedSites.forEach((site) => {
const li = document.createElement('li');
const p = document.createElement('p');
p.textContent = site;
const removeButton = document.createElement('span');
removeButton.innerHTML = `<svg width="23px" height="23px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <path d="M7 12L17 12" stroke="#db0000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path> </g></svg>`;
li.classList.add('remove-site');
li.dataset.site = site;
li.appendChild(removeButton);
li.appendChild(p);
blockedSitesList.prepend(li);
});
}
if (Array.isArray(blockedSites) && blockedSites.length === 0) {
blockedSitesList.innerHTML = '<div class="noUrlsAdded">List is empty.</div>';
}
}
//
// function shouldApplyFocusMode(hostname, focusMode, blockedSitesList) {
// // Check if the hostname is in the blockedSitesList
// const blockedSites = Array.from(blockedSitesList.children).map(li => li.textContent);
// return focusMode && blockedSites.includes(hostname);
// }
});
// EXPAND BUTTONS
const supBtn = document.getElementById("expand-btn");
const supDiv = document.querySelector(".expand-div");
const setBtn = document.getElementById("settings-btn");
const setDiv = document.querySelector(".settings-div");
supBtn.addEventListener("click", () => {
const expandSvg = document.querySelector(".expand-svg");
const showText = document.querySelector(".show-text");
supDiv.classList.toggle("expand");
const setSvg = document.querySelector(".settings-svg");
if (supDiv.classList.contains("expand")) {
showText.textContent = 'Hide';
supBtn.classList.add("expanded");
setDiv.classList.remove("expand");
expandSvg.setAttribute("transform", "rotate(90)");
setSvg.setAttribute("transform", "rotate(-90)");
setBtn.classList.remove("expanded");
} else {
expandSvg.setAttribute("transform", "rotate(-90)");
showText.textContent = 'Show';
supBtn.classList.remove("expanded");
}
});
setBtn.addEventListener("click", () => {
const expandSvg = document.querySelector(".settings-svg");
const linksSvg = document.querySelector(".expand-svg");
const showText = document.querySelector(".show-text");
setDiv.classList.toggle("expand");
if (setDiv.classList.contains("expand")) {
setBtn.classList.add("expanded");
expandSvg.setAttribute("transform", "rotate(90)");
supDiv.classList.remove("expand");
linksSvg.setAttribute("transform", "rotate(-90)");
supBtn.classList.remove("expanded");
showText.textContent = 'Show';
} else {
expandSvg.setAttribute("transform", "rotate(-90)");
// showText.textContent = 'Show';
setBtn.classList.remove("expanded");
}
});
//
// STARS RATING
const stars = document.querySelectorAll('.star');
function setRating(value) {
localStorage.setItem('userRating', value);
}
function getRating() {
return localStorage.getItem('userRating');
}
function updateStars() {
const rating = getRating();
if (rating) {
const value = parseInt(rating);
stars.forEach((star, index) => {
if (index < value) {
star.classList.add('selected');
} else {
star.classList.remove('selected');
}
});
}
}
updateStars();
stars.forEach((star) => {
star.addEventListener('mouseenter', function () {
const value = parseInt(this.getAttribute('data-value'));
stars.forEach((s, index) => {
if (index < value) {
s.classList.add('hovered');
} else {
s.classList.remove('hovered');
}
});
});
star.addEventListener('mouseleave', function () {
stars.forEach((s) => {
s.classList.remove('hovered');
});
updateStars();
});
star.addEventListener('click', function () {
const value = parseInt(this.getAttribute('data-value'));
setRating(value);
updateStars();
});
});
//