[fix] client/simple: insecure ctx clipboard copy

Uses the deprecated [`execCommand()`](https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand)
to copy content to clipboard if accessing the instance through HTTP, this method
isn't going away soon.

Closes https://github.com/searxng/searxng/issues/5359
This commit is contained in:
Ivan Gabaldon
2025-10-24 11:28:07 +02:00
committed by Markus Heiser
parent b770a46e1f
commit 8dacbbbb15
5 changed files with 59 additions and 22 deletions

View File

@@ -407,12 +407,31 @@ const toggleHelp = (keyBindings: typeof baseKeyBinding): void => {
};
const copyURLToClipboard = async (): Promise<void> => {
const currentUrlElement = document.querySelector<HTMLAnchorElement>(".result[data-vim-selected] h3 a");
assertElement(currentUrlElement);
const selectedResult = document.querySelector<HTMLElement>(".result[data-vim-selected]");
if (!selectedResult) return;
const url = currentUrlElement.getAttribute("href");
const resultAnchor = selectedResult.querySelector<HTMLAnchorElement>("a");
assertElement(resultAnchor);
const url = resultAnchor.getAttribute("href");
if (url) {
await navigator.clipboard.writeText(url);
if (window.isSecureContext) {
await navigator.clipboard.writeText(url);
} else {
const selection = window.getSelection();
if (selection) {
const node = document.createElement("span");
node.textContent = url;
resultAnchor.appendChild(node);
const range = document.createRange();
range.selectNodeContents(node);
selection.removeAllRanges();
selection.addRange(range);
document.execCommand("copy");
node.remove();
}
}
}
};

View File

@@ -1,6 +1,6 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import { http, listen, settings } from "../core/toolkit.ts";
import { assertElement, http, listen, settings } from "../core/toolkit.ts";
let engineDescriptions: Record<string, [string, string]> | undefined;
@@ -52,19 +52,25 @@ for (const engine of disableAllEngines) {
listen("click", engine, () => toggleEngines(false, engineToggles));
}
const copyHashButton: HTMLElement | null = document.querySelector<HTMLElement>("#copy-hash");
if (copyHashButton) {
listen("click", copyHashButton, async (event: Event) => {
event.preventDefault();
listen("click", "#copy-hash", async function (this: HTMLElement) {
const target = this.parentElement?.querySelector<HTMLPreElement>("pre");
assertElement(target);
const { copiedText, hash } = copyHashButton.dataset;
if (!(copiedText && hash)) return;
try {
await navigator.clipboard.writeText(hash);
copyHashButton.innerText = copiedText;
} catch (error) {
console.error("Failed to copy hash:", error);
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;
}
});

View File

@@ -121,7 +121,19 @@ listen("click", "#copy_url", async function (this: HTMLElement) {
const target = this.parentElement?.querySelector<HTMLPreElement>("pre");
assertElement(target);
await navigator.clipboard.writeText(target.innerText);
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;