Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Basilakis/30d1ce8e3412d58f9e1ef5f42a3fc7f9 to your computer and use it in GitHub Desktop.
Save Basilakis/30d1ce8e3412d58f9e1ef5f42a3fc7f9 to your computer and use it in GitHub Desktop.

Revisions

  1. @tegansnyder tegansnyder created this gist Feb 23, 2018.
    52 changes: 52 additions & 0 deletions Preventing-Puppeteer-Detection.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    I’m looking for any tips or tricks for making chrome headless mode less detectable. Here is what I’ve done so far:

    Set my args as follows:
    ```js
    const run = (async () => {

    const args = [
    '--no-sandbox',
    '--disable-setuid-sandbox',
    '--disable-infobars',
    '--window-position=0,0',
    '--ignore-certifcate-errors',
    '--ignore-certifcate-errors-spki-list',
    '--user-agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3312.0 Safari/537.36"'
    ];

    const options = {
    args,
    headless: true,
    ignoreHTTPSErrors: true,
    userDataDir: './tmp'
    };

    const browser = await puppeteer.launch(options);
    ```
    I’m loading in a preload file that overrides some window.navigator globals:
    ```js
    const preloadFile = fs.readFileSync('./preload.js', 'utf8');
    await page.evaluateOnNewDocument(preloadFile);
    ```
    ##### preload.js
    ```js
    // overwrite the `languages` property to use a custom getter
    Object.defineProperty(navigator, "languages", {
    get: function() {
    return ["en-US", "en"];
    };
    });

    // overwrite the `plugins` property to use a custom getter
    Object.defineProperty(navigator, 'plugins', {
    get: function() {
    // this just needs to have `length > 0`, but we could mock the plugins too
    return [1, 2, 3, 4, 5];
    },
    });
    ```
    I see there are some other things suggested here https://intoli.com/blog/making-chrome-headless-undetectable/ but I'm not 100% certain how to implement them in puppeteer. Any ideas tips or tricks?