Skip to content

Commit

Permalink
fix(playwright): only create adblocker if flag is true
Browse files Browse the repository at this point in the history
  • Loading branch information
nsarrazin committed Jul 12, 2024
1 parent d67b280 commit df67a2c
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/lib/server/websearch/scrape/playwright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ import { env } from "$env/dynamic/private";
import { logger } from "$lib/server/logger";
import { onExit } from "$lib/server/exitHandler";

const blocker = await PlaywrightBlocker.fromPrebuiltAdsAndTracking(fetch)
.then((blker) => {
const mostBlocked = blker.blockFonts().blockMedias().blockFrames().blockImages();
if (env.WEBSEARCH_JAVASCRIPT === "false") return mostBlocked.blockScripts();
return mostBlocked;
})
.catch((err) => {
logger.error(err, "Failed to initialize PlaywrightBlocker from prebuilt lists");
return PlaywrightBlocker.empty();
});
const blocker =
env.PLAYWRIGHT_ADBLOCKER === "true"
? await PlaywrightBlocker.fromPrebuiltAdsAndTracking(fetch)
.then((blker) => {
const mostBlocked = blker.blockFonts().blockMedias().blockFrames().blockImages();
if (env.WEBSEARCH_JAVASCRIPT === "false") return mostBlocked.blockScripts();
return mostBlocked;
})
.catch((err) => {
logger.error(err, "Failed to initialize PlaywrightBlocker from prebuilt lists");
return PlaywrightBlocker.empty();
})
: PlaywrightBlocker.empty();

let browserSingleton: Promise<Browser> | undefined;
async function getBrowser() {
Expand Down Expand Up @@ -65,7 +68,7 @@ export async function withPage<T>(

try {
const page = await ctx.newPage();
process.env.PLAYWRIGHT_ADBLOCKER === "true" && (await blocker.enableBlockingInPage(page));
env.PLAYWRIGHT_ADBLOCKER === "true" && (await blocker.enableBlockingInPage(page));

const res = await page.goto(url, { waitUntil: "load", timeout: 3500 }).catch(() => {
console.warn(`Failed to load page within 2s: ${url}`);
Expand Down

0 comments on commit df67a2c

Please sign in to comment.