2025-04-12 19:38:54 +07:00
|
|
|
import type { Page } from "patchright";
|
2025-04-13 13:29:36 +07:00
|
|
|
import logger from "../logger.js";
|
2025-04-12 19:38:54 +07:00
|
|
|
|
2025-04-13 00:34:57 +07:00
|
|
|
async function checkConsentButton(page: Page, spawnId: string = "unknown") {
|
2025-04-13 00:31:57 +07:00
|
|
|
const consentButton = page.locator("button[data-a-target='consent-banner-accept']");
|
|
|
|
|
if ((await consentButton.all()).length > 0) {
|
|
|
|
|
logger.debug(`[${spawnId}] Consent button found, clicking it...`);
|
|
|
|
|
await consentButton.click();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-12 19:38:54 +07:00
|
|
|
async function keepAlive(page: Page, spawnId: string = "unknown") {
|
|
|
|
|
try {
|
|
|
|
|
let waitTime = 0;
|
|
|
|
|
while (true) {
|
|
|
|
|
// Wait for a random time between 1 and 11 seconds
|
|
|
|
|
const timeout = 1000 + Math.floor(Math.random() * 60 * 1000) % 10000;
|
|
|
|
|
logger.debug(`[${spawnId}] Waiting for ${timeout / 1000} seconds...`);
|
|
|
|
|
await page.waitForTimeout(timeout);
|
|
|
|
|
waitTime += timeout;
|
|
|
|
|
if ((await page.locator(".ScCoreButton-sc-ocjdkq-0.ggPgVz").all()).length > 0) {
|
|
|
|
|
logger.debug(`[${spawnId}] Player encountered an error, refreshing the page...`);
|
|
|
|
|
await page.reload({timeout: 0, waitUntil: "domcontentloaded"});
|
2025-04-13 00:34:57 +07:00
|
|
|
waitTime = 0;
|
|
|
|
|
continue;
|
2025-04-12 19:38:54 +07:00
|
|
|
}
|
2025-04-13 00:34:57 +07:00
|
|
|
await checkConsentButton(page, spawnId);
|
2025-04-12 19:38:54 +07:00
|
|
|
if (waitTime > 5 * 60 * 1000) {
|
|
|
|
|
logger.debug(`[${spawnId}] Waited for more than 5 minutes, refreshing the page...`);
|
|
|
|
|
await page.reload({timeout: 0, waitUntil: "domcontentloaded"});
|
|
|
|
|
waitTime = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
logger.error(`[${spawnId}] Error while keeping the page alive: ${e}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-13 00:34:57 +07:00
|
|
|
export { keepAlive };
|