// Injetado na aba via chrome.scripting.executeScript. Roda isolado, sem imports: // recebe o texto já gerado e o escreve no campo que abriu o menu de contexto. function insertIntoFocused(text) { const el = document.activeElement; if (!el) return false; const tag = el.tagName; const isInput = tag === "INPUT" || tag === "TEXTAREA"; if (!isInput && !el.isContentEditable) return false; el.focus(); // Caminho preferido: execCommand emite os eventos de input nativos, então // campos controlados por React/Vue/Angular enxergam a mudança. Uma atribuição // direta em .value seria silenciosamente descartada por esses frameworks. if (document.execCommand("insertText", false, text)) return true; // Fallback para quando execCommand é bloqueado ou não suportado. if (isInput) { const start = el.selectionStart ?? el.value.length; const end = el.selectionEnd ?? el.value.length; // Usa o setter nativo do prototype: React sobrescreve o setter da instância // para rastrear o estado, e escrever por ele não notificaria o framework. const proto = tag === "TEXTAREA" ? HTMLTextAreaElement.prototype : HTMLInputElement.prototype; const setter = Object.getOwnPropertyDescriptor(proto, "value").set; setter.call(el, el.value.slice(0, start) + text + el.value.slice(end)); const pos = start + text.length; el.setSelectionRange(pos, pos); } else { const sel = window.getSelection(); if (sel && sel.rangeCount > 0) { const range = sel.getRangeAt(0); range.deleteContents(); range.insertNode(document.createTextNode(text)); sel.collapseToEnd(); } else { el.textContent += text; } } el.dispatchEvent(new Event("input", { bubbles: true })); el.dispatchEvent(new Event("change", { bubbles: true })); return true; }