diff --git a/epic-claimer-new.js b/epic-claimer-new.js index 54180c0..5f741cf 100644 --- a/epic-claimer-new.js +++ b/epic-claimer-new.js @@ -104,9 +104,14 @@ const claimGame = async (page, game) => { // Check if logged in const isLoggedIn = async page => { try { + // Wait for egs-navigation element to be present + await page.locator('egs-navigation').waitFor({ state: 'attached', timeout: 5000 }); const attr = await page.locator('egs-navigation').getAttribute('isloggedin'); - return attr === 'true'; - } catch { + const isLogged = attr === 'true'; + L.trace({ isLogged, attr }, 'Login status check'); + return isLogged; + } catch (err) { + L.trace({ err: err.message }, 'Login status check failed'); return false; } }; @@ -206,11 +211,29 @@ const attemptBrowserLogin = async (page, context) => { // Wait for successful login try { + L.trace('Waiting for navigation to free-games page'); await page.waitForURL('**/free-games**', { timeout: cfg.login_timeout }); - L.info('Login successful'); - return await isLoggedIn(page); + + // Give page time to fully load and egs-navigation to update + L.trace('Waiting for page to stabilize'); + await page.waitForTimeout(3000); + + // Check multiple times to ensure stable login state + for (let i = 0; i < 3; i++) { + const logged = await isLoggedIn(page); + if (logged) { + L.info('Login confirmed'); + return true; + } + L.trace({ attempt: i + 1 }, 'Login not yet confirmed, retrying'); + await page.waitForTimeout(2000); + } + + L.warn('Login URL reached but login status not confirmed'); + return false; } catch (err) { L.warn({ err: err.message }, 'Login URL timeout, checking if logged in anyway'); + await page.waitForTimeout(3000); return await isLoggedIn(page); } } catch (err) { @@ -261,13 +284,26 @@ const ensureLoggedIn = async (page, context) => { const maxWait = cfg.login_timeout; const checkInterval = 5000; let waited = 0; + let loginConfirmed = false; while (waited < maxWait) { await page.waitForTimeout(checkInterval); waited += checkInterval; - if (await isLoggedIn(page)) { - L.info('Manual login detected'); + // Check multiple times for stable state + for (let i = 0; i < 2; i++) { + if (await isLoggedIn(page)) { + // Confirm it's stable + await page.waitForTimeout(2000); + if (await isLoggedIn(page)) { + loginConfirmed = true; + break; + } + } + } + + if (loginConfirmed) { + L.info('Manual login detected and confirmed'); console.log('✅ Manual login detected!'); break; } @@ -279,7 +315,7 @@ const ensureLoggedIn = async (page, context) => { } } - if (!await isLoggedIn(page)) { + if (!loginConfirmed && !await isLoggedIn(page)) { throw new Error('Manual login did not complete within timeout'); } }