From f0bf3c167147514077c723eec3b256de4d550988 Mon Sep 17 00:00:00 2001 From: Ralf Vogler Date: Thu, 31 Mar 2022 19:41:42 +0200 Subject: [PATCH] prime-gaming: fix wait for sign in, exit if not, arg show otherwise headless --- prime-gaming.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/prime-gaming.js b/prime-gaming.js index c9c630b..8a5801a 100644 --- a/prime-gaming.js +++ b/prime-gaming.js @@ -3,6 +3,8 @@ import path from 'path'; import { __dirname, stealth } from './util.js'; const debug = process.env.PWDEBUG == '1'; // runs headful and opens https://playwright.dev/docs/inspector +const show = process.argv.includes('show', 2); +const headless = !debug && !show; // const URL_LOGIN = 'https://www.amazon.de/ap/signin'; // wrong. needs some session args to be valid? const URL_CLAIM = 'https://gaming.amazon.com/home'; @@ -10,8 +12,8 @@ const TIMEOUT = 20 * 1000; // 20s, default is 30s // https://playwright.dev/docs/auth#multi-factor-authentication const context = await chromium.launchPersistentContext(path.resolve(__dirname, 'userDataDir'), { - channel: 'chrome', // https://playwright.dev/docs/browsers#google-chrome--microsoft-edge - headless: false, + // channel: 'chrome', // https://playwright.dev/docs/browsers#google-chrome--microsoft-edge, chrome will not work on arm64 linux, only chromium which is the default + headless, 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! locale: "en-US", // ignore OS locale to be sure to have english text for locators @@ -32,9 +34,15 @@ const clickIfExists = async selector => { }; await page.goto(URL_CLAIM, {waitUntil: 'domcontentloaded'}); // default 'load' takes forever +await Promise.any(['button:has-text("Sign in")', '[data-a-target="user-dropdown-first-name-text"]'].map(s => page.waitForSelector(s))); await clickIfExists('[aria-label="Cookies usage disclaimer banner"] button:has-text("Accept Cookies")'); // to not waste screen space in --debug while (await page.locator('button:has-text("Sign in")').count() > 0) { - console.error("Not signed in anymore. Please login and then navigate to the 'Free Games' page."); + console.error('Not signed in anymore.'); + if (headless) { + console.log('Please run `node prime-gaming show` to login in the opened browser.'); + await context.close(); // not needed? + process.exit(1); + } await page.click('button:has-text("Sign in")'); if (!debug) context.setDefaultTimeout(0); // give user time to log in without timeout await page.waitForNavigation({url: 'https://gaming.amazon.com/home?signedIn=true'});