/** * parkmeraki-tracker-v5.js · ParkMeraki * * v5 — JS simplificado: zero chamadas externas de geo. * IP, cidade e país são resolvidos 100% pelo PHP server-side. * Sem CORS, sem ModSecurity, sem APIs externas. * * JS só envia: token, pagina, DataHora, Nome, Telefone, Email, Data. * PHP resolve: IP real, cidade, região, país via curl + ipwho.is. */ (function () { if (window.__pmTrackerAtivo) return; window.__pmTrackerAtivo = true; var TRACK_URL = 'https://parkmerakiindaiatuba.com.br/track_form.php'; var TOKEN = 'pm_track_2024'; var COOKIE_NOME = 'pm_lead'; var CAMPOS = { Nome: ['pm-nome'], Telefone: ['pm-tel'], Email: ['pm-email'], Data: ['pm-data'] }; var dados = {}; var ultimoEnviado = null; var autofillAtivo = false; var intervalPrincipal = null; var formularioEnviado = false; /* ── Horário de Brasília ── */ function dataHoraBrasil() { var partes = new Intl.DateTimeFormat('pt-BR', { timeZone: 'America/Sao_Paulo', day: '2-digit', month: '2-digit', year: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false }).formatToParts(new Date()); var p = {}; partes.forEach(function (x) { p[x.type] = x.value; }); return p.day + '/' + p.month + '/' + p.year + ' ' + p.hour + ':' + p.minute + ':' + p.second; } /* ── Utilitários ── */ function getEl(id) { var el = document.getElementById(id); if (el) return el; var host = document.getElementById('pmk-host-v1'); if (host && host.shadowRoot) return host.shadowRoot.getElementById(id); return null; } var timerAutofill = null; function ativarJanelaAutofill() { autofillAtivo = true; clearTimeout(timerAutofill); timerAutofill = setTimeout(function () { autofillAtivo = false; }, 100); } function lerCampos() { Object.keys(CAMPOS).forEach(function (campo) { CAMPOS[campo].forEach(function (id) { var el = getEl(id); if (!el) return; var v = el.value.trim(); if (v || !autofillAtivo) dados[campo] = v; }); }); } /* ── Envio — só dispara quando conteúdo muda ── */ function enviar() { if (formularioEnviado) return; lerCampos(); var n = dados.Nome || '', t = dados.Telefone || '', e = dados.Email || '', d = dados.Data || ''; if (!n && !t && !e && !d) return; var chave = n + '|' + t + '|' + e + '|' + d; if (chave === ultimoEnviado) return; ultimoEnviado = chave; var body = new URLSearchParams({ token: TOKEN, pagina: (document.referrer || window.location.href), // referrer = URL da página-pai (fora do iframe); fallback = URL do próprio iframe DataHora: dataHoraBrasil(), Nome: n, Telefone: t, Email: e, Data: d }); console.log('[PMTracker] Enviando:', dataHoraBrasil(), { Nome: n, Telefone: t, Email: e, Data: d }); fetch(TRACK_URL, { method: 'POST', body: body }) .catch(function (err) { console.warn('[PMTracker] Erro:', err); }); } /* ── Autofill CSS ── */ function lerAutofillCSS() { var encontrou = false; try { document.querySelectorAll('input:-webkit-autofill').forEach(function (el) { Object.keys(CAMPOS).forEach(function (campo) { if (CAMPOS[campo].indexOf(el.id) !== -1) { var v = el.value.trim(); if (v) { dados[campo] = v; encontrou = true; } } }); }); } catch (e) {} return encontrou; } function iniciarPollingAutofill() { var ciclos = 0; var poller = setInterval(function () { if (lerAutofillCSS()) { ativarJanelaAutofill(); enviar(); } if (++ciclos >= 100) clearInterval(poller); }, 300); } var style = document.createElement('style'); style.textContent = '@keyframes pm-af{from{opacity:1}to{opacity:1}}' + 'input:-webkit-autofill,input:-webkit-autofill:hover,' + 'input:-webkit-autofill:focus,input:-webkit-autofill:active{' + 'animation-name:pm-af!important;animation-duration:1ms!important}'; document.head.appendChild(style); var agendandoAutofill = false; document.addEventListener('animationstart', function (e) { if (e.animationName !== 'pm-af' || agendandoAutofill) return; agendandoAutofill = true; ativarJanelaAutofill(); [100, 300, 600, 1000].forEach(function (ms) { setTimeout(enviar, ms); }); setTimeout(function () { agendandoAutofill = false; }, 1500); }, true); /* ── Bind campos ── */ function bindCampo(el, campo) { if (el.__pmBound) return; el.__pmBound = true; ['input', 'change', 'keyup', 'paste'].forEach(function (ev) { el.addEventListener(ev, function () { dados[campo] = el.value.trim(); }, { passive: true }); }); el.addEventListener('blur', function () { dados[campo] = el.value.trim(); enviar(); }, { passive: true }); el.addEventListener('touchend', function () { setTimeout(function () { dados[campo] = el.value.trim(); enviar(); }, 300); }, { passive: true }); } /* ── Limpeza após envio ── */ function deletarCookie() { document.cookie = COOKIE_NOME + '=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; secure; samesite=Lax'; dados = {}; ultimoEnviado = null; } document.addEventListener('pm:enviado', function () { formularioEnviado = true; if (intervalPrincipal) { clearInterval(intervalPrincipal); intervalPrincipal = null; } [0, 300, 800, 1500].forEach(function (ms) { setTimeout(deletarCookie, ms); }); console.log('[PMTracker] Encerrado.'); }); /* ── Inicialização ── */ var tentativas = 0; function tentarInicializar() { tentativas++; var encontrado = false; Object.keys(CAMPOS).forEach(function (campo) { CAMPOS[campo].forEach(function (id) { var el = getEl(id); if (!el) return; encontrado = true; bindCampo(el, campo); }); }); if (!encontrado) { if (tentativas < 50) setTimeout(tentarInicializar, 200); else console.warn('[PMTracker] Formulário não encontrado após 10s.'); return; } iniciarPollingAutofill(); intervalPrincipal = setInterval(enviar, 500); console.log('[PMTracker] Pronto.'); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', tentarInicializar); } else { tentarInicializar(); } })();