Add go button

This commit is contained in:
foxster-mp4
2023-11-24 19:20:17 -08:00
parent 315bcc4a23
commit 7612a6ad05
3 changed files with 42 additions and 9 deletions

View File

@@ -11,27 +11,48 @@ import { isValidHTTPURL, open } from "./common/modules/utilities.js";
const { default: sources } = await import("./common/assets/sources.json", { assert: { type: "json" } });
(function main() {
const textField = document.querySelector("input");
for (const url of sources)
insertSource(url);
const textField = document.querySelector("input");
const goButton = document.getElementById("go");
const viewSource = () => {
const sourceURL = textField.value;
if (!isValidHTTPURL(sourceURL))
alert("Invalid HTTP URL.");
else open(`./view/?source=${sourceURL}`);
};
// If source provided
if (urlSearchParams.has('source')) {
textField.value = urlSearchParams.get("source");
textField.focus();
}
textField.addEventListener("keypress", function (event) {
textField.addEventListener("keypress", event => {
if (event.key === "Enter") {
event.preventDefault();
const sourceURL = textField.value;
if (!isValidHTTPURL(sourceURL))
alert("Invalid HTTP URL.");
else open(`./view/?source=${sourceURL}`);
viewSource();
}
});
const toggleGoButton = () => {
if (textField.value.length) {
goButton.style.display = "block";
setTimeout(() => {
goButton.style.opacity = 1;
}, 5);
} else {
goButton.style.opacity = 0;
setTimeout(() => {
goButton.style.display = "none";
}, 125);
}
}; toggleGoButton();
textField.addEventListener("input", toggleGoButton);
goButton.addEventListener("click", viewSource);
async function insertSource(url) {
fetch(url).then(data => data.json()).then(source => {
document.getElementById("source-code").insertAdjacentHTML("beforebegin",`