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();
import { chromium } from "playwright-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 chromium.connectOverCDP(session.wsEndpoint);
const context = browser.contexts()[0];
const page = await context.newPage();
// 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();
import asyncio
from pyppeteer import connect
import os
from dotenv import load_dotenv
from hyperbrowser import AsyncHyperbrowser
from hyperbrowser.models import CreateSessionParams
# Load environment variables from .env file
load_dotenv()
client = AsyncHyperbrowser(api_key=os.getenv("HYPERBROWSER_API_KEY"))
async def main():
# Create a session and connect to it using Pyppeteer
session = await client.sessions.create(
params=CreateSessionParams(
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.
)
)
try:
browser = await connect(
browserWSEndpoint=session.ws_endpoint, defaultViewport=None
)
pages = await browser.pages()
page = pages[0]
# Navigate to a website
print("Navigating to Hacker News...")
await page.goto("https://news.ycombinator.com/")
page_title = await page.title()
print("Page title:", page_title)
await page.evaluate("() => { console.log('Page 1:', document.title); }")
except Exception as e:
print(f"Error: {e}")
finally:
await client.sessions.stop(session.id)
# Run the async main function
if __name__ == "__main__":
asyncio.run(main())
import asyncio
from playwright.async_api import async_playwright
import os
from dotenv import load_dotenv
from hyperbrowser import AsyncHyperbrowser
from hyperbrowser.models import CreateSessionParams
# Load environment variables from .env file
load_dotenv()
client = AsyncHyperbrowser(api_key=os.getenv("HYPERBROWSER_API_KEY"))
async def main():
# Create a session and connect to it using Pyppeteer
session = await client.sessions.create(
params=CreateSessionParams(
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.
)
)
try:
async with async_playwright() as p:
browser = await p.chromium.connect_over_cdp(session.ws_endpoint)
default_context = browser.contexts[0]
page = await default_context.new_page()
# Navigate to a website
print("Navigating to Hacker News...")
await page.goto("https://news.ycombinator.com/")
page_title = await page.title()
print("Page title:", page_title)
await page.evaluate("() => { console.log('Page 1:', document.title); }")
except Exception as e:
print(f"Error: {e}")
finally:
await client.sessions.stop(session.id)
# Run the async main function
if __name__ == "__main__":
asyncio.run(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.
});
...
...
await client.sessions.create(
params=CreateSessionParams(
adblock=True,
trackers=True,
annoyances=True,
)
)
...
To enable trackers blocking, adblock must be enabled.
To enable annoyance blocking, adblock and trackers must be enabled.
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