Ad Blocking
Hyperbrowser's browser instances can automatically block ads and trackers. This improves page load times and further reduces detection risk. In addition to ads, Hyperbrowser allows you to block trackers and other annoyances including cookie notices.
To enable ad blocking, set the proper configurations in the session create parameters.
import { connect } from "puppeteer-core";
import { Hyperbrowser } from "@hyperbrowser/sdk";
import { config } from "dotenv";
config();
const client = new Hyperbrowser({
apiKey: process.env.HYPERBROWSER_API_KEY,
});
const main = async () => {
const session = await client.sessions.create({
adblock: true,
});
try {
const browser = await connect({
browserWSEndpoint: session.wsEndpoint,
defaultViewport: null,
});
const [page] = await browser.pages();
// Navigate to a website
console.log("Navigating to Hacker News...");
await page.goto("https://news.ycombinator.com/");
const pageTitle = await page.title();
console.log("Page 1:", pageTitle);
await page.evaluate(() => {
console.log("Page 1:", document.title);
});
} catch (err) {
console.error(`Encountered error: ${err}`);
} finally {
await client.sessions.stop(session.id);
}
};
main();
Trackers and Annoyances
Hyperbrowser can block some trackers automatically if the options are selected when creating a session. In addition, Hyperbrowser can also filter out some popups and cookie prompts if the the options are selected.
...
await client.sessions.create({
adblock: true,
trackers: true,
annoyances: true,
// You must have trackers set to true to enable blocking annoyances and
// adblock set to true to enable blocking trackers.
});
...
Automatically Accept cookies
Some site prompt users for cookies in a particularly intrusive way for scraping. If the acceptCookies
param is set, then Hyperbrowser will automatically accept cookies on the browsers behalf.
await client.sessions.create({
acceptCookies: true
});
Last updated