From f05a91e95dc23a7b568c91b9060e6458e0a1a335 Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Thu, 17 Jun 2021 23:55:19 -0400 Subject: [PATCH] Better checks for chat messages to appear - Re-click on a chat item if one click didn't work for some reason - Wait long for a single message to appear, but stop early when found - After single message found, wait less for more changes --- puppet/src/contentscript.js | 2 +- puppet/src/puppet.js | 37 ++++++++++++++++++++++++++++--------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/puppet/src/contentscript.js b/puppet/src/contentscript.js index 58eb2d7..428da3b 100644 --- a/puppet/src/contentscript.js +++ b/puppet/src/contentscript.js @@ -691,7 +691,7 @@ class MautrixController { waitForMessageListStability() { // Increase this if messages get missed on sync / chat change. // Decrease it if response times are too slow. - const delayMillis = 2000 + const delayMillis = 500 let myResolve const promise = new Promise(resolve => {myResolve = resolve}) diff --git a/puppet/src/puppet.js b/puppet/src/puppet.js index 6a3c954..076779c 100644 --- a/puppet/src/puppet.js +++ b/puppet/src/puppet.js @@ -539,13 +539,25 @@ export default class MessagesPuppeteer { this.log(hadMsgListObserver ? "Observer was already removed" : "Removed observer") await this._interactWithPage(async () => { - this.log(`Clicking chat list item`) - chatListItem.click() - this.log(`Waiting for chat header title to be "${chatName}"`) - await this.page.waitForFunction( - isCorrectChatVisible, - {polling: "mutation"}, - chatName) + let numTries = 3 + while (true) { + try { + this.log("Clicking chat list item") + chatListItem.click() + this.log(`Waiting for chat header title to be "${chatName}"`) + await this.page.waitForFunction( + isCorrectChatVisible, + {polling: "mutation", timeout: 1000}, + chatName) + break + } catch (e) { + if (--numTries == 0) { + throw e + } else { + this.log("Clicking chat list item didn't work...try again") + } + } + } // Always show the chat details sidebar, as this makes life easier this.log("Waiting for detail area to be auto-hidden upon entering chat") @@ -560,8 +572,15 @@ export default class MessagesPuppeteer { await this.page.waitForSelector("#_chat_detail_area > .mdRGT02Info") }) - this.log("Waiting for chat to stabilize") - await this.page.evaluate(() => window.__mautrixController.waitForMessageListStability()) + this.log("Waiting for any item to appear in chat") + try { + await this.page.waitForSelector("#_chat_room_msg_list div", {timeout: 2000}) + + this.log("Waiting for chat to stabilize") + await this.page.evaluate(() => window.__mautrixController.waitForMessageListStability()) + } catch (e) { + this.log("No messages in chat found. Maybe no messages were ever sent yet?") + } if (hadMsgListObserver) { this.log("Restoring msg list observer")