From 478f7136d9d57dd66bd9a2599f1cef8283215040 Mon Sep 17 00:00:00 2001 From: Pedro Lopez Date: Sat, 11 Apr 2020 18:48:50 -0400 Subject: [PATCH] feat: takeover on conflict This adds funcitonality to takeover after a certain timeout if WhatsApp Web has been opened in another browser close #119 --- src/Client.js | 16 ++++++++++++++-- src/util/Constants.js | 2 ++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Client.js b/src/Client.js index 23b308c..b1e99b1 100644 --- a/src/Client.js +++ b/src/Client.js @@ -286,6 +286,17 @@ class Client extends EventEmitter { this.emit(Events.STATE_CHANGED, state); const ACCEPTED_STATES = [WAState.CONNECTED, WAState.OPENING, WAState.PAIRING, WAState.TIMEOUT]; + + if(this.options.takeoverOnConflict) { + ACCEPTED_STATES.push(WAState.CONFLICT); + + if(state === WAState.CONFLICT) { + setTimeout(() => { + this.pupPage.evaluate(() => window.Store.AppState.takeover()); + }, this.options.takeoverTimeoutMs); + } + } + if (!ACCEPTED_STATES.includes(state)) { /** * Emitted when the client has been disconnected @@ -298,9 +309,10 @@ class Client extends EventEmitter { }); await page.exposeFunction('onBatteryStateChangedEvent', (state) => { - const { battery, plugged } = state; + if(battery === undefined) return; + /** * Emitted when the battery percentage for the attached device changes * @event Client#change_battery @@ -415,7 +427,7 @@ class Client extends EventEmitter { msg = await window.WWebJS.sendMessage(chat, message, options, sendSeen); } return msg.serialize(); - }, chatId, content, internalOptions, sendSeen); + }, chatId, content, internalOptions, sendSeen).catch(error => { throw error; }); return new Message(this, newMessage); } diff --git a/src/util/Constants.js b/src/util/Constants.js index 48434cc..1b92e73 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -12,6 +12,8 @@ exports.DefaultOptions = { qrTimeoutMs: 45000, qrRefreshIntervalMs: 20000, authTimeoutMs: 45000, + takeoverOnConflict: false, + takeoverTimeoutMs: 0 }; /**