|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | const EventEmitter = require('events'); |
4 | | -const puppeteer = require('puppeteer'); |
| 4 | +const puppeteer = require('puppeteer-extra'); |
| 5 | +const StealthPlugin = require('puppeteer-extra-plugin-stealth'); |
| 6 | +const AdblockerPlugin = require('puppeteer-extra-plugin-adblocker'); |
5 | 7 |
|
6 | 8 | const Util = require('./util/Util'); |
7 | 9 | const InterfaceController = require('./util/InterfaceController'); |
@@ -142,6 +144,18 @@ class Client extends EventEmitter { |
142 | 144 |
|
143 | 145 | await this.pupPage.evaluate(ExposeAuthStore); |
144 | 146 |
|
| 147 | + await this.pupPage.evaluate(() => { |
| 148 | + const originalRequire = window.require; |
| 149 | + window.require = (...args) => { |
| 150 | + try { |
| 151 | + return originalRequire(...args); |
| 152 | + } catch (error) { |
| 153 | + console.error('Error in require:', error, 'Args:', args); |
| 154 | + return {}; |
| 155 | + } |
| 156 | + }; |
| 157 | + }); |
| 158 | + |
145 | 159 | const needAuthentication = await this.pupPage.evaluate(async () => { |
146 | 160 | let state = window.require('WAWebSocketModel').Socket.state; |
147 | 161 |
|
@@ -322,6 +336,18 @@ class Client extends EventEmitter { |
322 | 336 | await webCache.persist(this.currentIndexHtml, version); |
323 | 337 | } |
324 | 338 |
|
| 339 | + await new Promise((r) => setTimeout(r, 10000)); // Avoid error "Dropping db read operation due to logout" |
| 340 | + await this.pupPage.evaluate(async () => { |
| 341 | + const targetABFlag = 'wa_web_disable_prefetch_loadables'; |
| 342 | + const ABPrefetchLoadablesExists = !!(await window.require('WAWebABPropsConfigs').ABPropConfigs[targetABFlag]); |
| 343 | + if (ABPrefetchLoadablesExists) { |
| 344 | + const isUsingABPrefetchLoadables = await window.require('WAWebABProps').getABPropConfigValue(targetABFlag); |
| 345 | + if (isUsingABPrefetchLoadables) { |
| 346 | + await window.require('WAWebPrefetchLoadables')(); |
| 347 | + } |
| 348 | + } |
| 349 | + }); |
| 350 | + |
325 | 351 | //Load util functions (serializers, helper functions) |
326 | 352 | await this.pupPage.evaluate(LoadUtils); |
327 | 353 |
|
@@ -373,6 +399,7 @@ class Client extends EventEmitter { |
373 | 399 | */ |
374 | 400 | this.emit(Events.READY); |
375 | 401 | this.authStrategy.afterAuthReady(); |
| 402 | + this.clickNewVersionModalButton(); |
376 | 403 | }, |
377 | 404 | ); |
378 | 405 | let lastPercent = null; |
@@ -422,6 +449,50 @@ class Client extends EventEmitter { |
422 | 449 | }); |
423 | 450 | } |
424 | 451 |
|
| 452 | + /** |
| 453 | + * Clicks on new version modal button to close it |
| 454 | + */ |
| 455 | + async clickNewVersionModalButton() { |
| 456 | + try { |
| 457 | + const modalSelector = 'div[data-animate-modal-popup="true"]'; |
| 458 | + const modalExists = await this.pupPage |
| 459 | + .waitForSelector(modalSelector, { |
| 460 | + timeout: 30000, |
| 461 | + visible: true, |
| 462 | + }) |
| 463 | + .then(() => true) |
| 464 | + .catch(() => false); |
| 465 | + if (!modalExists) { |
| 466 | + return; |
| 467 | + } |
| 468 | + const buttonSelector = `${modalSelector} button div:not(:empty)`; |
| 469 | + const buttonText = [ |
| 470 | + 'Продолжить', |
| 471 | + 'Continue', |
| 472 | + 'Continuar', |
| 473 | + 'Continuare', |
| 474 | + 'Continuez', |
| 475 | + 'Продовжити', |
| 476 | + ]; |
| 477 | + await this.pupPage.evaluate( |
| 478 | + (selector, buttonText) => { |
| 479 | + const elements = document.querySelectorAll(selector); |
| 480 | + for (const element of elements) { |
| 481 | + const text = element.textContent.trim(); |
| 482 | + if (buttonText.includes(text)) { |
| 483 | + element.click(); |
| 484 | + return; |
| 485 | + } |
| 486 | + } |
| 487 | + }, |
| 488 | + buttonSelector, |
| 489 | + buttonText, |
| 490 | + ); |
| 491 | + } catch (error) { |
| 492 | + console.error('Error clicking new version modal button:', error); |
| 493 | + } |
| 494 | + } |
| 495 | + |
425 | 496 | /** |
426 | 497 | * Sets up events and requirements, kicks off authentication request |
427 | 498 | */ |
@@ -458,6 +529,15 @@ class Client extends EventEmitter { |
458 | 529 | // navigator.webdriver fix |
459 | 530 | browserArgs.push('--disable-blink-features=AutomationControlled'); |
460 | 531 |
|
| 532 | + if (this.options.stealth) { |
| 533 | + const stealth = StealthPlugin(); |
| 534 | + stealth.enabledEvasions.delete('iframe.contentWindow'); |
| 535 | + stealth.enabledEvasions.delete('media.codecs'); |
| 536 | + stealth.enabledEvasions.delete('user-agent-override'); |
| 537 | + puppeteer.use(stealth); |
| 538 | + puppeteer.use(AdblockerPlugin({ blockTrackers: true })); |
| 539 | + } |
| 540 | + |
461 | 541 | browser = await puppeteer.launch({ |
462 | 542 | ...puppeteerOpts, |
463 | 543 | args: browserArgs, |
|
0 commit comments