From 65838153012525c23c08840fc5df8df58936308e Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Sat, 5 Jun 2021 23:51:12 -0400 Subject: [PATCH] Dismiss error dialog for getting disconnected If the connection to LINE is lost, when it comes back, and error dialog appears. Detect that dialog and click it automatically. The same detection works for any error dialog. --- puppet/src/contentscript.js | 40 ++++++++++++++++++------------------- puppet/src/puppet.js | 10 ---------- 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/puppet/src/contentscript.js b/puppet/src/contentscript.js index dbee656..faf4a73 100644 --- a/puppet/src/contentscript.js +++ b/puppet/src/contentscript.js @@ -90,7 +90,6 @@ class MautrixController { this.qrAppearObserver = null this.emailAppearObserver = null this.pinAppearObserver = null - this.expiryObserver = null this.ownID = null this.ownMsgPromise = Promise.resolve(-1) @@ -968,26 +967,25 @@ class MautrixController { } } - addExpiryObserver(element) { - this.removeExpiryObserver() - const button = element.querySelector("dialog button") - this.expiryObserver = new MutationObserver(changes => { - if (changes.length == 1 && !changes[0].target.classList.contains("MdNonDisp")) { - window.__mautrixExpiry(button) - } - }) - this.expiryObserver.observe(element, { - attributes: true, - attributeFilter: ["class"], - }) - } - - removeExpiryObserver() { - if (this.expiryObserver !== null) { - this.expiryObserver.disconnect() - this.expiryObserver = null - } - } } window.__mautrixController = new MautrixController() + +/** + * Watch for an error dialog / PIN expiry dialog to appear, and click its "OK" button. + * Must watch for both its parent appearing & it being added to its parent in the first place. + */ +const layer = document.querySelector("#layer_contents") +new MutationObserver(() => { + if (!layer.classList.contains("MdNonDisp")) { + const button = layer.querySelector("dialog button") + if (button) { + console.log("Something expired, clicking OK button to continue") + button.click() + } + } +}).observe(layer, { + attributes: true, + attributeFilter: ["class"], + childList: true, +}) diff --git a/puppet/src/puppet.js b/puppet/src/puppet.js index aa8baeb..0f07af9 100644 --- a/puppet/src/puppet.js +++ b/puppet/src/puppet.js @@ -97,7 +97,6 @@ export default class MessagesPuppeteer { await this.page.exposeFunction("__mautrixReceiveQR", this._receiveQRChange.bind(this)) await this.page.exposeFunction("__mautrixSendEmailCredentials", this._sendEmailCredentials.bind(this)) await this.page.exposeFunction("__mautrixReceivePIN", this._receivePIN.bind(this)) - await this.page.exposeFunction("__mautrixExpiry", this._receiveExpiry.bind(this)) await this.page.exposeFunction("__mautrixReceiveMessageID", id => this.sentMessageIDs.add(id)) await this.page.exposeFunction("__mautrixReceiveChanges", @@ -183,8 +182,6 @@ export default class MessagesPuppeteer { await this.page.evaluate( element => window.__mautrixController.addPINAppearObserver(element), loginContentArea) - await this.page.$eval("#layer_contents", - element => window.__mautrixController.addExpiryObserver(element)) this.log("Waiting for login response") let doneWaiting = false @@ -226,7 +223,6 @@ export default class MessagesPuppeteer { await this.page.evaluate(() => window.__mautrixController.removeQRAppearObserver()) await this.page.evaluate(() => window.__mautrixController.removeEmailAppearObserver()) await this.page.evaluate(() => window.__mautrixController.removePINAppearObserver()) - await this.page.evaluate(() => window.__mautrixController.removeExpiryObserver()) delete this.login_email delete this.login_password @@ -739,10 +735,4 @@ export default class MessagesPuppeteer { this.log("No client connected, not sending failure reason") } } - - async _receiveExpiry(button) { - this.log("Something expired, clicking OK button to continue") - this.page.click(button).catch(err => - this.error("Failed to dismiss expiry dialog:", err)) - } }