mirror of
https://github.com/hansjone/netx.git
synced 2026-10-09 11:50:44 +08:00
Fix WebCRT copy/paste on HTTP LAN by falling back off Clipboard API.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
4ea3b737f2
commit
ba33ab5c4f
6 changed files with 227 additions and 40 deletions
65
web/src/utils/clipboard.ts
Normal file
65
web/src/utils/clipboard.ts
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
/** Clipboard helpers that work on http://LAN (non-secure) as well as HTTPS. */
|
||||
|
||||
export function isClipboardApiAvailable(): boolean {
|
||||
try {
|
||||
return Boolean(
|
||||
typeof window !== "undefined" &&
|
||||
window.isSecureContext &&
|
||||
navigator.clipboard &&
|
||||
typeof navigator.clipboard.writeText === "function",
|
||||
);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Write text; prefers Clipboard API, falls back to execCommand('copy'). */
|
||||
export async function writeClipboardText(text: string): Promise<boolean> {
|
||||
const value = String(text || "");
|
||||
if (!value) return false;
|
||||
|
||||
if (isClipboardApiAvailable() && navigator.clipboard?.writeText) {
|
||||
try {
|
||||
await navigator.clipboard.writeText(value);
|
||||
return true;
|
||||
} catch {
|
||||
/* fall through */
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const ta = document.createElement("textarea");
|
||||
ta.value = value;
|
||||
ta.setAttribute("readonly", "");
|
||||
ta.style.cssText = "position:fixed;left:-9999px;top:0;opacity:0";
|
||||
document.body.appendChild(ta);
|
||||
ta.focus();
|
||||
ta.select();
|
||||
ta.setSelectionRange(0, value.length);
|
||||
const ok = document.execCommand("copy");
|
||||
document.body.removeChild(ta);
|
||||
return Boolean(ok);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read clipboard via Clipboard API when available.
|
||||
* On insecure http://IP, returns null — caller must use a paste-event bridge.
|
||||
*/
|
||||
export async function readClipboardText(): Promise<string | null> {
|
||||
if (
|
||||
typeof window !== "undefined" &&
|
||||
window.isSecureContext &&
|
||||
navigator.clipboard &&
|
||||
typeof navigator.clipboard.readText === "function"
|
||||
) {
|
||||
try {
|
||||
return await navigator.clipboard.readText();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue