/** * Maestri Remote — Live OTA Bootstrapper * Executa assets locais em cache e atualiza silenciosamente em background do Pod K3s. */ const REMOTE_SERVER = 'https://remote.winicomw.ddnsfree.com'; (function initLiveBootstrapper() { // 1. Atualiza o DOM com o body mais recente se houver em cache try { const cachedBody = localStorage.getItem('maestri_ota_body'); if (cachedBody && cachedBody.length > 200) { document.body.innerHTML = cachedBody; } } catch (e) {} // 2. Executa o código da aplicação const cachedJs = localStorage.getItem('maestri_ota_js'); if (cachedJs && cachedJs.length > 500) { console.log('[OTA Bootstrapper] Carregando bundle atualizado do armazenamento local...'); try { const script = document.createElement('script'); script.textContent = cachedJs; document.body.appendChild(script); } catch (err) { console.error('[OTA Bootstrapper] Falha ao executar bundle em cache, usando fallback:', err); loadFallbackScript(); } } else { loadFallbackScript(); } function loadFallbackScript() { const script = document.createElement('script'); const isApp = window.location.protocol === 'capacitor:' || window.location.protocol === 'file:' || !window.location.host || window.location.host === 'localhost'; script.src = isApp ? `${REMOTE_SERVER}/app.js?v=${Date.now()}` : `/app.js?v=${Date.now()}`; document.body.appendChild(script); } // 3. Em background: Consulta se há atualização no servidor setTimeout(() => checkForOtaUpdate(false), 1500); setInterval(() => checkForOtaUpdate(false), 20000); async function checkForOtaUpdate(force = false) { try { // Usa querystring com timestamp para evitar preflight OPTIONS e contornar CORS const res = await fetch(`${REMOTE_SERVER}/api/version?src=ota&t=${Date.now()}`); if (!res.ok) return false; const manifest = await res.json(); const localBuild = localStorage.getItem('maestri_ota_build_id'); if (force || (manifest.buildId && manifest.buildId !== localBuild)) { console.log(`[OTA Bootstrapper] Nova versão detectada: ${manifest.version} (${manifest.buildId}). Baixando...`); // Baixa os assets atualizados do servidor (JS, CSS e HTML) com timestamp anti-cache const [jsRes, cssRes, htmlRes] = await Promise.all([ fetch(`${REMOTE_SERVER}/app.js?b=${manifest.buildId}&t=${Date.now()}`), fetch(`${REMOTE_SERVER}/style.css?b=${manifest.buildId}&t=${Date.now()}`), fetch(`${REMOTE_SERVER}/index.html?b=${manifest.buildId}&t=${Date.now()}`) ]); if (jsRes.ok && cssRes.ok) { const newJs = await jsRes.text(); const newCss = await cssRes.text(); if (htmlRes.ok) { const fullHtml = await htmlRes.text(); const bodyMatch = fullHtml.match(/
]*>([\s\S]*)<\/body>/i); if (bodyMatch && bodyMatch[1]) { const cleanBody = bodyMatch[1].replace(/