const DEV = (() => {
try {
return window.MOBUX_DEV === true;
} catch (_) {
return false;
}
})();
const SESSION_ID = (() => {
try {
if (window.crypto && window.crypto.randomUUID) {
return window.crypto.randomUUID().slice(0, 8);
}
} catch (_) {
}
return Math.random().toString(36).slice(2, 10);
})();
const OVERLAY_KEY = 'mobux:telemetry';
let overlayEl = null;
function overlayEnabled() {
if (!DEV) return false;
try {
if (new URLSearchParams(window.location.search).get('telemetry') === '1') {
return true;
}
} catch (_) {
}
try {
return localStorage.getItem(OVERLAY_KEY) === '1';
} catch (_) {
return false;
}
}
function ensureOverlay() {
if (overlayEl || !document.body) return overlayEl;
overlayEl = document.createElement('div');
overlayEl.id = 'mobux-telemetry-overlay';
Object.assign(overlayEl.style, {
position: 'fixed',
bottom: '0',
left: '0',
right: '0',
maxHeight: '40vh',
overflowY: 'auto',
margin: '0',
padding: '4px 6px',
font: '11px/1.35 monospace',
color: '#9fe',
background: 'rgba(0,0,0,0.78)',
zIndex: '2147483647',
whiteSpace: 'pre-wrap',
wordBreak: 'break-word',
pointerEvents: 'none',
});
document.body.appendChild(overlayEl);
return overlayEl;
}
function appendOverlay(line) {
const el = ensureOverlay();
if (!el) return;
const row = document.createElement('div');
row.textContent = line;
el.appendChild(row);
while (el.childNodes.length > 200) el.removeChild(el.firstChild);
el.scrollTop = el.scrollHeight;
}
function format(event, data) {
let payload = '';
if (data !== undefined) {
if (typeof data === 'string') {
payload = data;
} else {
try {
payload = JSON.stringify(data);
} catch (_) {
payload = String(data);
}
}
}
const ts = new Date().toISOString().slice(11, 23); return `${ts} [${SESSION_ID}] ${event}${payload ? ' ' + payload : ''}`;
}
function log(event, data) {
if (!DEV) return; const line = format(event, data);
try {
fetch('/api/telemetry', {
method: 'POST',
headers: { 'Content-Type': 'text/plain' },
body: line,
credentials: 'same-origin',
keepalive: true,
}).catch(() => {});
} catch (_) {
}
if (overlayEnabled()) {
try {
appendOverlay(line);
} catch (_) {
}
}
}
function overlay(on) {
if (!DEV) return false;
const next = on === undefined ? !overlayEnabled() : !!on;
try {
localStorage.setItem(OVERLAY_KEY, next ? '1' : '0');
} catch (_) {
}
if (next) {
ensureOverlay();
} else if (overlayEl) {
overlayEl.remove();
overlayEl = null;
}
return next;
}
const telemetry = { log, overlay, enabled: DEV, sessionId: SESSION_ID };
if (DEV) {
try {
window.mobuxTelemetry = telemetry;
} catch (_) {
}
if (overlayEnabled()) {
if (document.body) {
ensureOverlay();
} else {
document.addEventListener('DOMContentLoaded', () => ensureOverlay(), { once: true });
}
}
}
export default telemetry;
export { log, overlay };