From 69028c6edd8d55973121689acb7eb07168d56d65 Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Fri, 2 Jul 2021 13:58:36 -0400 Subject: [PATCH] Fix problems with outgoing messages 1. Don't set the "own message" promise to null on resolve, as other places may still be awaiting on it afterwards 2. Retry pressing "Enter" to send a message if it doesn't work on the first try, which can happen for some reason --- puppet/src/contentscript.js | 16 +++++++++++----- puppet/src/puppet.js | 13 ++++++++++++- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/puppet/src/contentscript.js b/puppet/src/contentscript.js index 66ec9e4..207c7cf 100644 --- a/puppet/src/contentscript.js +++ b/puppet/src/contentscript.js @@ -95,6 +95,7 @@ class MautrixController { this.pinAppearObserver = null this.ownID = null + this.ownMsgPromise = Promise.resolve(-1) this._promiseOwnMsgReset() } @@ -474,13 +475,20 @@ class MautrixController { }, timeoutLimitMillis) } + /** + * Check if we're waiting for a Matrix-sent message to resolve. + */ + _isWaitingForOwnMessage() { + return !!this.promiseOwnMsgResolve + } + /** * Wait for a user-sent message to finish getting sent. * * @return {Promise} - The ID of the sent message. */ async waitForOwnMessage() { - return this.ownMsgPromise ? await this.ownMsgPromise : -1 + return await this.ownMsgPromise } /** @@ -867,7 +875,7 @@ class MautrixController { addChatListObserver() { this.removeChatListObserver() this.chatListObserver = new MutationObserver(async (mutations) => { - if (this.ownMsgPromise) { + if (this._isWaitingForOwnMessage()) { // Wait for pending sent messages to be resolved before responding to mutations try { await this.ownMsgPromise @@ -1213,8 +1221,7 @@ class MautrixController { } _observeOwnMessage(ownMsg) { - if (!this.ownMsgPromise) { - // Not waiting for a pending sent message + if (!this._isWaitingForOwnMessage()) { return null } @@ -1299,7 +1306,6 @@ class MautrixController { } _promiseOwnMsgReset() { - this.ownMsgPromise = null this.promiseOwnMsgSuccessSelector = null this.promiseOwnMsgFailureSelector = null this.promiseOwnMsgResolve = null diff --git a/puppet/src/puppet.js b/puppet/src/puppet.js index 71bca9d..c382e69 100644 --- a/puppet/src/puppet.js +++ b/puppet/src/puppet.js @@ -790,7 +790,18 @@ export default class MessagesPuppeteer { // Setting its innerText directly works fine though... await input.click() await input.evaluate((e, text) => e.innerText = text, text) - await input.press("Enter") + while (true) { + await input.press("Enter") + try { + await this.page.waitForFunction( + e => e.innerText == "", + {timeout: 500}, + input) + break + } catch (e) { + this.error(`Failed to press Enter when sending message, try again (${e})`) + } + } }) return await this._waitForSentMessage(chatID)