diff --git a/index.d.ts b/index.d.ts index 37d0952..e6488b3 100644 --- a/index.d.ts +++ b/index.d.ts @@ -294,6 +294,9 @@ declare namespace WAWebJS { /** Timeout for qr code selector in puppeteer * @default 45000 */ qrTimeoutMs?: number, + /** How many times should the qrcode be refreshed before giving up + * @default 0 (disabled) */ + qrMaxRetries?: number, /** Restart client with a new session (i.e. use null 'session' var) if authentication fails * @default false */ restartOnAuthFail?: boolean diff --git a/src/Client.js b/src/Client.js index 5123c2a..2b7ab5b 100644 --- a/src/Client.js +++ b/src/Client.js @@ -20,6 +20,7 @@ const { ClientInfo, Message, MessageMedia, Contact, Location, GroupNotification * @param {object} options.puppeteer - Puppeteer launch options. View docs here: https://github.com/puppeteer/puppeteer/ * @param {number} options.qrRefreshIntervalMs - Refresh interval for qr code (how much time to wait before checking if the qr code has changed) * @param {number} options.qrTimeoutMs - Timeout for qr code selector in puppeteer + * @param {number} options.qrMaxRetries - How many times should the qrcode be refreshed before giving up * @param {string} options.restartOnAuthFail - Restart client with a new session (i.e. use null 'session' var) if authentication fails * @param {object} options.session - Whatsapp session to restore. If not set, will start a new session * @param {string} options.session.WABrowserId @@ -127,6 +128,8 @@ class Client extends EventEmitter { } } else { + let qrRetries = 0; + const getQrCode = async () => { // Check if retry button is present var QR_RETRY_SELECTOR = 'div[data-ref] > span > button'; @@ -147,6 +150,14 @@ class Client extends EventEmitter { * @param {string} qr QR Code */ this.emit(Events.QR_RECEIVED, qr); + + if (this.options.qrMaxRetries > 0) { + qrRetries++; + if (qrRetries > this.options.qrMaxRetries) { + this.emit(Events.DISCONNECTED, 'Max qrcode retries reached'); + await this.destroy(); + } + } }; getQrCode(); this._qrRefreshInterval = setInterval(getQrCode, this.options.qrRefreshIntervalMs); diff --git a/src/util/Constants.js b/src/util/Constants.js index 04abdeb..2ff4a6f 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -11,6 +11,7 @@ exports.DefaultOptions = { qrTimeoutMs: 45000, qrRefreshIntervalMs: 20000, authTimeoutMs: 45000, + qrMaxRetries: 0, takeoverOnConflict: false, takeoverTimeoutMs: 0, userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',