-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
main.stealth.js
135 lines (130 loc) · 7.41 KB
/
main.stealth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//@ts-check
const { chromium } = require('playwright'); // stealth plugin needs no outdated playwright-extra
const debug = process.env.PWDEBUG == '1'; // runs headful and opens https://playwright.dev/docs/inspector
const URL_LOGIN = 'https://www.epicgames.com/login';
const URL_CLAIM = 'https://www.epicgames.com/store/en-US/free-games';
const TIMEOUT = 20 * 1000; // 20s, default is 30s
// stealth with playwright: https://github.com/berstend/puppeteer-extra/issues/454#issuecomment-917437212
const newStealthContext = async (browser, contextOptions = {}) => {
if (!debug) { // fix userAgent in headless mode
const dummyContext = await browser.newContext();
const originalUserAgent = await (await dummyContext.newPage()).evaluate(() => navigator.userAgent);
await dummyContext.close();
// console.log('originalUserAgent:', originalUserAgent); // Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/96.0.4664.110 Safari/537.36
contextOptions = {
...contextOptions,
userAgent: originalUserAgent.replace("Headless", ""), // HeadlessChrome -> Chrome, TODO needed?
};
}
};
// could change to .mjs to get top-level-await, but would then also need to change require to import and dynamic import for stealth below would just add more async/await
(async () => {
// https://playwright.dev/docs/auth#multi-factor-authentication
const context = await chromium.launchPersistentContext('userDataDir', {
channel: 'chrome', // https://playwright.dev/docs/browsers#google-chrome--microsoft-edge
headless: false,
viewport: { width: 1280, height: 1280 },
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36', // see replace of Headless in newStealthContext above. TODO update if browser is updated!
});
// 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 => 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);
}
// end stealth setup
if (!debug) context.setDefaultTimeout(TIMEOUT);
const page = context.pages.length ? context.pages[0] : await context.newPage();
console.log('userAgent:', await page.evaluate(() => navigator.userAgent));
await page.goto(URL_CLAIM, {waitUntil: 'domcontentloaded'}); // default 'load' takes forever
// with persistent context the cookie message will only show up the first time, so we can't unconditionally wait for it - just let the user click it.
// await page.click('button:has-text("Accept All Cookies")'); // to not waste screen space in --debug
while (await page.locator('a[role="button"]:has-text("Sign In")').count() > 0) {
console.error("Not signed in anymore. Please login and then navigate to the 'Free Games' page.");
context.setDefaultTimeout(0); // give user time to log in without timeout
await page.goto(URL_LOGIN, {waitUntil: 'domcontentloaded'});
// after login it just reloads the login page...
await page.waitForNavigation({url: URL_CLAIM});
context.setDefaultTimeout(TIMEOUT);
// process.exit(1);
}
console.log('Signed in.');
// click on each banner with 'Free Now'. TODO just extract the URLs and go to them in the loop
const game_sel = 'div[data-component="FreeOfferCard"]:has-text("Free Now")';
await page.waitForSelector(game_sel);
// const games = await page.$$(game_sel); // 'Element is not attached to the DOM' after navigation; had `for (const game of games) { await game.click(); ... }
const n = await page.locator(game_sel).count();
console.log('Number of free games:', n);
for (let i=1; i<=n; i++) {
await page.click(`:nth-match(${game_sel}, ${i})`);
const title = await page.locator('h1 div').first().innerText();
console.log('Current free game:', title);
// click Continue if 'This game contains mature content recommended only for ages 18+'
if (await page.locator(':has-text("Continue")').count() > 0) {
console.log('This game contains mature content recommended only for ages 18+');
await page.click('button:has-text("Continue")');
}
const btnText = await page.locator('[data-testid="purchase-cta-button"]').innerText();
if (btnText.toLowerCase() == 'in library') {
console.log('Already in library! Nothing to claim.');
} else {
console.log('Not in library yet! Click GET.')
await page.click('[data-testid="purchase-cta-button"]');
// click Continue if 'Device not supported. This product is not compatible with your current device.'
// await page.waitForTimeout(1000); // wait for 1s since count does not wait.
// @ts-ignore https://caniuse.com/?search=promise.any
await Promise.any([':has-text("Continue")', '#webPurchaseContainer iframe'].map(s => page.waitForSelector(s))); // wait for Continue xor iframe
if (await page.locator(':has-text("Continue")').count() > 0) {
console.log('Device not supported. This product is not compatible with your current device.');
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();
// This is true even when there is no captcha shown! That was the reason why old.stealth.js worked - it did not have this check... TODO check for hcaptcha
// if (await iframe.frameLocator('#talon_frame_checkout_free_prod').locator('text=Please complete a security check to continue').count() > 0) {
// console.error('Encountered hcaptcha. Giving up :(');
// await page.pause();
// process.exit(1);
// }
await page.waitForSelector('text=Thank you for buying');
console.log('Claimed successfully!');
await page.pause();
}
if (i<n) { // no need to go back if it's the last game
await page.goto(URL_CLAIM, {waitUntil: 'domcontentloaded'});
await page.waitForSelector(game_sel);
}
}
// await context.waitForEvent("close");
await context.close();
})();