Files
castorsrm/src/website/twitch.ts
Nguyễn Thế Hưng 6cf4096064 chore: fix running dist package in nodejs
So we have to add ".js" to the import name...
2025-04-13 13:29:36 +07:00

40 lines
1.6 KiB
TypeScript

import type { Page } from "patchright";
import logger from "../logger.js";
async function checkConsentButton(page: Page, spawnId: string = "unknown") {
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();
}
}
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"});
waitTime = 0;
continue;
}
await checkConsentButton(page, spawnId);
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}`);
}
}
export { keepAlive };