UI
This commit is contained in:
@@ -129,7 +129,19 @@
|
||||
|
||||
const $ = (id) => document.getElementById(id);
|
||||
const $$ = (sel, root = document) => Array.from(root.querySelectorAll(sel));
|
||||
const api = (path) => window.BT ? BT.api(path) : fetch(path).then((r) => r.json());
|
||||
const api = async (path, options = {}) => {
|
||||
if (window.BT) return BT.api(path, options);
|
||||
const opts = {...options};
|
||||
if (opts.body !== undefined && !(opts.body instanceof FormData)) {
|
||||
opts.headers = {"Content-Type": "application/json", ...(opts.headers || {})};
|
||||
}
|
||||
const response = await fetch(path, opts);
|
||||
const text = await response.text();
|
||||
let payload = {};
|
||||
try { payload = text ? JSON.parse(text) : {}; } catch (_) { payload = {raw: text}; }
|
||||
if (!response.ok) throw new Error(payload.error || payload.detail || `HTTP ${response.status}`);
|
||||
return payload;
|
||||
};
|
||||
const esc = (value) => window.BT ? BT.escape(value) : String(value ?? "").replace(/[&<>"']/g, (ch) => ({"&": "&", "<": "<", ">": ">", '"': """, "'": "'"}[ch]));
|
||||
const uid = (prefix) => `${prefix}-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user