Merge pull request #284 from getmaxun/adblock-fix-china

feat: avoid crash due to adblocker
This commit is contained in:
Karishma
2024-12-23 00:06:17 +05:30
committed by GitHub
2 changed files with 26 additions and 7 deletions

View File

@@ -111,13 +111,21 @@ export default class Interpreter extends EventEmitter {
private async applyAdBlocker(page: Page): Promise<void> { private async applyAdBlocker(page: Page): Promise<void> {
if (this.blocker) { if (this.blocker) {
await this.blocker.enableBlockingInPage(page); try {
await this.blocker.enableBlockingInPage(page);
} catch (err) {
this.log(`Ad-blocker operation failed:`, Level.ERROR);
}
} }
} }
private async disableAdBlocker(page: Page): Promise<void> { private async disableAdBlocker(page: Page): Promise<void> {
if (this.blocker) { if (this.blocker) {
await this.blocker.disableBlockingInPage(page); try {
await this.blocker.disableBlockingInPage(page);
} catch (err) {
this.log(`Ad-blocker operation failed:`, Level.ERROR);
}
} }
} }
@@ -662,7 +670,11 @@ export default class Interpreter extends EventEmitter {
const workflowCopy: Workflow = JSON.parse(JSON.stringify(workflow)); const workflowCopy: Workflow = JSON.parse(JSON.stringify(workflow));
// apply ad-blocker to the current page // apply ad-blocker to the current page
await this.applyAdBlocker(p); try {
await this.applyAdBlocker(p);
} catch (error) {
this.log(`Failed to apply ad-blocker: ${error.message}`, Level.ERROR);
}
const usedActions: string[] = []; const usedActions: string[] = [];
let selectors: string[] = []; let selectors: string[] = [];
let lastAction = null; let lastAction = null;

View File

@@ -245,10 +245,17 @@ export class RemoteBrowser {
await this.setupPageEventListeners(this.currentPage); await this.setupPageEventListeners(this.currentPage);
const blocker = await PlaywrightBlocker.fromLists(fetch, ['https://easylist.to/easylist/easylist.txt']); try {
await blocker.enableBlockingInPage(this.currentPage); const blocker = await PlaywrightBlocker.fromLists(fetch, ['https://easylist.to/easylist/easylist.txt']);
this.client = await this.currentPage.context().newCDPSession(this.currentPage); await blocker.enableBlockingInPage(this.currentPage);
await blocker.disableBlockingInPage(this.currentPage); this.client = await this.currentPage.context().newCDPSession(this.currentPage);
await blocker.disableBlockingInPage(this.currentPage);
console.log('Adblocker initialized');
} catch (error: any) {
console.warn('Failed to initialize adblocker, continuing without it:', error.message);
// Still need to set up the CDP session even if blocker fails
this.client = await this.currentPage.context().newCDPSession(this.currentPage);
}
}; };
/** /**