From b7b93f4c46238f54ec08461cfd8234cc725410cf Mon Sep 17 00:00:00 2001 From: Ralf Vogler Date: Tue, 28 Dec 2021 01:47:38 +0100 Subject: [PATCH] check if game already claimed --- main.stealth.js | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/main.stealth.js b/main.stealth.js index 0c2a3d5..d7931af 100644 --- a/main.stealth.js +++ b/main.stealth.js @@ -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.'); } -const { chromium } = require('playwright'); +const { chromium } = require('playwright'); // stealth plugin needs no outdated playwright-extra (async () => { const browser = await chromium.launch({ channel: 'chrome', - headless: false, + headless: true, }); - // 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); + // stealth with playwright: https://github.com/berstend/puppeteer-extra/issues/454#issuecomment-917437212 + const originalUserAgent = await (await (await browser.newContext()).newPage()).evaluate(() => navigator.userAgent); + 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({ storageState: 'auth.json', viewport: { width: 1280, height: 1280 }, - userAgent: originalUserAgent.replace("Headless", ""), + userAgent: originalUserAgent.replace("Headless", ""), // HeadlessChrome -> Chrome }); const enabledEvasions = [ 'chrome.app', @@ -37,30 +37,39 @@ const { chromium } = require('playwright'); ]; 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] }) - } + 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); + 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 + // 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"]'); - // 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(); + const game = await page.locator('h1 div').first().innerText(); + console.log('Current free game:', game); + 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 { + 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(); + console.log(await page.locator('[data-testid="purchase-cta-button"]').innerText()); + await page.pause(); + // await context.waitForEvent("close"); + } await context.close(); await browser.close(); })();