-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage_websites.js
84 lines (79 loc) · 3.26 KB
/
manage_websites.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
var websiteDiv;
document.getElementById("bb4").onclick = () => mainMenu();
document.getElementById("websites-submit").onclick = () => {
addNewSite(document.getElementById("websites-add").value);
document.getElementById("websites-add").value = "";
}
document.getElementById("discard-button").onclick = () => document.getElementById("WebsiteBlocked").children = discardSites(document.getElementById("WebsiteBlocked").children);
function setWebsites() { // creates the set websites GUI
document.getElementById("MainMenu").hidden = true;
document.getElementById("SetupTimer").hidden = true;
document.getElementById("TimerOn").hidden = true;
document.getElementById("abortedScreen").hidden = true;
document.getElementById("websitesScreen").hidden = false;
websiteDiv = document.createElement("div");
websiteDiv.id = "WebsiteBlocked";
document.getElementById("websites-list").appendChild(websiteDiv);
const things = getCookie("banned").split(", ")
for (let i = 0; i < things.length; i += 1) addToBlockedWebsitesList(things[i]); // add website to blocked list
}
function addToBlockedWebsitesList(inner) { // add known blocked website to the blocked website list (in HTML)
let check = document.createElement("INPUT");
check.setAttribute("type", "checkbox");
check.name = inner;
let label = document.createElement("LABEL");
label.innerText = inner + "\n";
label.className = "boxes";
label.name = inner;
websiteDiv.appendChild(check);
websiteDiv.appendChild(label);
}
function addNewSite(link) { // tries (if possible) to add a new cookie and site
if (link == "") return;
else if (link.substring(0, 8) != "https://" && link.substring(0, 7) != "http://") alert('Please enter a valid URL precending with "https://" or "http://".');
else if (link.includes(" ")) alert('Please remove any whitespace.');
else {
try {
var url = new URL(link);
link = String(url.origin)+"/";
foundValid = true;
} catch (_){
foundValid = false;
}
if (!foundValid) {
alert("Please enter a valid URL with a proper domain.");
return;
}
if (getCookie("banned").includes(link)) {
alert("This website is already blocked.")
return;
}
arr = new Set(getCookie("banned").split(", "))
arr.add(link);
addCookie("banned", Array.from(arr).join(", "));
addToBlockedWebsitesList(link, true);
}
}
function discardSites(instances) {
var ind = instances.length - 2;
while (ind >= 0) {
if (instances[ind].checked) {
var arr = getCookie("banned").split(", ");
for (let i = 0; i < arr.length; i += 1) {
var found = true;
for (let j = 0; j < Math.min(instances[ind + 1].innerText.length, arr[i].length); j += 1) {
if (arr[i][j] != instances[ind + 1].innerText[j]) found = false;
}
if (found) {
arr.splice(i, 1);
addCookie("banned", arr.join(", "));
break;
}
}
instances[ind].remove();
instances[ind].remove();
}
ind -= 2;
}
return instances;
}