From b8f7068a873d47ccc3c657c2c82aa1bc718cc455 Mon Sep 17 00:00:00 2001 From: Ralf Vogler Date: Wed, 10 Jul 2024 13:27:48 +0200 Subject: [PATCH] pg: scrollUntilStable to wait in loop to load all games since one scroll may not be enough --- prime-gaming.js | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/prime-gaming.js b/prime-gaming.js index 5e3e283..eeface0 100644 --- a/prime-gaming.js +++ b/prime-gaming.js @@ -97,12 +97,25 @@ try { process.exit(1); } + const waitUntilStable = async (f, act) => { + let v; + while (true) { + const v2 = await f(); + if (v == v2) break; + v = v2; + await act(); + } + }; + const scrollUntilStable = async f => waitUntilStable(f, async () => { + await page.keyboard.press('End'); // scroll to bottom to show all games + await page.waitForLoadState('networkidle'); // wait for all games to be loaded + await page.waitForTimeout(2000); // TODO networkidle wasn't enough to load all already collected games + }); + await page.click('button[data-type="Game"]'); - await page.keyboard.press('End'); // scroll to bottom to show all games - await page.waitForLoadState('networkidle'); // wait for all games to be loaded - await page.waitForTimeout(2000); // TODO networkidle wasn't enough to load all already collected games const games = page.locator('div[data-a-target="offer-list-FGWP_FULL"]'); await games.waitFor(); + await scrollUntilStable(() => games.locator('.item-card__action').count()); 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([data-a-target="FGWPOffer"])').elementHandles(); @@ -324,8 +337,7 @@ try { if (notify_games.length) { // make screenshot of all games if something was claimed const p = screenshot(`${filenamify(datetime())}.png`); // await page.screenshot({ path: p, fullPage: true }); // fullPage does not make a difference since scroll not on body but on some element - await page.keyboard.press('End'); // scroll to bottom to show all games - await page.waitForTimeout(1000); // wait for fade in animation + await scrollUntilStable(() => games.locator('.item-card__action').count()); const viewportSize = page.viewportSize(); // current viewport size await page.setViewportSize({ ...viewportSize, height: 3000 }); // increase height, otherwise element screenshot is cut off at the top and bottom await games.screenshot({ path: p }); // screenshot of all claimed games @@ -339,17 +351,7 @@ try { await loot.waitFor(); process.stdout.write('Loading all DLCs on page...'); - let n1 = 0; - let n2 = 0; - do { - n1 = n2; - n2 = await loot.locator('[data-a-target="item-card"]').count(); - // console.log(n2); - process.stdout.write(` ${n2}`); - await page.keyboard.press('End'); // scroll to bottom to show all dlcs - await page.waitForLoadState('networkidle'); // did not wait for dlcs to be loaded - await page.waitForTimeout(1000); - } while (n2 > n1); + scrollUntilStable(() => loot.locator('[data-a-target="item-card"]').count()) console.log('\nNumber of already claimed DLC:', await loot.locator('p:has-text("Collected")').count());