/* ── Busca automática CNPJ via BrasilAPI ── */ var _lastCnpjSearch = ""; function gdFillInput(el, value) { if (!el || !value) return; var nativeSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value").set; nativeSetter.call(el, value); el.dispatchEvent(new Event("input", {bubbles:true})); el.dispatchEvent(new Event("change", {bubbles:true})); } function gdFillByName(name, value) { if (!value) return; // Tenta via _gdSetValue (React Hook Form) if (window._gdSetValue) { try { window._gdSetValue(name, value, {shouldValidate:true,shouldDirty:true}); return; } catch(e){} } // Fallback: encontra o input pelo name attribute var el = document.querySelector('input[name="'+name+'"]') || document.querySelector('textarea[name="'+name+'"]'); if (el) gdFillInput(el, value); } function runCnpjSearch() { if (!location.href.includes("/agendar")) return; var el = document.querySelector('input[placeholder="00.000.000/0000-00"]'); if (!el) return; var raw = el.value.replace(/[^a-zA-Z0-9]/g,"").toUpperCase(); if (raw.length !== 14 || raw === _lastCnpjSearch) return; _lastCnpjSearch = raw; fetch("https://brasilapi.com.br/api/cnpj/v1/" + raw) .then(function(r){ return r.ok ? r.json() : null; }) .then(function(d){ if (!d) { _lastCnpjSearch = ""; return; } gdFillByName("companyName", d.razao_social || d.nome_fantasia || ""); gdFillByName("email", d.email || ""); gdFillByName("phone", (d.ddd_telefone_1||"").replace(/\D/g,"")); gdFillByName("zip", (d.cep||"").replace(/\D/g,"")); gdFillByName("address", (d.logradouro||"") + (d.numero ? ", "+d.numero : "")); gdFillByName("neighborhood",d.bairro || ""); gdFillByName("city", d.municipio || ""); gdFillByName("state", d.uf || ""); }) .catch(function(){ _lastCnpjSearch = ""; }); } setInterval(runCnpjSearch, 600);