Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const main = async () => {
// create a ad-blocker engine
const engine = await PuppeteerBlocker.fromLists(fetch, fullLists)
await writeFile(OUTPUT_FILENAME, engine.serialize())
}
'use strict'
const { PuppeteerBlocker } = require('@cliqz/adblocker-puppeteer')
const debug = require('debug-logfmt')('browserless:goto')
const createDevices = require('@browserless/devices')
const { getDomain } = require('tldts')
const pReflect = require('p-reflect')
const pTimeout = require('p-timeout')
const path = require('path')
const fs = require('fs')
const engine = PuppeteerBlocker.deserialize(
new Uint8Array(fs.readFileSync(path.resolve(__dirname, './engine.bin')))
)
engine.on('request-blocked', ({ url }) => debug('adblock:block', url))
engine.on('request-redirected', ({ url }) => debug('adblock:redirect', url))
const isEmpty = val => val == null || !(Object.keys(val) || val).length
const parseCookies = (url, str) => {
const domain = `.${getDomain(url)}`
return str.split(';').reduce((acc, str) => {
const [name, value] = str.split('=')
const cookie = {
name: name.trim(),
value,
domain,
(async () => {
const blocker = await PuppeteerBlocker.fromLists(fetch, fullLists, {
enableCompression: true,
});
const browser = await puppeteer.launch({
defaultViewport: null,
headless: false,
});
const page = await browser.newPage();
await blocker.enableBlockingInPage(page);
blocker.on('request-blocked', (request: Request) => {
console.log('blocked', request.url);
});
blocker.on('request-redirected', (request: Request) => {
console.log('redirected', request.url);
private async loadFromCache(): Promise {
if (!this.opts.useCache) {
throw new Error('caching disabled')
}
this.debug('load from cache', this.engineCacheFile)
return PuppeteerBlocker.deserialize(
new Uint8Array(await fs.readFile(this.engineCacheFile))
)
}
private async loadFromRemote(): Promise {
this.debug('load from remote', { blockTrackers: this.opts.blockTrackers })
if (this.opts.blockTrackers === true) {
return PuppeteerBlocker.fromPrebuiltAdsAndTracking(fetch)
} else {
return PuppeteerBlocker.fromPrebuiltAdsOnly(fetch)
}
}
private async loadFromRemote(): Promise {
this.debug('load from remote', { blockTrackers: this.opts.blockTrackers })
if (this.opts.blockTrackers === true) {
return PuppeteerBlocker.fromPrebuiltAdsAndTracking(fetch)
} else {
return PuppeteerBlocker.fromPrebuiltAdsOnly(fetch)
}
}