pull out stealth into util
This commit is contained in:
parent
bd61963fc2
commit
9bb548dc3e
3 changed files with 46 additions and 64 deletions
|
|
@ -1,6 +1,8 @@
|
||||||
//@ts-check
|
//@ts-check
|
||||||
const { chromium } = require('playwright'); // stealth plugin needs no outdated playwright-extra
|
const { chromium } = require('playwright'); // stealth plugin needs no outdated playwright-extra
|
||||||
const path = require('path');
|
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 debug = process.env.PWDEBUG == '1'; // runs non-headless and opens https://playwright.dev/docs/inspector
|
||||||
|
|
||||||
const URL_LOGIN = 'https://www.epicgames.com/login';
|
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.
|
// 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
|
// 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
|
// https://github.com/berstend/puppeteer-extra/tree/master/packages/puppeteer-extra-plugin-stealth/evasions
|
||||||
const enabledEvasions = [
|
await stealth(context);
|
||||||
'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
|
|
||||||
|
|
||||||
if (!debug) context.setDefaultTimeout(TIMEOUT);
|
if (!debug) context.setDefaultTimeout(TIMEOUT);
|
||||||
|
|
||||||
const page = context.pages().length ? context.pages()[0] : await context.newPage(); // should always exist
|
const page = context.pages().length ? context.pages()[0] : await context.newPage(); // should always exist
|
||||||
console.log('userAgent:', await page.evaluate(() => navigator.userAgent));
|
console.log('userAgent:', await page.evaluate(() => navigator.userAgent));
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
//@ts-check
|
//@ts-check
|
||||||
const { chromium } = require('playwright'); // stealth plugin needs no outdated playwright-extra
|
const { chromium } = require('playwright'); // stealth plugin needs no outdated playwright-extra
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const { stealth } = require('./util.js');
|
||||||
|
|
||||||
const debug = process.env.PWDEBUG == '1'; // runs headful and opens https://playwright.dev/docs/inspector
|
const debug = process.env.PWDEBUG == '1'; // runs headful and opens https://playwright.dev/docs/inspector
|
||||||
|
|
||||||
// const URL_LOGIN = 'https://www.amazon.de/ap/signin'; // wrong. needs some session args to be valid?
|
// const URL_LOGIN = 'https://www.amazon.de/ap/signin'; // wrong. needs some session args to be valid?
|
||||||
|
|
@ -18,41 +20,11 @@ const TIMEOUT = 20 * 1000; // 20s, default is 30s
|
||||||
locale: "en-US", // ignore OS locale to be sure to have english text for locators
|
locale: "en-US", // ignore OS locale to be sure to have english text for locators
|
||||||
});
|
});
|
||||||
|
|
||||||
// stealth with playwright: https://github.com/berstend/puppeteer-extra/issues/454#issuecomment-917437212
|
// TODO test if needed
|
||||||
// https://github.com/berstend/puppeteer-extra/tree/master/packages/puppeteer-extra-plugin-stealth/evasions
|
await stealth(context);
|
||||||
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
|
|
||||||
|
|
||||||
if (!debug) context.setDefaultTimeout(TIMEOUT);
|
if (!debug) context.setDefaultTimeout(TIMEOUT);
|
||||||
|
|
||||||
// const page = /* context.pages().length ? context.pages[0] : */ await context.newPage();
|
// const page = /* context.pages().length ? context.pages[0] : */ await context.newPage();
|
||||||
const page = context.pages()[0];
|
const page = context.pages()[0];
|
||||||
console.log('userAgent:', await page.evaluate(() => navigator.userAgent));
|
console.log('userAgent:', await page.evaluate(() => navigator.userAgent));
|
||||||
|
|
|
||||||
37
util.js
37
util.js
|
|
@ -12,3 +12,40 @@ const newStealthContext = async (browser, contextOptions = {}) => {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const stealth = async (context) => {
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { stealth };
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue