Fall back when crypto.randomUUID / clipboard are unavailable

Both APIs only exist in secure contexts, so opening a PDF threw
"crypto.randomUUID is not a function" when the app was served over
plain HTTP (e.g. the Coolify sslip.io preview domain).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chaloemphong
2026-11-19 10:25:42 +07:00
parent ac91737c63
commit efb34fbec1
2 changed files with 15 additions and 2 deletions
+6 -1
View File
@@ -593,7 +593,12 @@ function OcrPanel() {
<>
<p className="lbl">{tr('ข้อความที่อ่านได้', 'Recognised text')}</p>
<textarea className="ocr-out" readOnly value={text} />
<button className="btn" onClick={() => navigator.clipboard.writeText(text)}>{tr('คัดลอก', 'Copy')}</button>
<button className="btn" onClick={(e) => {
// navigator.clipboard is undefined outside secure contexts; fall back to selecting the textarea.
if (navigator.clipboard) { void navigator.clipboard.writeText(text); return; }
const ta = e.currentTarget.previousElementSibling as HTMLTextAreaElement | null;
ta?.select(); document.execCommand('copy');
}}>{tr('คัดลอก', 'Copy')}</button>
</>
)}
</section>
+9 -1
View File
@@ -1,7 +1,15 @@
import type { Pending } from './types';
import { tr } from '../i18n';
export const uid = () => crypto.randomUUID();
// crypto.randomUUID only exists in secure contexts (HTTPS/localhost); fall back to getRandomValues elsewhere.
export const uid = (): string => {
if (typeof crypto.randomUUID === 'function') return crypto.randomUUID();
const b = crypto.getRandomValues(new Uint8Array(16));
b[6] = (b[6] & 0x0f) | 0x40;
b[8] = (b[8] & 0x3f) | 0x80;
const h = Array.from(b, (x) => x.toString(16).padStart(2, '0')).join('');
return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20)}`;
};
export function download(data: Blob | Uint8Array, name: string, type = 'application/pdf') {
const blob = data instanceof Blob ? data : new Blob([data as BlobPart], { type });