Skip to content

Instantly share code, notes, and snippets.

@meotimdihia
Created July 1, 2023 06:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meotimdihia/4f73b514f1912b7cb8f6bdbc28d1b19b to your computer and use it in GitHub Desktop.
Save meotimdihia/4f73b514f1912b7cb8f6bdbc28d1b19b to your computer and use it in GitHub Desktop.
import { firefox, chromium } from "playwright-extra"
import { Browser, Page, LaunchOptions, BrowserContext } from "playwright"
import { PlaywrightBlocker } from "@cliqz/adblocker-playwright"
import fetch from "cross-fetch" // required 'fetch'
import { promises as fs } from "fs" // used for caching
import os from "os"
import RecaptchaPlugin from "puppeteer-extra-plugin-recaptcha"
import StealthPlugin from "puppeteer-extra-plugin-stealth"
import { newInjectedContext } from "fingerprint-injector"
export default async function instance(
configs: {
adblock?: boolean
proxy?: boolean
persistent?: boolean
type?: "edge" | "firefox" | "chrome"
headless?: boolean
} = {
adblock: true,
proxy: true,
persistent: false,
type: "chrome",
headless: true
},
options: LaunchOptions = {}
): Promise<[BrowserContext | Browser, Page]> {
const {
adblock = true,
proxy = true,
type = "chrome",
persistent = false,
headless = true
} = configs
options = Object.assign(options, {
...(process.env.SERVER != "localhost" && proxy
? {
proxy: {
server: "http://localhost:8000"
}
}
: {}),
...(type == "edge" ? { channel: "msedge" } : {}),
...(type == "chrome"
? {
channel: "chrome",
...(headless
? { headless: false, args: [`--headless=new`] }
: { headless: false })
}
: {}),
headless: headless,
ignoreHTTPSErrors: true
})
if (type != "firefox") {
chromium.use(StealthPlugin())
// chromium.use(
// RecaptchaPlugin({
// provider: {
// id: "2captcha",
// token: "xxxxxxxx" // REPLACE THIS WITH YOUR OWN 2CAPTCHA API KEY ⚡
// },
// visualFeedback: true // colorize reCAPTCHAs (violet = detected, green = solved)
// })
// )
}
let page
if (persistent) {
const browser = await (type == "firefox"
? firefox
: chromium
).launchPersistentContext("/tmp/test-user-data-dir", options)
page = await browser.newPage()
if (adblock) {
PlaywrightBlocker.fromPrebuiltAdsAndTracking(fetch, {
path: os.tmpdir() + "/engine.bin",
read: fs.readFile,
write: fs.writeFile
}).then(blocker => {
blocker.enableBlockingInPage(page)
})
}
return [browser, page]
} else {
const browser = await (type == "firefox" ? firefox : chromium).launch(
options
)
const context = await newInjectedContext(browser, {
// Constraints for the generated fingerprint (optional)
fingerprintOptions: {
devices: ["desktop"],
operatingSystems: ["linux"]
},
// Playwright's newContext() options (optional, random example for illustration)
newContextOptions: {
geolocation: {
latitude: 51.50853,
longitude: -0.12574
}
}
})
page = await context.newPage()
if (adblock) {
PlaywrightBlocker.fromPrebuiltAdsAndTracking(fetch, {
path: os.tmpdir() + "/engine.bin",
read: fs.readFile,
write: fs.writeFile
}).then(blocker => {
blocker.enableBlockingInPage(page)
})
}
return [browser, page]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment