Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: avoid crash due to adblocker #284

Merged
merged 3 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions maxun-core/src/interpret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,21 @@ export default class Interpreter extends EventEmitter {

private async applyAdBlocker(page: Page): Promise<void> {
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> {
if (this.blocker) {
await this.blocker.disableBlockingInPage(page);
try {
await this.blocker.disableBlockingInPage(page);
} catch (err) {
this.log(`Ad-blocker operation failed:`, Level.ERROR);
}
}
}

Expand Down Expand Up @@ -662,7 +670,11 @@ export default class Interpreter extends EventEmitter {
const workflowCopy: Workflow = JSON.parse(JSON.stringify(workflow));

// 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[] = [];
let selectors: string[] = [];
let lastAction = null;
Expand Down
15 changes: 11 additions & 4 deletions server/src/browser-management/classes/RemoteBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,17 @@ export class RemoteBrowser {

await this.setupPageEventListeners(this.currentPage);

const blocker = await PlaywrightBlocker.fromLists(fetch, ['https://easylist.to/easylist/easylist.txt']);
await blocker.enableBlockingInPage(this.currentPage);
this.client = await this.currentPage.context().newCDPSession(this.currentPage);
await blocker.disableBlockingInPage(this.currentPage);
try {
const blocker = await PlaywrightBlocker.fromLists(fetch, ['https://easylist.to/easylist/easylist.txt']);
await blocker.enableBlockingInPage(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);
}
};

/**
Expand Down