pull out stealth into util

This commit is contained in:
Ralf Vogler 2022-02-24 17:07:33 +01:00
parent bd61963fc2
commit 9bb548dc3e
3 changed files with 46 additions and 64 deletions

View file

@ -1,6 +1,8 @@
//@ts-check
const { chromium } = require('playwright'); // stealth plugin needs no outdated playwright-extra
const path = require('path');
const { stealth } = require('./util.js');
const debug = process.env.PWDEBUG == '1'; // runs non-headless and opens https://playwright.dev/docs/inspector
const URL_LOGIN = 'https://www.epicgames.com/login';
@ -25,39 +27,10 @@ const TIMEOUT = 20 * 1000; // 20s, default is 30s
// Without stealth plugin, the website shows an hcaptcha on login with username/password and in the last step of claiming a game. It may have other heuristics like unsuccessful logins as well. After <6h (TBD) it resets to no captcha again. Getting a new IP also resets.
// stealth with playwright: https://github.com/berstend/puppeteer-extra/issues/454#issuecomment-917437212
// https://github.com/berstend/puppeteer-extra/tree/master/packages/puppeteer-extra-plugin-stealth/evasions
const enabledEvasions = [
'chrome.app',
'chrome.csi',
'chrome.loadTimes',
'chrome.runtime',
// 'defaultArgs',
'iframe.contentWindow',
'media.codecs',
'navigator.hardwareConcurrency',
'navigator.languages',
'navigator.permissions',
'navigator.plugins',
// 'navigator.vendor',
'navigator.webdriver',
'sourceurl',
// 'user-agent-override', // doesn't work since playwright has no page.browser()
'webgl.vendor',
'window.outerdimensions'
];
const evasions = enabledEvasions.map(e => require(`puppeteer-extra-plugin-stealth/evasions/${e}`));
const stealth = {
callbacks: [],
async evaluateOnNewDocument(...args) {
this.callbacks.push({ cb: args[0], a: args[1] })
}
}
evasions.forEach(e => e().onPageCreated(stealth));
for (let evasion of stealth.callbacks) {
await context.addInitScript(evasion.cb, evasion.a);
}
// end stealth setup
await stealth(context);
if (!debug) context.setDefaultTimeout(TIMEOUT);
const page = context.pages().length ? context.pages()[0] : await context.newPage(); // should always exist
console.log('userAgent:', await page.evaluate(() => navigator.userAgent));