// SPDX-License-Identifier: AGPL-3.0-or-later import { http, listen, settings } from "../toolkit.ts"; import { assertElement } from "../util/assertElement.ts"; let engineDescriptions: Record | undefined; const loadEngineDescriptions = async (): Promise => { if (engineDescriptions) return; try { const res = await http("GET", "engine_descriptions.json"); engineDescriptions = await res.json(); } catch (error) { console.error("Error fetching engineDescriptions:", error); } if (!engineDescriptions) return; for (const [engine_name, [description, source]] of Object.entries(engineDescriptions)) { const elements = document.querySelectorAll(`[data-engine-name="${engine_name}"] .engine-description`); const sourceText = ` (${settings.translations?.Source}: ${source})`; for (const element of elements) { element.innerHTML = description + sourceText; } } }; const toggleEngines = (enable: boolean, engineToggles: NodeListOf): void => { for (const engineToggle of engineToggles) { // check if element visible, so that only engines of the current category are modified if (engineToggle.offsetParent) { engineToggle.checked = !enable; } } }; const engineElements: NodeListOf = document.querySelectorAll("[data-engine-name]"); for (const engineElement of engineElements) { listen("mouseenter", engineElement, loadEngineDescriptions); } const engineToggles: NodeListOf = document.querySelectorAll( "tbody input[type=checkbox][class~=checkbox-onoff]" ); const enableAllEngines: NodeListOf = document.querySelectorAll(".enable-all-engines"); for (const engine of enableAllEngines) { listen("click", engine, () => toggleEngines(true, engineToggles)); } const disableAllEngines: NodeListOf = document.querySelectorAll(".disable-all-engines"); for (const engine of disableAllEngines) { listen("click", engine, () => toggleEngines(false, engineToggles)); } listen("click", "#copy-hash", async function (this: HTMLElement) { const target = this.parentElement?.querySelector("pre"); assertElement(target); if (window.isSecureContext) { await navigator.clipboard.writeText(target.innerText); } else { const selection = window.getSelection(); if (selection) { const range = document.createRange(); range.selectNodeContents(target); selection.removeAllRanges(); selection.addRange(range); document.execCommand("copy"); } } const copiedText = this.dataset.copiedText; if (copiedText) { this.innerText = copiedText; } });