Skip to content

Commit

Permalink
main.stealth.js: puppeteer-extra-plugin-stealth enough to avoid hcapt…
Browse files Browse the repository at this point in the history
…cha, claim successful
  • Loading branch information
vogler committed Dec 27, 2021
1 parent ba97a0e commit 64d0ba8
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 3 deletions.
1 change: 1 addition & 0 deletions main.captcha.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ if (!existsSync('auth.json')) {
console.error('Missing auth.json! Run `npm login` to login and create this file by closing the opened browser.');
}

// npm i playwright playwright-extra@next @extra/recaptcha@next
const { chromium } = require('playwright-extra')

// add recaptcha plugin and provide it your 2captcha token (= their apiKey)
Expand Down
66 changes: 66 additions & 0 deletions main.stealth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const { existsSync } = require('fs');
if (!existsSync('auth.json')) {
console.error('Missing auth.json! Run `npm login` to login and create this file by closing the opened browser.');
}

const { chromium } = require('playwright');

(async () => {
const browser = await chromium.launch({
channel: 'chrome',
headless: false,
});
// https://github.com/berstend/puppeteer-extra/issues/454#issuecomment-917437212
const originalUserAgent = await (await (await browser.newContext()).newPage()).evaluate(() => { return navigator.userAgent });
console.log(originalUserAgent);
const context = await browser.newContext({
storageState: 'auth.json',
viewport: { width: 1280, height: 1280 },
userAgent: originalUserAgent.replace("Headless", ""),
});
const enabledEvasions = [
'chrome.app',
'chrome.csi',
'chrome.loadTimes',
'chrome.runtime',
'iframe.contentWindow',
'media.codecs',
'navigator.hardwareConcurrency',
'navigator.languages',
'navigator.permissions',
'navigator.plugins',
'navigator.webdriver',
'sourceurl',
// 'user-agent-override', // doesn't work since playwright has no page.browser()
'webgl.vendor',
'window.outerdimensions'
];
const evasions = enabledEvasions.map(e => new 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);
}

const page = await context.newPage();
await page.goto('https://www.epicgames.com/store/en-US/free-games');
// await expect(page.locator('a[role="button"]:has-text("Sign In")')).toHaveCount(0);
await page.click('button:has-text("Accept All Cookies")'); // to not waste screen space in --debug
await page.click('[data-testid="offer-card-image-landscape"]');
// TODO check if already claimed
await page.click('[data-testid="purchase-cta-button"]');
await page.click('button:has-text("Continue")');
// it then creates an iframe for the rest
// await page.frame({ url: /.*store\/purchase.*/ }).click('button:has-text("Place Order")'); // not found because it does not wait for iframe
const iframe = page.frameLocator('.webPurchaseContainer iframe')
await iframe.locator('button:has-text("Place Order")').click();
await iframe.locator('button:has-text("I Agree")').click();
await page.pause();
await context.close();
await browser.close();
})();
142 changes: 141 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
"codegen": "npx playwright codegen --load-storage=auth.json https://www.epicgames.com/store/en-US/free-games",
"start": "npx playwright test --timeout 10000",
"debug": "npx playwright test --debug",
"captcha": "node main.captcha"
"captcha": "node main.captcha",
"stealth": "node main.stealth"
},
"devDependencies": {
"@extra/recaptcha": "^4.2.1-next.616",
"@playwright/test": "^1.17.1",
"dotenv": "^10.0.0",
"playwright": "^1.17.1",
"playwright-extra": "^4.2.1-next.616"
"playwright-extra": "^4.2.1-next.616",
"puppeteer-extra-plugin-stealth": "^2.9.0"
}
}

0 comments on commit 64d0ba8

Please sign in to comment.