From 1856c79135a5d4b30a983cbb5e88db414eede19c Mon Sep 17 00:00:00 2001 From: Demetrius Albuquerque Date: Thu, 17 Apr 2025 18:55:27 -0300 Subject: [PATCH] Update shrekSendScript.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refatoração(enviarScript): enviar mensagens simulando tecla Enter - Substitui clique no botão por dispatch de KeyboardEvent (keydown e keyup de “Enter”) - Dispara evento ‘input’ em vez de ‘change’ para melhor compatibilidade com o textarea - Mantém lógica de delay entre linhas - Simplifica loop para iteração baseada em índice --- shrekSendScript.js | 86 +++++++++++++++++++++++++++++++++------------- 1 file changed, 62 insertions(+), 24 deletions(-) diff --git a/shrekSendScript.js b/shrekSendScript.js index ff005de..315abfa 100644 --- a/shrekSendScript.js +++ b/shrekSendScript.js @@ -1,28 +1,57 @@ -async function enviarScript(scriptText){ - const lines = scriptText.split(/[\n\t]+/).map(line => line.trim()).filter(line => line); - main = document.querySelector("#main"), - textarea = main.querySelector(`div[contenteditable="true"]`) - - if(!textarea) throw new Error("Não há uma conversa aberta") - - for(const line of lines){ - console.log(line) - - textarea.focus(); - document.execCommand('insertText', false, line); - textarea.dispatchEvent(new Event('change', {bubbles: true})); - - setTimeout(() => { - (main.querySelector(`[data-testid="send"]`) || main.querySelector(`[data-icon="send"]`)).click(); - }, 100); - - if(lines.indexOf(line) !== lines.length - 1) await new Promise(resolve => setTimeout(resolve, 250)); - } - - return lines.length; +async function enviarScript(scriptText) { + const lines = scriptText + .split(/[\n\t]+/) + .map(line => line.trim()) + .filter(line => line); + + const main = document.querySelector("#main"); + const textarea = main?.querySelector(`div[contenteditable="true"]`); + if (!textarea) throw new Error("Não há uma conversa aberta"); + + for (let i = 0; i < lines.length; i++) { + const line = lines[i]; + console.log(line); + + // insere o texto + textarea.focus(); + document.execCommand("insertText", false, line); + // alguns apps reagem melhor a 'input' do que a 'change' + textarea.dispatchEvent(new Event("input", { bubbles: true })); + + // simula “Enter” + const enterDown = new KeyboardEvent("keydown", { + bubbles: true, + cancelable: true, + key: "Enter", + code: "Enter", + which: 13, + keyCode: 13, + }); + textarea.dispatchEvent(enterDown); + + const enterUp = new KeyboardEvent("keyup", { + bubbles: true, + cancelable: true, + key: "Enter", + code: "Enter", + which: 13, + keyCode: 13, + }); + textarea.dispatchEvent(enterUp); + + // intervalo entre linhas + if (i < lines.length - 1) { + await new Promise(res => setTimeout(res, 250)); + } + } + + return lines.length; } -enviarScript(` + +const minhasLinhas = ` +Olá, tudo bem? + SHREK Written by @@ -3700,4 +3729,13 @@ black) Oh, that's funny. Oh. Oh. I can't breathe. I can't breathe. THE END -`).then(e => console.log(`Código finalizado, ${e} mensagens enviadas`)).catch(console.error) +`; + +(async () => { + try { + const total = await enviarScript(minhasLinhas); + console.log(`✅ Enviou ${total} mensagens com sucesso!`); + } catch (err) { + console.error("❌ Falhou ao enviar:", err); + } +})();