check if game already claimed

This commit is contained in:
Ralf Vogler 2021-12-28 01:47:38 +01:00
parent 64d0ba8ce7
commit b7b93f4c46

View file

@ -3,20 +3,20 @@ if (!existsSync('auth.json')) {
console.error('Missing auth.json! Run `npm login` to login and create this file by closing the opened browser.'); console.error('Missing auth.json! Run `npm login` to login and create this file by closing the opened browser.');
} }
const { chromium } = require('playwright'); const { chromium } = require('playwright'); // stealth plugin needs no outdated playwright-extra
(async () => { (async () => {
const browser = await chromium.launch({ const browser = await chromium.launch({
channel: 'chrome', channel: 'chrome',
headless: false, headless: true,
}); });
// https://github.com/berstend/puppeteer-extra/issues/454#issuecomment-917437212 // stealth with playwright: https://github.com/berstend/puppeteer-extra/issues/454#issuecomment-917437212
const originalUserAgent = await (await (await browser.newContext()).newPage()).evaluate(() => { return navigator.userAgent }); const originalUserAgent = await (await (await browser.newContext()).newPage()).evaluate(() => navigator.userAgent);
console.log(originalUserAgent); console.log('userAgent:', 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
const context = await browser.newContext({ const context = await browser.newContext({
storageState: 'auth.json', storageState: 'auth.json',
viewport: { width: 1280, height: 1280 }, viewport: { width: 1280, height: 1280 },
userAgent: originalUserAgent.replace("Headless", ""), userAgent: originalUserAgent.replace("Headless", ""), // HeadlessChrome -> Chrome
}); });
const enabledEvasions = [ const enabledEvasions = [
'chrome.app', 'chrome.app',
@ -37,30 +37,39 @@ const { chromium } = require('playwright');
]; ];
const evasions = enabledEvasions.map(e => new require(`puppeteer-extra-plugin-stealth/evasions/${e}`)); const evasions = enabledEvasions.map(e => new require(`puppeteer-extra-plugin-stealth/evasions/${e}`));
const stealth = { const stealth = {
callbacks: [], callbacks: [],
async evaluateOnNewDocument(...args) { async evaluateOnNewDocument(...args) {
this.callbacks.push({ cb: args[0], a: args[1] }) this.callbacks.push({ cb: args[0], a: args[1] })
} }
} }
evasions.forEach(e => e().onPageCreated(stealth)); evasions.forEach(e => e().onPageCreated(stealth));
for (let evasion of stealth.callbacks) { for (let evasion of stealth.callbacks) {
await context.addInitScript(evasion.cb, evasion.a); await context.addInitScript(evasion.cb, evasion.a);
} }
const page = await context.newPage(); const page = await context.newPage();
await page.goto('https://www.epicgames.com/store/en-US/free-games'); 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 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('button:has-text("Accept All Cookies")'); // to not waste screen space in --debug
// click on banner to go to current free game. TODO what if there are multiple games?
await page.click('[data-testid="offer-card-image-landscape"]'); await page.click('[data-testid="offer-card-image-landscape"]');
// TODO check if already claimed const game = await page.locator('h1 div').first().innerText();
await page.click('[data-testid="purchase-cta-button"]'); console.log('Current free game:', game);
await page.click('button:has-text("Continue")'); const btnText = await page.locator('[data-testid="purchase-cta-button"]').innerText();
// it then creates an iframe for the rest if (btnText.toLowerCase() == 'in library') {
// await page.frame({ url: /.*store\/purchase.*/ }).click('button:has-text("Place Order")'); // not found because it does not wait for iframe console.log('Already in library! Nothing to claim.');
const iframe = page.frameLocator('.webPurchaseContainer iframe') } else {
await iframe.locator('button:has-text("Place Order")').click(); await page.click('[data-testid="purchase-cta-button"]');
await iframe.locator('button:has-text("I Agree")').click(); await page.click('button:has-text("Continue")');
await page.pause(); // 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();
console.log(await page.locator('[data-testid="purchase-cta-button"]').innerText());
await page.pause();
// await context.waitForEvent("close");
}
await context.close(); await context.close();
await browser.close(); await browser.close();
})(); })();