pg: use patchright: elementHandles() broken, didn't test PG_REDEEM

This commit is contained in:
Ralf Vogler 2025-05-26 02:42:23 +02:00
parent 2d5f232ba5
commit 29576f0af0

View file

@ -1,7 +1,8 @@
import { firefox } from 'playwright-firefox'; // stealth plugin needs no outdated playwright-extra
// import { chromium } from 'playwright-chromium';
import { chromium } from 'patchright';
import { authenticator } from 'otplib';
import chalk from 'chalk';
import { resolve, jsonDb, datetime, stealth, filenamify, prompt, confirm, notify, html_game_list, handleSIGINT } from './src/util.js';
import { resolve, jsonDb, datetime, filenamify, prompt, confirm, notify, html_game_list, handleSIGINT } from './src/util.js';
import { cfg } from './src/config.js';
const screenshot = (...a) => resolve(cfg.dir.screenshots, 'prime-gaming', ...a);
@ -14,20 +15,20 @@ console.log(datetime(), 'started checking prime-gaming');
const db = await jsonDb('prime-gaming.json', {});
// https://playwright.dev/docs/auth#multi-factor-authentication
const context = await firefox.launchPersistentContext(cfg.dir.browser, {
const context = await chromium.launchPersistentContext(cfg.dir.browser, {
headless: cfg.headless,
viewport: { width: cfg.width, height: cfg.height },
locale: 'en-US', // ignore OS locale to be sure to have english text for locators
recordVideo: cfg.record ? { dir: 'data/record/', size: { width: cfg.width, height: cfg.height } } : undefined, // will record a .webm video for each page navigated; without size, video would be scaled down to fit 800x800
recordHar: cfg.record ? { path: `data/record/pg-${filenamify(datetime())}.har` } : undefined, // will record a HAR file with network requests and responses; can be imported in Chrome devtools
handleSIGINT: false, // have to handle ourselves and call context.close(), otherwise recordings from above won't be saved
args: [
'--hide-crash-restore-bubble',
],
});
handleSIGINT(context);
// TODO test if needed
await stealth(context);
if (!cfg.debug) context.setDefaultTimeout(cfg.timeout);
const page = context.pages().length ? context.pages()[0] : await context.newPage(); // should always exist
@ -127,7 +128,7 @@ try {
await scrollUntilStable(() => page.evaluate(() => document.querySelector('.tw-full-width').scrollHeight)); // height may change during loading while number of games is still the same?
console.log('Number of already claimed games (total):', await games.locator('p:has-text("Collected")').count());
// can't use .all() since the list of elements via locator will change after click while we iterate over it
const internal = await games.locator('.item-card__action:has(button[data-a-target="FGWPOffer"])').elementHandles();
const internal = await games.locator('.item-card__action:has(button[data-a-target="FGWPOffer"])').all();
const external = await games.locator('.item-card__action:has(a[data-a-target="FGWPOffer"])').all();
// bottom to top: oldest to newest games
internal.reverse();
@ -155,8 +156,8 @@ try {
// claim games in internal store
for (const card of internal) {
await card.scrollIntoViewIfNeeded();
const title = await (await card.$('.item-card-details__body__primary')).innerText();
const slug = await (await card.$('a')).getAttribute('href');
const title = await (await card.locator('.item-card-details__body__primary')).innerText();
const slug = await (await card.locator('a')).getAttribute('href');
const url = 'https://gaming.amazon.com' + slug.split('?')[0];
console.log('Current free game:', chalk.blue(title));
if (cfg.pg_timeLeft && await skipBasedOnTime(url)) continue;