CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
ProdigyPNP

CoCalc is a real-time collaborative commercial alternative to JupyterHub and Overleaf that provides Jupyter Notebooks, LaTeX documents, and SageMath.

GitHub Repository: ProdigyPNP/ProdigyMathGameHacking
Path: blob/master/PHEx/src/popup.js
Views: 720
1
(async() => {
2
function set(key, value) {
3
chrome.storage.local.set({ [key]: value })
4
};
5
function get(key) {
6
return new Promise(resolve => {
7
chrome.storage.local.get([key], result => {
8
resolve(result[key])
9
})
10
})
11
};
12
function validURL(str) {
13
var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
14
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
15
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
16
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
17
'(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
18
'(\\#[-a-z\\d_]*)?$','i'); // fragment locator
19
return !!pattern.test(str) || new URL(str).hostname === "localhost";
20
}
21
22
const checkbox = document.querySelector(".check")
23
const input = document.querySelector("input")
24
25
input.value = await get("url") || "";
26
checkbox.checked = await get("checked") || false;
27
28
input.onchange = () => {
29
document.querySelector("p").innerHTML = ""
30
}
31
32
checkbox.addEventListener("click", async (event) => {
33
if (await get("checked")) {
34
// if already checked, no need to run checks
35
// set checked to new value, which should be false
36
set("checked", checkbox.checked);
37
} else {
38
// if we're turning on checked, we need to run a few checks
39
if (validURL(input.value)) {
40
// if the URL is valid, update url and checked to their latest values.
41
set("url", input.value);
42
set("checked", checkbox.checked);
43
} else {
44
// if the URL is invalid, scream at them until they burst into tears
45
document.querySelector("p").innerHTML = "Invalid URL";
46
checkbox.checked = false;
47
}
48
}
49
})
50
})();
51