fix: fallback для crypto.randomUUID в insecure context (HTTP по IP)

Под голым HTTP (например http://192.168.87.76:8103/ для dev-сервера)
crypto.randomUUID отсутствует — secure context требует HTTPS или
localhost. Без fallback скрипт падал с TypeError до рендера и
страница оставалась пустой.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
anor
2026-05-06 16:26:28 +10:00
co-authored by Claude Opus 4.7
parent a20a28b60b
commit 9f2e355298
+11 -2
View File
@@ -167,10 +167,19 @@ const API = '';
let token = localStorage.getItem('token');
let pendingFiles = [];
let streaming = false;
// crypto.randomUUID работает только в secure context (HTTPS / localhost),
// под голым HTTP по IP его нет — используем fallback
function genUUID() {
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') return crypto.randomUUID();
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
const r = Math.random() * 16 | 0;
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
}
// Session continuity: reuse last tabId from localStorage
// New session only via "Новый чат" button or ?tab=UUID
const urlTab = new URLSearchParams(window.location.search).get('tab');
let tabId = urlTab || localStorage.getItem('tabId') || crypto.randomUUID();
let tabId = urlTab || localStorage.getItem('tabId') || genUUID();
localStorage.setItem('tabId', tabId);
function checkAdmin() {
@@ -470,7 +479,7 @@ async function sendMessage() {
// --- Session ---
async function resetSession() {
// Create new tab = new session, old one stays in DB
tabId = crypto.randomUUID();
tabId = genUUID();
localStorage.setItem('tabId', tabId);
msgs.innerHTML = '';
addMsg('system', 'Новый диалог начат.');