feat: random device resolution & accept cookies
This commit is contained in:
@ -1,4 +1,4 @@
|
|||||||
import { type BrowserContext, type Locator } from "patchright";
|
import { devices, type BrowserContext, type Locator } from "patchright";
|
||||||
import logger from "../logger";
|
import logger from "../logger";
|
||||||
import * as twitch from "../website/twitch";
|
import * as twitch from "../website/twitch";
|
||||||
import * as constants from "../constants";
|
import * as constants from "../constants";
|
||||||
@ -7,7 +7,10 @@ async function spawn(context: BrowserContext, targetUrl: string, spawnId: string
|
|||||||
const server = constants.REFLECT4_SERVERS[Math.floor(Math.random() * constants.REFLECT4_SERVERS.length)];
|
const server = constants.REFLECT4_SERVERS[Math.floor(Math.random() * constants.REFLECT4_SERVERS.length)];
|
||||||
logger.debug(`[${spawnId}] Using reflect4 server: ${server}`);
|
logger.debug(`[${spawnId}] Using reflect4 server: ${server}`);
|
||||||
const page = await context.newPage();
|
const page = await context.newPage();
|
||||||
await page.setViewportSize({width: 640, height: 360});
|
const deviceName = Object.keys(devices)[Math.floor(Math.random() * Object.keys(devices).length)];
|
||||||
|
const device = devices[deviceName];
|
||||||
|
logger.debug(`[${spawnId}] Using device: ${deviceName}`);
|
||||||
|
await page.setViewportSize(device.viewport);
|
||||||
try {
|
try {
|
||||||
await page.goto(server);
|
await page.goto(server);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -33,6 +36,8 @@ async function spawn(context: BrowserContext, targetUrl: string, spawnId: string
|
|||||||
await page.waitForTimeout(15000); // Wait for 15 second to let the page load
|
await page.waitForTimeout(15000); // Wait for 15 second to let the page load
|
||||||
// Keep-alive the page open for 5 minutes then refresh
|
// Keep-alive the page open for 5 minutes then refresh
|
||||||
if (targetUrl.startsWith("https://www.twitch.tv/")) {
|
if (targetUrl.startsWith("https://www.twitch.tv/")) {
|
||||||
|
logger.info(`[${spawnId}] Twitch URL detected, initializing...`);
|
||||||
|
await twitch.init(page, spawnId);
|
||||||
await twitch.keepAlive(page, spawnId);
|
await twitch.keepAlive(page, spawnId);
|
||||||
} else {
|
} else {
|
||||||
logger.warn(`[${spawnId}] Unsupported URL: ${targetUrl}`);
|
logger.warn(`[${spawnId}] Unsupported URL: ${targetUrl}`);
|
||||||
|
|||||||
@ -1,6 +1,14 @@
|
|||||||
import type { Page } from "patchright";
|
import type { Page } from "patchright";
|
||||||
import logger from "../logger";
|
import logger from "../logger";
|
||||||
|
|
||||||
|
async function init(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") {
|
async function keepAlive(page: Page, spawnId: string = "unknown") {
|
||||||
try {
|
try {
|
||||||
let waitTime = 0;
|
let waitTime = 0;
|
||||||
@ -25,4 +33,4 @@ async function keepAlive(page: Page, spawnId: string = "unknown") {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { keepAlive };
|
export { init, keepAlive };
|
||||||
|
|||||||
Reference in New Issue
Block a user