pull out stealth into util
This commit is contained in:
parent
bd61963fc2
commit
9bb548dc3e
3 changed files with 46 additions and 64 deletions
37
util.js
37
util.js
|
|
@ -12,3 +12,40 @@ const newStealthContext = async (browser, contextOptions = {}) => {
|
|||
};
|
||||
}
|
||||
};
|
||||
|
||||
const stealth = async (context) => {
|
||||
// stealth with playwright: https://github.com/berstend/puppeteer-extra/issues/454#issuecomment-917437212
|
||||
// https://github.com/berstend/puppeteer-extra/tree/master/packages/puppeteer-extra-plugin-stealth/evasions
|
||||
const enabledEvasions = [
|
||||
'chrome.app',
|
||||
'chrome.csi',
|
||||
'chrome.loadTimes',
|
||||
'chrome.runtime',
|
||||
// 'defaultArgs',
|
||||
'iframe.contentWindow',
|
||||
'media.codecs',
|
||||
'navigator.hardwareConcurrency',
|
||||
'navigator.languages',
|
||||
'navigator.permissions',
|
||||
'navigator.plugins',
|
||||
// 'navigator.vendor',
|
||||
'navigator.webdriver',
|
||||
'sourceurl',
|
||||
// 'user-agent-override', // doesn't work since playwright has no page.browser()
|
||||
'webgl.vendor',
|
||||
'window.outerdimensions'
|
||||
];
|
||||
const evasions = enabledEvasions.map(e => require(`puppeteer-extra-plugin-stealth/evasions/${e}`));
|
||||
const stealth = {
|
||||
callbacks: [],
|
||||
async evaluateOnNewDocument(...args) {
|
||||
this.callbacks.push({ cb: args[0], a: args[1] })
|
||||
}
|
||||
}
|
||||
evasions.forEach(e => e().onPageCreated(stealth));
|
||||
for (let evasion of stealth.callbacks) {
|
||||
await context.addInitScript(evasion.cb, evasion.a);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { stealth };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue