Skip to content

Commit

Permalink
pg: scrollUntilStable to wait in loop to load all games since one scr…
Browse files Browse the repository at this point in the history
…oll may not be enough
  • Loading branch information
vogler committed Jul 10, 2024
1 parent 17f1ee4 commit b8f7068
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions prime-gaming.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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());

Expand Down

0 comments on commit b8f7068

Please sign in to comment.