@wppconnect-team/wppconnect 1.17.0 → 1.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,21 @@
1
+ # 1.18.0 (2022-10-18)
2
+
3
+
4
+ ### Features
5
+
6
+ * Improved QRCode re-scan (fix [#1189](https://github.com/wppconnect-team/wppconnect/issues/1189)) ([8ec5264](https://github.com/wppconnect-team/wppconnect/commit/8ec5264e55e01c15efd3e3c273c54e34cad166c9))
7
+
8
+
9
+
10
+ ## 1.17.1 (2022-10-10)
11
+
12
+
13
+ ### Bug Fixes
14
+
15
+ * **deps:** update dependency @wppconnect/wa-js to ^2.13.1 ([#1364](https://github.com/wppconnect-team/wppconnect/issues/1364)) ([ff92035](https://github.com/wppconnect-team/wppconnect/commit/ff92035cb05abc21a8e241d8c7d3d5be4614edfb))
16
+
17
+
18
+
1
19
  # 1.17.0 (2022-09-17)
2
20
 
3
21
 
@@ -15,11 +15,14 @@ export declare class HostLayer {
15
15
  protected autoCloseInterval: any;
16
16
  protected autoCloseCalled: boolean;
17
17
  protected statusFind?: StatusFindCallback;
18
+ protected isInitialized: boolean;
19
+ protected isInjected: boolean;
18
20
  onLoadingScreen?: LoadingScreenCallback;
19
21
  constructor(page: Page, session?: string, options?: CreateConfig);
20
22
  protected log(level: LogLevel, message: string, meta?: object): void;
21
23
  protected initialize(): Promise<void>;
22
24
  protected afterPageLoad(): Promise<void>;
25
+ protected afterPageScriptInjected(): Promise<void>;
23
26
  protected tryAutoClose(): void;
24
27
  protected startAutoClose(): void;
25
28
  protected cancelAutoClose(): void;
@@ -77,6 +77,8 @@ var HostLayer = /** @class */ (function () {
77
77
  this.autoCloseInterval = null;
78
78
  this.autoCloseCalled = false;
79
79
  this.statusFind = null;
80
+ this.isInitialized = false;
81
+ this.isInjected = false;
80
82
  this.onLoadingScreen = null;
81
83
  this.session = session;
82
84
  this.options = __assign(__assign({}, create_config_1.defaultOptions), options);
@@ -138,6 +140,10 @@ var HostLayer = /** @class */ (function () {
138
140
  }
139
141
  hasUserDataDir = !!((_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.puppeteerOptions) === null || _b === void 0 ? void 0 : _b.userDataDir);
140
142
  clear = !hasUserDataDir || (hasUserDataDir && !isValidToken);
143
+ this.page.on('close', function () {
144
+ _this.cancelAutoClose();
145
+ _this.log('verbose', 'Page Closed', { type: 'page' });
146
+ });
141
147
  return [4 /*yield*/, (0, browser_1.initWhatsapp)(this.page, sessionToken, clear, this.options.whatsappVersion)];
142
148
  case 3:
143
149
  _c.sent();
@@ -145,10 +151,7 @@ var HostLayer = /** @class */ (function () {
145
151
  _this.log('verbose', 'Page loaded', { type: 'page' });
146
152
  _this.afterPageLoad();
147
153
  });
148
- this.page.on('close', function () {
149
- _this.cancelAutoClose();
150
- _this.log('verbose', 'Page Closed', { type: 'page' });
151
- });
154
+ this.isInitialized = true;
152
155
  return [2 /*return*/];
153
156
  }
154
157
  });
@@ -174,21 +177,15 @@ var HostLayer = /** @class */ (function () {
174
177
  }, options)];
175
178
  case 1:
176
179
  _a.sent();
180
+ this.isInjected = false;
177
181
  return [4 /*yield*/, (0, browser_1.injectApi)(this.page, this.onLoadingScreen)
178
182
  .then(function () {
183
+ _this.isInjected = true;
179
184
  _this.log('verbose', 'wapi.js injected');
180
- _this.getWAVersion()
181
- .then(function (version) {
182
- _this.log('info', "WhatsApp WEB version: ".concat(version));
183
- })
184
- .catch(function () { return null; });
185
- _this.getWAJSVersion()
186
- .then(function (version) {
187
- _this.log('info', "WA-JS version: ".concat(version));
188
- })
189
- .catch(function () { return null; });
185
+ _this.afterPageScriptInjected();
190
186
  })
191
187
  .catch(function (e) {
188
+ console.log(e);
192
189
  _this.log('verbose', 'wapi.js failed');
193
190
  })];
194
191
  case 2:
@@ -198,6 +195,24 @@ var HostLayer = /** @class */ (function () {
198
195
  });
199
196
  });
200
197
  };
198
+ HostLayer.prototype.afterPageScriptInjected = function () {
199
+ return __awaiter(this, void 0, void 0, function () {
200
+ var _this = this;
201
+ return __generator(this, function (_a) {
202
+ this.getWAVersion()
203
+ .then(function (version) {
204
+ _this.log('info', "WhatsApp WEB version: ".concat(version));
205
+ })
206
+ .catch(function () { return null; });
207
+ this.getWAJSVersion()
208
+ .then(function (version) {
209
+ _this.log('info', "WA-JS version: ".concat(version));
210
+ })
211
+ .catch(function () { return null; });
212
+ return [2 /*return*/];
213
+ });
214
+ });
215
+ };
201
216
  HostLayer.prototype.tryAutoClose = function () {
202
217
  if (this.autoCloseInterval) {
203
218
  this.cancelAutoClose();
@@ -328,13 +343,14 @@ var HostLayer = /** @class */ (function () {
328
343
  return __awaiter(this, void 0, void 0, function () {
329
344
  return __generator(this, function (_a) {
330
345
  switch (_a.label) {
331
- case 0: return [4 /*yield*/, this.page
332
- .waitForFunction("!document.querySelector('#initial_startup')")
333
- .catch(function () { })];
346
+ case 0:
347
+ if (!!this.isInjected) return [3 /*break*/, 2];
348
+ return [4 /*yield*/, (0, sleep_1.sleep)(200)];
334
349
  case 1:
335
350
  _a.sent();
336
- return [4 /*yield*/, (0, auth_1.getInterfaceStatus)(this.page).catch(function () { return null; })];
337
- case 2:
351
+ return [3 /*break*/, 0];
352
+ case 2: return [4 /*yield*/, this.page.waitForFunction(function () { return WPP.isReady; }).catch(function () { })];
353
+ case 3:
338
354
  _a.sent();
339
355
  return [2 /*return*/];
340
356
  }
@@ -19,7 +19,7 @@ export declare class ListenerLayer extends ProfileLayer {
19
19
  page: Page;
20
20
  private listenerEmitter;
21
21
  constructor(page: Page, session?: string, options?: CreateConfig);
22
- protected afterPageLoad(): Promise<void>;
22
+ protected afterPageScriptInjected(): Promise<void>;
23
23
  /**
24
24
  * Register the event and create a disposable object to stop the listening
25
25
  * @param event Name of event
@@ -109,13 +109,13 @@ var ListenerLayer = /** @class */ (function (_super) {
109
109
  };
110
110
  return _this;
111
111
  }
112
- ListenerLayer.prototype.afterPageLoad = function () {
112
+ ListenerLayer.prototype.afterPageScriptInjected = function () {
113
113
  return __awaiter(this, void 0, void 0, function () {
114
114
  var functions, _loop_1, this_1, _i, functions_1, func;
115
115
  var _this = this;
116
116
  return __generator(this, function (_a) {
117
117
  switch (_a.label) {
118
- case 0: return [4 /*yield*/, _super.prototype.afterPageLoad.call(this)];
118
+ case 0: return [4 /*yield*/, _super.prototype.afterPageScriptInjected.call(this)];
119
119
  case 1:
120
120
  _a.sent();
121
121
  functions = __spreadArray(__spreadArray([], Object.values(exposed_enum_1.ExposedFn), true), [
@@ -76,7 +76,6 @@ var decrypt_1 = require("./helpers/decrypt");
76
76
  var WAuserAgente_1 = require("../config/WAuserAgente");
77
77
  var axios_1 = __importDefault(require("axios"));
78
78
  var treekill = require("tree-kill");
79
- var enum_1 = require("./model/enum");
80
79
  var helpers_1 = require("./helpers");
81
80
  var Whatsapp = /** @class */ (function (_super) {
82
81
  __extends(Whatsapp, _super);
@@ -84,6 +83,7 @@ var Whatsapp = /** @class */ (function (_super) {
84
83
  var _this = _super.call(this, page, session, options) || this;
85
84
  _this.page = page;
86
85
  var connected = false;
86
+ var interval = null;
87
87
  var removeToken = function () { return __awaiter(_this, void 0, void 0, function () {
88
88
  var removed;
89
89
  return __generator(this, function (_a) {
@@ -110,103 +110,100 @@ var Whatsapp = /** @class */ (function (_super) {
110
110
  case 1:
111
111
  _a.sent();
112
112
  _a.label = 2;
113
- case 2: return [2 /*return*/];
113
+ case 2:
114
+ clearInterval(interval);
115
+ return [2 /*return*/];
114
116
  }
115
117
  });
116
118
  }); });
119
+ page
120
+ .evaluate(function () { return WPP.conn.isAuthenticated(); })
121
+ .then(function (isAuthenticated) {
122
+ connected = isAuthenticated;
123
+ })
124
+ .catch(function () { return null; });
117
125
  }
118
- _this.onStateChange(function (state) { return __awaiter(_this, void 0, void 0, function () {
126
+ interval = setInterval(function (state) { return __awaiter(_this, void 0, void 0, function () {
127
+ var newConnected;
119
128
  var _this = this;
120
129
  return __generator(this, function (_a) {
121
- switch (state) {
122
- case enum_1.SocketState.CONNECTED:
123
- connected = true;
124
- // wait for localStore populate
125
- setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
126
- var tokenData, updated;
127
- return __generator(this, function (_a) {
128
- switch (_a.label) {
129
- case 0:
130
- this.log('verbose', 'Updating session token', { type: 'token' });
131
- return [4 /*yield*/, this.getSessionTokenBrowser()];
132
- case 1:
133
- tokenData = _a.sent();
134
- return [4 /*yield*/, Promise.resolve(this.tokenStore.setToken(this.session, tokenData))];
135
- case 2:
136
- updated = _a.sent();
137
- if (updated) {
138
- this.log('verbose', 'Session token updated', {
139
- type: 'token',
140
- });
141
- }
142
- else {
143
- this.log('warn', 'Failed to update session token', {
144
- type: 'token',
145
- });
146
- }
147
- return [2 /*return*/];
148
- }
149
- });
150
- }); }, 1000);
151
- break;
152
- case enum_1.SocketState.UNPAIRED:
153
- case enum_1.SocketState.UNPAIRED_IDLE:
154
- setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
155
- return __generator(this, function (_a) {
156
- switch (_a.label) {
157
- case 0: return [4 /*yield*/, removeToken()];
158
- case 1:
159
- _a.sent();
160
- // Fire only after a success connection and disconnection
161
- if (connected && this.statusFind) {
162
- try {
163
- this.statusFind('desconnectedMobile', session);
130
+ switch (_a.label) {
131
+ case 0: return [4 /*yield*/, page
132
+ .evaluate(function () { return WPP.conn.isAuthenticated(); })
133
+ .catch(function () { return null; })];
134
+ case 1:
135
+ newConnected = _a.sent();
136
+ if (newConnected === null || newConnected === connected) {
137
+ return [2 /*return*/];
138
+ }
139
+ if (newConnected) {
140
+ connected = newConnected;
141
+ setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
142
+ var tokenData, updated;
143
+ return __generator(this, function (_a) {
144
+ switch (_a.label) {
145
+ case 0:
146
+ this.log('verbose', 'Updating session token', { type: 'token' });
147
+ return [4 /*yield*/, this.getSessionTokenBrowser()];
148
+ case 1:
149
+ tokenData = _a.sent();
150
+ return [4 /*yield*/, Promise.resolve(this.tokenStore.setToken(this.session, tokenData))];
151
+ case 2:
152
+ updated = _a.sent();
153
+ if (updated) {
154
+ this.log('verbose', 'Session token updated', {
155
+ type: 'token',
156
+ });
164
157
  }
165
- catch (error) { }
158
+ else {
159
+ this.log('warn', 'Failed to update session token', {
160
+ type: 'token',
161
+ });
162
+ }
163
+ return [2 /*return*/];
164
+ }
165
+ });
166
+ }); }, 1000);
167
+ }
168
+ else {
169
+ if (!newConnected && connected) {
170
+ setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
171
+ return __generator(this, function (_a) {
172
+ switch (_a.label) {
173
+ case 0: return [4 /*yield*/, page.evaluate(function () { return localStorage.clear(); })];
174
+ case 1:
175
+ _a.sent();
176
+ return [4 /*yield*/, page.reload()];
177
+ case 2:
178
+ _a.sent();
179
+ return [2 /*return*/];
166
180
  }
167
- if (!connected) return [3 /*break*/, 5];
168
- return [4 /*yield*/, page.evaluate(function () { return localStorage.clear(); })];
169
- case 2:
170
- _a.sent();
171
- return [4 /*yield*/, page.evaluate(function () {
172
- var promises = [];
173
- window.indexedDB
174
- .databases()
175
- .then(function (dbs) {
176
- dbs.forEach(function (db) {
177
- promises.push(new Promise(function (resolve) {
178
- var r = window.indexedDB.deleteDatabase(db.name);
179
- r.onerror = r.onblocked = function () {
180
- this.result.close();
181
- window.indexedDB.deleteDatabase(db.name);
182
- resolve();
183
- };
184
- r.onsuccess = function () {
185
- resolve();
186
- };
187
- }));
188
- });
189
- })
190
- .catch(function () { return null; });
191
- return Promise.all(promises);
192
- })];
193
- case 3:
194
- _a.sent();
195
- return [4 /*yield*/, page.reload()];
196
- case 4:
197
- _a.sent();
198
- _a.label = 5;
199
- case 5:
200
- connected = false;
201
- return [2 /*return*/];
202
- }
203
- });
204
- }); }, 1000);
205
- break;
181
+ });
182
+ }); }, 1000);
183
+ }
184
+ connected = newConnected;
185
+ setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
186
+ return __generator(this, function (_a) {
187
+ switch (_a.label) {
188
+ case 0: return [4 /*yield*/, removeToken()];
189
+ case 1:
190
+ _a.sent();
191
+ // Fire only after a success connection and disconnection
192
+ if (newConnected && this.statusFind) {
193
+ try {
194
+ this.statusFind('desconnectedMobile', session);
195
+ }
196
+ catch (error) { }
197
+ }
198
+ return [2 /*return*/];
199
+ }
200
+ });
201
+ }); }, 1000);
202
+ }
203
+ return [2 /*return*/];
206
204
  }
207
- return [2 /*return*/];
208
205
  });
209
- }); });
206
+ }); }, 1000);
210
207
  return _this;
211
208
  }
212
209
  /**
@@ -81,6 +81,7 @@ var path = __importStar(require("path"));
81
81
  var qrcode = __importStar(require("qrcode-terminal"));
82
82
  var puppeteer_config_1 = require("../config/puppeteer.config");
83
83
  var token_store_1 = require("../token-store");
84
+ var sleep_1 = require("../utils/sleep");
84
85
  var getInterfaceStatus = function (waPage) { return __awaiter(void 0, void 0, void 0, function () {
85
86
  return __generator(this, function (_a) {
86
87
  switch (_a.label) {
@@ -126,61 +127,40 @@ exports.getInterfaceStatus = getInterfaceStatus;
126
127
  * @param waPage
127
128
  */
128
129
  var isAuthenticated = function (waPage) { return __awaiter(void 0, void 0, void 0, function () {
129
- var status;
130
130
  return __generator(this, function (_a) {
131
131
  switch (_a.label) {
132
- case 0: return [4 /*yield*/, (0, exports.getInterfaceStatus)(waPage)];
133
- case 1:
134
- status = _a.sent();
135
- if (typeof status !== 'string') {
136
- return [2 /*return*/, null];
137
- }
138
- return [2 /*return*/, ['CONNECTED', 'PAIRING'].includes(status)];
132
+ case 0: return [4 /*yield*/, waPage.evaluate(function () { return WPP.conn.isAuthenticated(); })];
133
+ case 1: return [2 /*return*/, _a.sent()];
139
134
  }
140
135
  });
141
136
  }); };
142
137
  exports.isAuthenticated = isAuthenticated;
143
138
  var needsToScan = function (waPage) { return __awaiter(void 0, void 0, void 0, function () {
144
- var status;
139
+ var connected;
145
140
  return __generator(this, function (_a) {
146
141
  switch (_a.label) {
147
- case 0: return [4 /*yield*/, (0, exports.getInterfaceStatus)(waPage)];
142
+ case 0: return [4 /*yield*/, (0, exports.isAuthenticated)(waPage)];
148
143
  case 1:
149
- status = _a.sent();
150
- if (typeof status !== 'string') {
151
- return [2 /*return*/, null];
152
- }
153
- return [2 /*return*/, status === 'UNPAIRED'];
144
+ connected = _a.sent();
145
+ return [2 /*return*/, !connected];
154
146
  }
155
147
  });
156
148
  }); };
157
149
  exports.needsToScan = needsToScan;
158
150
  var isInsideChat = function (waPage) { return __awaiter(void 0, void 0, void 0, function () {
159
- var status;
160
151
  return __generator(this, function (_a) {
161
152
  switch (_a.label) {
162
- case 0: return [4 /*yield*/, (0, exports.getInterfaceStatus)(waPage)];
163
- case 1:
164
- status = _a.sent();
165
- if (typeof status !== 'string') {
166
- return [2 /*return*/, null];
167
- }
168
- return [2 /*return*/, status === 'CONNECTED'];
153
+ case 0: return [4 /*yield*/, waPage.evaluate(function () { return WPP.conn.isMainReady(); })];
154
+ case 1: return [2 /*return*/, _a.sent()];
169
155
  }
170
156
  });
171
157
  }); };
172
158
  exports.isInsideChat = isInsideChat;
173
159
  var isConnectingToPhone = function (waPage) { return __awaiter(void 0, void 0, void 0, function () {
174
- var status;
175
160
  return __generator(this, function (_a) {
176
161
  switch (_a.label) {
177
- case 0: return [4 /*yield*/, (0, exports.getInterfaceStatus)(waPage)];
178
- case 1:
179
- status = _a.sent();
180
- if (typeof status !== 'string') {
181
- return [2 /*return*/, null];
182
- }
183
- return [2 /*return*/, status === 'PAIRING'];
162
+ case 0: return [4 /*yield*/, waPage.evaluate(function () { return WPP.conn.isMainLoaded() && !WPP.conn.isMainReady(); })];
163
+ case 1: return [2 /*return*/, _a.sent()];
184
164
  }
185
165
  });
186
166
  }); };
@@ -229,11 +209,14 @@ function injectSessionToken(page, token, clear) {
229
209
  });
230
210
  };
231
211
  page.on('request', reqHandler);
232
- return [4 /*yield*/, page.goto(puppeteer_config_1.puppeteerConfig.whatsappUrl)];
212
+ return [4 /*yield*/, page.goto(puppeteer_config_1.puppeteerConfig.whatsappUrl + '?_=' + Date.now())];
233
213
  case 2:
234
214
  _a.sent();
235
- if (!clear) return [3 /*break*/, 4];
215
+ if (!clear) return [3 /*break*/, 5];
236
216
  return [4 /*yield*/, page.evaluate(function (session) {
217
+ if (document.title !== 'Initializing WhatsApp') {
218
+ return;
219
+ }
237
220
  localStorage.clear();
238
221
  window.indexedDB
239
222
  .databases()
@@ -246,19 +229,29 @@ function injectSessionToken(page, token, clear) {
246
229
  })];
247
230
  case 3:
248
231
  _a.sent();
249
- _a.label = 4;
250
- case 4: return [4 /*yield*/, page.evaluate(function (session) {
251
- Object.keys(session).forEach(function (key) {
252
- localStorage.setItem(key, session[key]);
253
- });
254
- localStorage.setItem('remember-me', 'true');
255
- }, token)];
232
+ return [4 /*yield*/, (0, sleep_1.sleep)(2000)];
233
+ case 4:
234
+ _a.sent();
235
+ _a.label = 5;
256
236
  case 5:
237
+ if (!(token.WASecretBundle !== 'MultiDevice')) return [3 /*break*/, 7];
238
+ return [4 /*yield*/, page.evaluate(function (session) {
239
+ Object.keys(session).forEach(function (key) {
240
+ localStorage.setItem(key, session[key]);
241
+ });
242
+ }, token)];
243
+ case 6:
244
+ _a.sent();
245
+ _a.label = 7;
246
+ case 7: return [4 /*yield*/, page.evaluate(function () {
247
+ localStorage.setItem('remember-me', 'true');
248
+ })];
249
+ case 8:
257
250
  _a.sent();
258
251
  // Disable
259
252
  page.removeAllListeners('request');
260
253
  return [4 /*yield*/, page.setRequestInterception(false)];
261
- case 6:
254
+ case 9:
262
255
  _a.sent();
263
256
  return [2 /*return*/];
264
257
  }
@@ -12,7 +12,7 @@ export declare function unregisterServiceWorker(page: Page): Promise<void>;
12
12
  export declare function setWhatsappVersion(page: Page, version: string): Promise<void>;
13
13
  export declare function initWhatsapp(page: Page, token?: SessionToken, clear?: boolean, version?: string): Promise<Page>;
14
14
  export declare function onLoadingScreen(page: Page, onLoadingScreenCallBack?: LoadingScreenCallback): Promise<void>;
15
- export declare function injectApi(page: Page, onLoadingScreenCallBack?: LoadingScreenCallback): Promise<boolean | import("puppeteer").JSHandle<unknown>>;
15
+ export declare function injectApi(page: Page, onLoadingScreenCallBack?: LoadingScreenCallback): Promise<void>;
16
16
  /**
17
17
  * Initializes browser, will try to use chrome as default
18
18
  * @param session
@@ -148,12 +148,10 @@ function setWhatsappVersion(page, version) {
148
148
  var body;
149
149
  return __generator(this, function (_a) {
150
150
  switch (_a.label) {
151
- case 0: return [4 /*yield*/, unregisterServiceWorker(page)];
152
- case 1:
153
- _a.sent();
151
+ case 0:
154
152
  body = waVersion.getPageContent(version);
155
153
  return [4 /*yield*/, page.setRequestInterception(true)];
156
- case 2:
154
+ case 1:
157
155
  _a.sent();
158
156
  page.on('request', function (req) {
159
157
  if (req.url().startsWith('https://web.whatsapp.com/check-update')) {
@@ -184,18 +182,21 @@ function initWhatsapp(page, token, clear, version) {
184
182
  switch (_a.label) {
185
183
  case 0: return [4 /*yield*/, page.setUserAgent(WAuserAgente_1.useragentOverride)];
186
184
  case 1:
185
+ _a.sent();
186
+ return [4 /*yield*/, unregisterServiceWorker(page)];
187
+ case 2:
187
188
  _a.sent();
188
189
  // Auth with token
189
190
  return [4 /*yield*/, (0, auth_1.injectSessionToken)(page, token, clear)];
190
- case 2:
191
+ case 3:
191
192
  // Auth with token
192
193
  _a.sent();
193
- if (!version) return [3 /*break*/, 4];
194
+ if (!version) return [3 /*break*/, 5];
194
195
  return [4 /*yield*/, setWhatsappVersion(page, version)];
195
- case 3:
196
- _a.sent();
197
- _a.label = 4;
198
196
  case 4:
197
+ _a.sent();
198
+ _a.label = 5;
199
+ case 5:
199
200
  timeout = 10 * 1000;
200
201
  return [4 /*yield*/, page
201
202
  .goto(puppeteer_config_1.puppeteerConfig.whatsappUrl, {
@@ -203,7 +204,7 @@ function initWhatsapp(page, token, clear, version) {
203
204
  waitUntil: 'domcontentloaded',
204
205
  })
205
206
  .catch(function () { })];
206
- case 5:
207
+ case 6:
207
208
  _a.sent();
208
209
  return [2 /*return*/, page];
209
210
  }
@@ -221,7 +222,8 @@ function onLoadingScreen(page, onLoadingScreenCallBack) {
221
222
  case 0: return [4 /*yield*/, page.evaluate("function getElementByXpath(path) {\n return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;\n }")];
222
223
  case 1:
223
224
  _a.sent();
224
- return [4 /*yield*/, page.exposeFunction('loadingScreen', function (percent, message) { return __awaiter(_this, void 0, void 0, function () {
225
+ return [4 /*yield*/, page
226
+ .exposeFunction('loadingScreen', function (percent, message) { return __awaiter(_this, void 0, void 0, function () {
225
227
  return __generator(this, function (_a) {
226
228
  if (lastPercent !== percent || lastPercentMessage !== message) {
227
229
  onLoadingScreenCallBack && onLoadingScreenCallBack(percent, message);
@@ -230,7 +232,8 @@ function onLoadingScreen(page, onLoadingScreenCallBack) {
230
232
  }
231
233
  return [2 /*return*/];
232
234
  });
233
- }); })];
235
+ }); })
236
+ .catch(function () { return null; })];
234
237
  case 2:
235
238
  _a.sent();
236
239
  return [4 /*yield*/, page.evaluate(function (selectors) {
@@ -286,15 +289,6 @@ function injectApi(page, onLoadingScreenCallBack) {
286
289
  path: require.resolve('@wppconnect/wa-js'),
287
290
  })];
288
291
  case 2:
289
- _a.sent();
290
- return [4 /*yield*/, page
291
- .waitForFunction(function () {
292
- return typeof window.WPP !== 'undefined' && window.WPP.isReady;
293
- }, {
294
- timeout: 60000,
295
- })
296
- .catch(function () { return false; })];
297
- case 3:
298
292
  _a.sent();
299
293
  return [4 /*yield*/, page
300
294
  .evaluate(function () {
@@ -302,25 +296,28 @@ function injectApi(page, onLoadingScreenCallBack) {
302
296
  WPP.conn.setKeepAlive(true);
303
297
  })
304
298
  .catch(function () { return false; })];
305
- case 4:
299
+ case 3:
306
300
  _a.sent();
307
301
  return [4 /*yield*/, page.addScriptTag({
308
302
  path: require.resolve(path.join(__dirname, '../../dist/lib/wapi', 'wapi.js')),
309
303
  })];
310
- case 5:
304
+ case 4:
311
305
  _a.sent();
312
306
  return [4 /*yield*/, onLoadingScreen(page, onLoadingScreenCallBack)];
313
- case 6:
307
+ case 5:
314
308
  _a.sent();
309
+ // Make sure WAPI is initialized
315
310
  return [4 /*yield*/, page
316
311
  .waitForFunction(function () {
317
312
  return (typeof window.WAPI !== 'undefined' &&
318
- typeof window.Store !== 'undefined');
313
+ typeof window.Store !== 'undefined' &&
314
+ window.WPP.isReady);
319
315
  })
320
316
  .catch(function () { return false; })];
321
- case 7:
322
- // Make sure WAPI is initialized
323
- return [2 /*return*/, _a.sent()];
317
+ case 6:
318
+ // Make sure WAPI is initialized
319
+ _a.sent();
320
+ return [2 /*return*/];
324
321
  }
325
322
  });
326
323
  });