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)