Real User-Agent is detectable with
Real screen size is also detectable with iframe, to prevent it I used preload() script.
OS: Windows 10
"puppeteer": 3.0.4
"puppeteer-extra": 3.1.9
"puppeteer-extra-plugin-stealth": 2.4.9
Code to reproduce:
const puppeteer = require('puppeteer');
const puppeteerExtraPluginStealth = require('puppeteer-extra-plugin-stealth');
const puppeteerExtraPluginUserAgentOverride = require('puppeteer-extra-plugin-stealth/evasions/user-agent-override');
const {PuppeteerExtra} = require('puppeteer-extra');
function preload(device) {
Object.defineProperty(navigator, 'platform', {
value: device.platform,
writable: true,
});
Object.defineProperty(navigator, 'userAgent', {
value: device.userAgent,
writable: true,
});
Object.defineProperty(screen, 'height', {
value: device.viewport.height,
writable: true,
});
Object.defineProperty(screen, 'width', {
value: device.viewport.width,
writable: true,
});
Object.defineProperty(window, 'devicePixelRatio', {
value: device.viewport.deviceScaleFactor,
writable: true,
});
}
const device = {
userAgent: 'Mozilla/5.0 (Macintosh)',
viewport: {
width: 1200,
height: 800,
deviceScaleFactor: 1,
isMobile: false,
hasTouch: false,
isLandscape: true,
},
locale: 'en-US,en;q=0.9',
platform: 'Macintosh',
};
(async () => {
try {
const pptr = new PuppeteerExtra(puppeteer);
const pluginStealth = puppeteerExtraPluginStealth();
pluginStealth.enabledEvasions.delete('user-agent-override'); // Remove this specific stealth plugin from the default set
pptr.use(pluginStealth);
const pluginUserAgentOverride = puppeteerExtraPluginUserAgentOverride({
userAgent: device.userAgent,
locale: device.locale,
platform: device.platform,
});
pptr.use(pluginUserAgentOverride);
const browser = await pptr.launch({
args: [
'--disable-features=site-per-process',
`--window-size=${device.viewport.width},${device.viewport.height}`,
],
headless: false,
defaultViewport: device.viewport,
});
const page = await browser.newPage();
await page.evaluateOnNewDocument(preload, device);
await page.goto('https://codepen.io/ukrexpo/pen/JjYverG');
} catch (err) {
console.error(err);
}
})();
@ukrexpo shouldn't platform be MacIntel instead?
Awesome! This just fixed a problem I was encountering on a particular site. Thanks @ukrexpo.
I also discovered that puppeteer reports a screen.colorDepth and screen.pixelDepth of 30 instead of the 24 I see on other desktop browsers (Windows and Mac). It sounds like there are a few devices that support 30-bit color, but theoretically the depth should match up with graphics card vendor reported by WebGL.
Additionally a plugin like this could consider changing window.outerHeight and screen.availHeight to account for an OS dock height and browser tabs + omnibar height. It gives away puppeteer quickly if the window.innerHeight doesn't make sense with the values for screen.height and screen.availHeight.
careful also with
document.documentElement.clientWidth
document.documentElement.clientHeight
it looks like chrome headless doesn't account for scrollbar size
regarding window.innerHeight / screen.availHeight
maybe be carefull with css media queries which could detect the real size ?
I think in more recent Chrome versions this might be required: --flag-switches-begin --disable-site-isolation-trials --flag-switches-end
Most helpful comment
I think in more recent Chrome versions this might be required:
--flag-switches-begin --disable-site-isolation-trials --flag-switches-end