Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frank-dspeed/b7b99f4d56b77f5f71877c35baa413e0 to your computer and use it in GitHub Desktop.
Save frank-dspeed/b7b99f4d56b77f5f71877c35baa413e0 to your computer and use it in GitHub Desktop.
Run puppeteer with prepend or custom args arrguments for docker and so on

Final Solution!

The following example exposes defaultArgs for the Chrome and Firefox Launcher via undocumented api's and uses the documented ignoreDefaultArgs: true option to supply fully costumized args: [] containing the defaults for the given browser launcher

import

import Puppeteer from 'puppeteer';

in case of firefox inside docker set product before running the next part: is not needed if you started Puppeteer with a Environment var

Puppeteer._productName = 'firefox'

finaly launch

Puppeteer.launch({
    // product: 'firefox', // if you want to use firefox do not forget the above 
    ignoreDefaultArgs: true, // use only our supplyed args: []
    executablePath: '/usr/bin/docker', // we want to run docker right?
    args: [
       // docker arguments
        'run', 
        '-p=9333:9333', 
        '-p=3000:3000',
        '-it',
        `${dockerImage}`,
        `/usr/bin/google-chrome`,
        // end docker arguments
        ...Puppeteer._launcher.defaultArgs({ 
                devtools: false, // devtools auto open needs headless false
                headless: true, // needs to be false if devtools auto open
                userDataDir // needs to get set here no where else is optional
         }),
         // Here comes what you would put into args in general
    ],
    pipe: true
}

How this works

Puppeteer._launcher is a getter that looks up _productName and returns the correct launcher which holds the defaultArgs Method.

_productName also gets set on Puppeteer.launch({ product: 'chrome'}) to the value of product

we avoid calling the internal Puppeteer._launcher.defaultArgs(options) call with the ignoreDefaultArgs: true option as we can filter the output our self if needed as we call it our self

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment