puppeteer-extra-plugin-stealth
Version:
Stealth mode: Applies various techniques to make detection of headless puppeteer harder.
50 lines (41 loc) • 1.16 kB
JavaScript
const { PuppeteerExtraPlugin } = require('puppeteer-extra-plugin')
const withUtils = require('../_utils/withUtils')
/**
* Set the hardwareConcurrency to 4 (optionally configurable with `hardwareConcurrency`)
*
* @see https://arh.antoinevastel.com/reports/stats/osName_hardwareConcurrency_report.html
*
* @param {Object} [opts] - Options
* @param {number} [opts.hardwareConcurrency] - The value to use in `navigator.hardwareConcurrency` (default: `4`)
*/
class Plugin extends PuppeteerExtraPlugin {
constructor(opts = {}) {
super(opts)
}
get name() {
return 'stealth/evasions/navigator.hardwareConcurrency'
}
get defaults() {
return {
hardwareConcurrency: 4
}
}
async onPageCreated(page) {
await withUtils(page).evaluateOnNewDocument(
(utils, { opts }) => {
utils.replaceGetterWithProxy(
Object.getPrototypeOf(navigator),
'hardwareConcurrency',
utils.makeHandler().getterValue(opts.hardwareConcurrency)
)
},
{
opts: this.opts
}
)
}
}
module.exports = function (pluginConfig) {
return new Plugin(pluginConfig)
}