diff --git a/src/Client.js b/src/Client.js index c4df1bfaaa..78d6276bad 100644 --- a/src/Client.js +++ b/src/Client.js @@ -112,27 +112,14 @@ class Client extends EventEmitter { * Private function */ async inject() { - if ( - this.options.authTimeoutMs === undefined || - this.options.authTimeoutMs == 0 - ) { - this.options.authTimeoutMs = 30000; - } - let start = Date.now(); - let timeout = this.options.authTimeoutMs; - let res = false; - while (start > Date.now() - timeout) { - res = await this.pupPage.evaluate( - 'window.Debug?.VERSION != undefined', - ); - if (res) { - break; - } - await new Promise((r) => setTimeout(r, 200)); - } - if (!res) { - throw 'auth timeout'; - } + const authTimeout = this.options.authTimeoutMs || 30000; + await this.pupPage + .waitForFunction('window.Debug?.VERSION != undefined', { + timeout: authTimeout, + }) + .catch(() => { + throw 'auth timeout'; + }); await this.setDeviceName( this.options.deviceName, this.options.browserName, @@ -326,21 +313,14 @@ class Client extends EventEmitter { //Load util functions (serializers, helper functions) await this.pupPage.evaluate(LoadUtils); - let start = Date.now(); - let res = false; - while (start > Date.now() - 30000) { - // Check window.WWebJS Injection - res = await this.pupPage.evaluate( - 'window.WWebJS != undefined', - ); - if (res) { - break; - } - await new Promise((r) => setTimeout(r, 200)); - } - if (!res) { - throw 'ready timeout'; - } + // Check window.WWebJS Injection + await this.pupPage + .waitForFunction('window.WWebJS != undefined', { + timeout: 30000, + }) + .catch(() => { + throw 'ready timeout'; + }); /** * Current connection information @@ -490,8 +470,6 @@ class Client extends EventEmitter { referer: 'https://whatsapp.com/', }); - await this.inject(); - this.pupPage.on('framenavigated', async (frame) => { if (frame.url().includes('post_logout=1') || this.lastLoggedOut) { this.emit(Events.DISCONNECTED, 'LOGOUT'); @@ -502,6 +480,8 @@ class Client extends EventEmitter { } await this.inject(); }); + + await this.inject(); } /**