From 13fae1c23f8888b1b9e7b794cb82b492668a2937 Mon Sep 17 00:00:00 2001 From: Hazel Noack Date: Wed, 16 Jul 2025 17:38:28 +0200 Subject: [PATCH] implemented search on youtube --- README.md | 4 +- frontend/scripts/search.js | 138 +++++++++++++++++++------------------ 2 files changed, 73 insertions(+), 69 deletions(-) diff --git a/README.md b/README.md index 736d76a..d73fb6c 100644 --- a/README.md +++ b/README.md @@ -55,9 +55,9 @@ air dev ## TODO +- implement templating for every one of the frontend files - implement functionality to clear and clean cache -- implement fetching in intervals - - host this website on a demo page +- host this website on a demo page - implement ctl - implement autocomplete with a nice go backend and fast communication. Since it all runs locally nobody should have privacy concerns NEEDS TO BE ABLE TO TOGGLED OFF FOR DEMO PAGE diff --git a/frontend/scripts/search.js b/frontend/scripts/search.js index 76ba297..536fc6c 100644 --- a/frontend/scripts/search.js +++ b/frontend/scripts/search.js @@ -4,90 +4,94 @@ const form = document.getElementById("search-form"); const input = document.getElementById("search-input"); // https://stackoverflow.com/a/3809435/16804841 -const expression = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/gi; +const expression = + /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/gi; const urlRegex = new RegExp(expression); const searchEngines = { - "g": { - action: "https://www.google.com/search", - name: "q", - }, - "d": { - action: "https://duckduckgo.com/", - name: "q", - }, - "y": { - action: "https://yandex.com/search/", - name: "text", - }, - "lure": { - action: "https://lure.sh/pkgs", - name: "q", - }, + g: { + action: "https://www.google.com/search", + name: "q", + }, + d: { + action: "https://duckduckgo.com/", + name: "q", + }, + y: { + action: "https://www.youtube.com/results", + name: "search_query", + }, + ya: { + action: "https://yandex.com/search/", + name: "text", + }, + lure: { + action: "https://lure.sh/pkgs", + name: "q", + }, }; -const translationPrefixes = [ - "t", - "translation", -] - +const translationPrefixes = ["t", "translation"]; function getDeepLUrl(s) { - const parts = s.split("-") - if (parts.length != 3) { - return undefined - } + const parts = s.split("-"); + if (parts.length != 3) { + return undefined; + } - return `https://www.deepl.com/en/translator?/#${encodeURIComponent(parts[0].trim())}/${encodeURIComponent(parts[1].trim())}/${encodeURIComponent(parts[2].trim())}`; + return `https://www.deepl.com/en/translator?/#${encodeURIComponent( + parts[0].trim() + )}/${encodeURIComponent(parts[1].trim())}/${encodeURIComponent( + parts[2].trim() + )}`; } +form.addEventListener("submit", (event) => { + event.preventDefault(); -form.addEventListener("submit", event => { - event.preventDefault(); + s = input.value; - s = input.value; + // check if url + if (s.match(urlRegex)) { + window.open(s, "_self"); + return; + } - // check if url - if (s.match(urlRegex)) { - window.open(s, "_self"); - return + // deepl translations + let doTranslation = false; + for (const value of translationPrefixes) { + const prefix = `!${value} `; + if (s.startsWith(prefix)) { + doTranslation = true; + s = s.slice(prefix.length); // Remove the !{key} prefix + break; } + } - // deepl translations - let doTranslation = false; - for (const value of translationPrefixes) { - const prefix = `!${value} `; - if (s.startsWith(prefix)) { - doTranslation = true; - s = s.slice(prefix.length); // Remove the !{key} prefix - break; - } + if (doTranslation) { + const url = getDeepLUrl(s); + if (url) { + window.open(url.toString(), "_self"); + return; } + } - if (doTranslation) { - const url = getDeepLUrl(s); - if (url) { - window.open(url.toString(), "_self"); - return; - } + // Check if the string starts with ! followed by a key from searchEngines + let selectedEngine = { + action: form.getAttribute("action"), + name: input.getAttribute("name"), + }; + + for (const [key, value] of Object.entries(searchEngines)) { + const prefix = `!${key} `; + if (s.startsWith(prefix)) { + selectedEngine = value; + s = s.slice(prefix.length); // Remove the !{key} prefix + break; } + } - // Check if the string starts with ! followed by a key from searchEngines - let selectedEngine = { - action: form.getAttribute("action"), - name: input.getAttribute("name"), - }; - - for (const [key, value] of Object.entries(searchEngines)) { - const prefix = `!${key} `; - if (s.startsWith(prefix)) { - selectedEngine = value; - s = s.slice(prefix.length); // Remove the !{key} prefix - break; - } - } - - const url = new URL(selectedEngine.action); - url.searchParams.set(selectedEngine.name, s.trim()); - window.open(url.toString(), "_self"); + const url = new URL(selectedEngine.action); + url.searchParams.set(selectedEngine.name, s.trim()); + window.open(url.toString(), "_self"); });