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:
+11
-2
@@ -167,10 +167,19 @@ const API = '';
|
|||||||
let token = localStorage.getItem('token');
|
let token = localStorage.getItem('token');
|
||||||
let pendingFiles = [];
|
let pendingFiles = [];
|
||||||
let streaming = false;
|
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
|
// Session continuity: reuse last tabId from localStorage
|
||||||
// New session only via "Новый чат" button or ?tab=UUID
|
// New session only via "Новый чат" button or ?tab=UUID
|
||||||
const urlTab = new URLSearchParams(window.location.search).get('tab');
|
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);
|
localStorage.setItem('tabId', tabId);
|
||||||
|
|
||||||
function checkAdmin() {
|
function checkAdmin() {
|
||||||
@@ -470,7 +479,7 @@ async function sendMessage() {
|
|||||||
// --- Session ---
|
// --- Session ---
|
||||||
async function resetSession() {
|
async function resetSession() {
|
||||||
// Create new tab = new session, old one stays in DB
|
// Create new tab = new session, old one stays in DB
|
||||||
tabId = crypto.randomUUID();
|
tabId = genUUID();
|
||||||
localStorage.setItem('tabId', tabId);
|
localStorage.setItem('tabId', tabId);
|
||||||
msgs.innerHTML = '';
|
msgs.innerHTML = '';
|
||||||
addMsg('system', 'Новый диалог начат.');
|
addMsg('system', 'Новый диалог начат.');
|
||||||
|
|||||||
Reference in New Issue
Block a user