Clean up Sonar issues and lint warnings
Some checks failed
build-and-push / lint (push) Failing after 5s
build-and-push / sonar (push) Has been skipped
build-and-push / docker (push) Has been skipped

This commit is contained in:
nocci 2025-12-30 16:45:17 +00:00
parent b9aa6e0073
commit 7ffc454e47
6 changed files with 124 additions and 89 deletions

View file

@ -48,9 +48,18 @@ if (!cfg.debug) context.setDefaultTimeout(cfg.timeout);
const page = context.pages().length ? context.pages()[0] : await context.newPage(); // should always exist
await page.setViewportSize({ width: cfg.width, height: cfg.height }); // workaround for https://github.com/vogler/free-games-claimer/issues/277 until Playwright fixes it
// some debug info about the page (screen dimensions, user agent, platform)
// eslint-disable-next-line no-undef
if (cfg.debug) console.debug(await page.evaluate(() => [(({ width, height, availWidth, availHeight }) => ({ width, height, availWidth, availHeight }))(window.screen), navigator.userAgent, navigator.platform, navigator.vendor])); // deconstruct screen needed since `window.screen` prints {}, `window.screen.toString()` '[object Screen]', and can't use some pick function without defining it on `page`
// some debug info about the page (screen dimensions, user agent)
if (cfg.debug) {
// eslint-disable-next-line no-undef
const debugInfo = await page.evaluate(() => {
const { width, height, availWidth, availHeight } = window.screen;
return {
screen: { width, height, availWidth, availHeight },
userAgent: navigator.userAgent,
};
});
console.debug(debugInfo);
}
if (cfg.debug_network) {
// const filter = _ => true;
const filter = r => r.url().includes('store.epicgames.com');
@ -90,9 +99,8 @@ try {
}
};
const email = cfg.eg_email || await prompt({ message: 'Enter email' });
if (!email) await notifyBrowserLogin();
else {
void (async () => {
if (email) {
const watchCaptchaChallenge = async () => {
try {
await page.waitForSelector('.h_captcha_challenge iframe', { timeout: 15000 });
console.error('Got a captcha during login (likely due to too many attempts)! You may solve it in the browser, get a new IP or try again in a few hours.');
@ -100,24 +108,25 @@ try {
} catch {
return;
}
})();
void (async () => {
};
const watchCaptchaIncorrect = async () => {
try {
await page.waitForSelector('p:has-text("Incorrect response.")', { timeout: 15000 });
console.error('Incorrect response for captcha!');
} catch {
return;
}
})();
};
watchCaptchaChallenge();
watchCaptchaIncorrect();
await page.fill('#email', email);
const password = email && (cfg.eg_password || await prompt({ type: 'password', message: 'Enter password' }));
if (!password) await notifyBrowserLogin();
else {
const password = cfg.eg_password || await prompt({ type: 'password', message: 'Enter password' });
if (password) {
await page.fill('#password', password);
await page.click('button[type="submit"]');
}
} else await notifyBrowserLogin();
const error = page.locator('#form-error-message');
void (async () => {
const watchLoginError = async () => {
try {
await error.waitFor({ timeout: 15000 });
console.error('Login error:', await error.innerText());
@ -125,8 +134,8 @@ try {
} catch {
return;
}
})();
void (async () => {
};
const watchMfaStep = async () => {
try {
await page.waitForURL('**/id/login/mfa**', { timeout: cfg.login_timeout });
console.log('Enter the security code to continue - This appears to be a new device, browser or location. A security code has been sent to your email address at ...');
@ -136,8 +145,10 @@ try {
} catch {
return;
}
})();
}
};
watchLoginError();
watchMfaStep();
} else await notifyBrowserLogin();
await page.waitForURL(URL_CLAIM);
if (!cfg.debug) context.setDefaultTimeout(cfg.timeout);
}
@ -223,14 +234,13 @@ try {
console.log(' Base game:', baseUrl);
// await page.click('a:has-text("Overview")');
// re-add original add-on to queue after base game
urls.push(baseUrl); // add base game to the list of games to claim
urls.push(url); // add add-on itself again
urls.push(baseUrl, url); // add base game to the list of games to claim and re-add add-on itself
} else { // GET
console.log(' Not in library yet! Click', btnText);
await purchaseBtn.click({ delay: 11 }); // got stuck here without delay (or mouse move), see #75, 1ms was also enough
// Accept End User License Agreement (only needed once)
void (async () => {
const acceptEulaIfShown = async () => {
try {
await page.locator(':has-text("end user license agreement")').waitFor({ timeout: 10000 });
console.log(' Accept End User License Agreement (only needed once)');
@ -239,7 +249,8 @@ try {
} catch {
return;
}
})();
};
acceptEulaIfShown();
// it then creates an iframe for the purchase
await page.waitForSelector('#webPurchaseContainer iframe');
@ -252,7 +263,7 @@ try {
continue;
}
void (async () => {
const enterParentalPinIfNeeded = async () => {
try {
await iframe.locator('.payment-pin-code').waitFor({ timeout: 10000 });
if (!cfg.eg_parentalpin) {
@ -264,7 +275,8 @@ try {
} catch {
return;
}
})();
};
enterParentalPinIfNeeded();
if (cfg.debug) await page.pause();
if (cfg.dryrun) {
@ -279,18 +291,19 @@ try {
// I Agree button is only shown for EU accounts! https://github.com/vogler/free-games-claimer/pull/7#issuecomment-1038964872
const btnAgree = iframe.locator('button:has-text("I Accept")');
void (async () => {
const acceptIfRequired = async () => {
try {
await btnAgree.waitFor({ timeout: 10000 });
await btnAgree.click();
} catch {
return;
}
})(); // EU: wait for and click 'I Agree'
}; // EU: wait for and click 'I Agree'
acceptIfRequired();
try {
// context.setDefaultTimeout(100 * 1000); // give time to solve captcha, iframe goes blank after 60s?
const captcha = iframe.locator('#h_captcha_challenge_checkout_free_prod iframe');
void (async () => {
const watchCaptchaChallenge = async () => {
try {
await captcha.waitFor({ timeout: 10000 });
console.error(' Got hcaptcha challenge! Lost trust due to too many login attempts? You can solve the captcha in the browser or get a new IP address.');
@ -298,8 +311,8 @@ try {
} catch {
return;
}
})(); // may time out if not shown
void (async () => {
}; // may time out if not shown
const watchCaptchaFailure = async () => {
try {
await iframe.locator('.payment__errors:has-text("Failed to challenge captcha, please try again later.")').waitFor({ timeout: 10000 });
console.error(' Failed to challenge captcha, please try again later.');
@ -307,7 +320,9 @@ try {
} catch {
return;
}
})();
};
watchCaptchaChallenge();
watchCaptchaFailure();
await page.locator('text=Thanks for your order!').waitFor({ state: 'attached' });
db.data[user][game_id].status = 'claimed';
db.data[user][game_id].time = datetime(); // claimed time overwrites failed/dryrun time