CoCalc is a real-time collaborative commercial alternative to JupyterHub and Overleaf that provides Jupyter Notebooks, LaTeX documents, and SageMath.
CoCalc is a real-time collaborative commercial alternative to JupyterHub and Overleaf that provides Jupyter Notebooks, LaTeX documents, and SageMath.
Path: blob/master/PHEx/firefox/popup.js
Views: 720
(async () => {12/** Set a value in the local storage */3function set(key, value) {4browser.storage.local.set({ [key]: value });5};67/** Get a value from the local storage */8function get(key) {9return new Promise(resolve => {10browser.storage.local.get([key], result => {11resolve(result[key]);12})13})14};1516/** Validate the URL */17function validURL(str) {18var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol19'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name20'((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address21'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path22'(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string23'(\\#[-a-z\\d_]*)?$','i'); // fragment locator24return !!pattern.test(str) || new URL(str).hostname === "localhost";25}2627/** Checkbox (use custom P-NP URL) */28const checkbox = document.querySelector(".check")2930/** Textarea (custom P-NP URL) */31const input = document.querySelector("input")3233// input is either the custom URL, or blank.34input.value = await get("url") || "";3536// checkbox is either checked or not.37checkbox.checked = await get("checked") || false;3839input.onchange = () => {40document.querySelector("p").innerHTML = ""41}424344checkbox.addEventListener("click", async (event) => {45if (await get("checked")) {46// if already checked, `no` need to run checks47// set checked to new value, which should be false48set("checked", checkbox.checked);4950} else {5152// if we're turning on checked, we need to run a few checks53if (validURL(input.value)) {54// if the URL is valid, update url and checked to their latest values.55set("url", input.value);56set("checked", checkbox.checked);5758} else {59// if the URL is invalid, scream at them until they burst into tears60alert("[PHEx] Invalid Custom P-NP URL");61checkbox.checked = false;62}63}64});6566})();6768