Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 18 additions & 38 deletions src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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');
Expand All @@ -502,6 +480,8 @@ class Client extends EventEmitter {
}
await this.inject();
});

await this.inject();
}

/**
Expand Down