From 0d154c826e3658aa7062d19227619771eab31731 Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Mon, 14 Jun 2021 01:47:20 -0400 Subject: [PATCH] Fix outbound message sending The message text input field in LINE chats doesn't play nice with Puppeteer's "type" function for live-typing text... It's a div instead of a text input, and uses innerText instead of value. Setting its innerText directly seems to work best, so just use that. --- puppet/src/puppet.js | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/puppet/src/puppet.js b/puppet/src/puppet.js index 44a3f03..70f0c9b 100644 --- a/puppet/src/puppet.js +++ b/puppet/src/puppet.js @@ -128,6 +128,18 @@ export default class MessagesPuppeteer { await this.page.addScriptTag({ path: "./src/contentscript.js", type: "module" }) } + /** + * Set the contents of a text input field to the given text. + * Works by triple-clicking the input field to select all existing text, to replace it on type. + * + * @param {ElementHandle} inputElement - The input element to type into. + * @param {string} text - The text to input. + */ + async _enterText(inputElement, text) { + await inputElement.click({clickCount: 3}) + await inputElement.type(text) + } + /** * Wait for the session to be logged in and monitor changes while it's not. */ @@ -557,8 +569,11 @@ export default class MessagesPuppeteer { () => window.__mautrixController.promiseOwnMessage(5000, "time")) const input = await this.page.$("#_chat_room_input") + // Live-typing in the field can have its text mismatch what was requested!! + // Probably because the input element is a div instead of a real text input...ugh! + // Setting its innerText directly works fine though... await input.click() - await input.type(text) + await input.evaluate((e, text) => e.innerText = text, text) await input.press("Enter") return await this._waitForSentMessage(chatID) @@ -724,18 +739,8 @@ export default class MessagesPuppeteer { async _sendEmailCredentials() { this.log("Inputting login credentials") - - // Triple-click input fields to select all existing text and replace it on type - let input - - input = await this.page.$("#line_login_email") - await input.click({clickCount: 3}) - await input.type(this.login_email) - - input = await this.page.$("#line_login_pwd") - await input.click({clickCount: 3}) - await input.type(this.login_password) - + await this._enterText(await this.page.$("#line_login_email"), this.login_email) + await this._enterText(await this.page.$("#line_login_pwd"), this.login_password) await this.page.click("button#login_btn") }