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/firefox/popup.js
Views: 720
1
(async () => {
2
3
/** Set a value in the local storage */
4
function set(key, value) {
5
browser.storage.local.set({ [key]: value });
6
};
7
8
/** Get a value from the local storage */
9
function get(key) {
10
return new Promise(resolve => {
11
browser.storage.local.get([key], result => {
12
resolve(result[key]);
13
})
14
})
15
};
16
17
/** Validate the URL */
18
function validURL(str) {
19
var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
20
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
21
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
22
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
23
'(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
24
'(\\#[-a-z\\d_]*)?$','i'); // fragment locator
25
return !!pattern.test(str) || new URL(str).hostname === "localhost";
26
}
27
28
/** Checkbox (use custom P-NP URL) */
29
const checkbox = document.querySelector(".check")
30
31
/** Textarea (custom P-NP URL) */
32
const input = document.querySelector("input")
33
34
// input is either the custom URL, or blank.
35
input.value = await get("url") || "";
36
37
// checkbox is either checked or not.
38
checkbox.checked = await get("checked") || false;
39
40
input.onchange = () => {
41
document.querySelector("p").innerHTML = ""
42
}
43
44
45
checkbox.addEventListener("click", async (event) => {
46
if (await get("checked")) {
47
// if already checked, `no` need to run checks
48
// set checked to new value, which should be false
49
set("checked", checkbox.checked);
50
51
} else {
52
53
// if we're turning on checked, we need to run a few checks
54
if (validURL(input.value)) {
55
// if the URL is valid, update url and checked to their latest values.
56
set("url", input.value);
57
set("checked", checkbox.checked);
58
59
} else {
60
// if the URL is invalid, scream at them until they burst into tears
61
alert("[PHEx] Invalid Custom P-NP URL");
62
checkbox.checked = false;
63
}
64
}
65
});
66
67
})();
68