Changesets · Branches · Tags · Compare
| Changeset | 539b23dafbe787cc9027964c47dd3209e1684c5183bf97d45a30e0aed37feb3f |
|---|---|
| Author | KikoCis <kikocisneros@gmail.com> · 2026-07-17 10:44 +02:00 |
| Committer | KikoCis <kikocisneros@gmail.com> · 2026-07-17 10:44 +02:00 |
| Parents | 3297454a05f6 |
1 file changed
added core/telemetry.js +63 −0
| @@ -0,0 +1,63 @@ | ||
| 1 | +// Buzón opt-in de errores/feedback. Apagado por defecto — la promesa de | |
| 2 | +// «nada sale de tu máquina» sigue siendo cierta salvo que el usuario decida | |
| 3 | +// lo contrario en Ajustes. Cuando está activo: | |
| 4 | +// - errores no capturados (window.onerror / unhandledrejection) y los | |
| 5 | +// puntos concretos del código que llaman a reportError() se mandan a | |
| 6 | +// /proxy/report (mismo origen, vía el proxy compartido de Elffuss) | |
| 7 | +// - sendFeedback() manda texto libre que el usuario escriba a propósito | |
| 8 | +// Nunca se manda código ni contenido del proyecto — solo el mensaje técnico, | |
| 9 | +// la pila, la URL y el user-agent. Sin cola ni reintentos: si el envío | |
| 10 | +// falla, se descarta sin más (evita que un fallo de red se convierta en | |
| 11 | +// almacenamiento sin límite). | |
| 12 | +let appName = 'elffuss'; | |
| 13 | +let enabled = false; | |
| 14 | +let hooked = false; | |
| 15 | + | |
| 16 | +const storageKey = () => 'elffuss.telemetry.' + appName; | |
| 17 | + | |
| 18 | +export function isEnabled() { return enabled; } | |
| 19 | + | |
| 20 | +export function setEnabled(v) { | |
| 21 | + enabled = !!v; | |
| 22 | + try { localStorage.setItem(storageKey(), enabled ? '1' : '0'); } catch { /* localStorage lleno/bloqueado */ } | |
| 23 | + if (enabled) hookGlobalErrors(); | |
| 24 | +} | |
| 25 | + | |
| 26 | +async function post(kind, message, opts = {}) { | |
| 27 | + if (!enabled) return; | |
| 28 | + try { | |
| 29 | + await fetch('/proxy/report', { | |
| 30 | + method: 'POST', | |
| 31 | + headers: { 'Content-Type': 'application/json' }, | |
| 32 | + body: JSON.stringify({ | |
| 33 | + app: appName, kind, message: String(message ?? '').slice(0, 2000), | |
| 34 | + stack: String(opts.stack || '').slice(0, 4000), | |
| 35 | + url: location.href, userAgent: navigator.userAgent, | |
| 36 | + extra: String(opts.extra || '').slice(0, 2000), | |
| 37 | + }), | |
| 38 | + }); | |
| 39 | + } catch { /* sin conexión o proxy caído: se descarta, no hay reintento */ } | |
| 40 | +} | |
| 41 | + | |
| 42 | +export function reportError(message, opts = {}) { return post('error', message, opts); } | |
| 43 | +export function sendFeedback(text) { return post('feedback', text); } | |
| 44 | + | |
| 45 | +function hookGlobalErrors() { | |
| 46 | + if (hooked) return; | |
| 47 | + hooked = true; | |
| 48 | + window.addEventListener('error', e => { | |
| 49 | + reportError(e.message || String(e.error) || 'error', { stack: e.error?.stack || '' }); | |
| 50 | + }); | |
| 51 | + window.addEventListener('unhandledrejection', e => { | |
| 52 | + const r = e.reason; | |
| 53 | + reportError('unhandledrejection: ' + (r?.message || r), { stack: r?.stack || '' }); | |
| 54 | + }); | |
| 55 | +} | |
| 56 | + | |
| 57 | +// Llamar UNA vez al arrancar, con el nombre de la app (para distinguir | |
| 58 | +// elffuss-code de elffuss-claw en los informes) — lee la preferencia guardada. | |
| 59 | +export function init(name) { | |
| 60 | + appName = name || appName; | |
| 61 | + try { enabled = localStorage.getItem(storageKey()) === '1'; } catch { enabled = false; } | |
| 62 | + if (enabled) hookGlobalErrors(); | |
| 63 | +} |