andyearnshaw / docketeer

A tiny application that lets you use a dockerised browser for Puppeteer
The Unlicense
3 stars 1 forks source link

Evaluate use of shell script as an alternative launcher #7

Closed andyearnshaw closed 1 year ago

andyearnshaw commented 2 years ago

@frank-dspeed suggested this solution for tackling some of the issues:

const os = require('os');
const path = require('path');
const fs = require('fs');

const dockerImage = 'browserless/chrome:latest'
const dockerExecutablePath = '/usr/bin/google-chrome'
const executablePath = path.join(__dirname, 'Chrome-Launcher');

const docker = `#!/bin/bash
docker run -p=9333:9333 -p=3000:3000 ${dockerImage} ${dockerExecutablePath} \${@}`

const debugBinary = `#!/bin/bash
echo "\${@}" > ${executablePath}.log.txt`

fs.writeFileSync(executablePath, docker);
fs.chmodSync(executablePath,'777');

const puppeteer = require('puppeteer');
const browser = puppeteer.launch({ 
    executablePath,
    userDataDir: './',
    ignoreDefaultArgs: [
        '--remote-debugging-port=0',
    ],
    args: [
        "--remote-debugging-address=0.0.0.0",
        "--no-sandbox",
        "--headless",
        "--disable-gpu",
        "--disable-software-rasterizer",
       "--remote-debugging-pipe"
    ]
});
frank-dspeed commented 2 years ago

this is not even needed my newer solution shows that you can directly use the docker binary as replacement for chrome you only need to ignore all default arguments and supply your own.

but the shell version is working always even if the default parameters would change you need to choose the design for your solution but i guess at the end it is the most best to ignore all puppeteer defaults and supply then own arguments

i will raise a PR in puppeteer to allow connecting a existing child.

frank-dspeed commented 2 years ago

node_modules/puppeteer/lib/cjs/puppeteer/node/Launcher.js#L141

holds

defaultArgs(options = {}) {
        const chromeArguments = [
            '--allow-pre-commit-input',
            '--disable-background-networking',
            '--enable-features=NetworkService,NetworkServiceInProcess',
            '--disable-background-timer-throttling',
            '--disable-backgrounding-occluded-windows',
            '--disable-breakpad',
            '--disable-client-side-phishing-detection',
            '--disable-component-extensions-with-background-pages',
            '--disable-default-apps',
            '--disable-dev-shm-usage',
            '--disable-extensions',
            // TODO: remove AvoidUnnecessaryBeforeUnloadCheckSync below
            // once crbug.com/1324138 is fixed and released.
            '--disable-features=Translate,BackForwardCache,AvoidUnnecessaryBeforeUnloadCheckSync',
            '--disable-hang-monitor',
            '--disable-ipc-flooding-protection',
            '--disable-popup-blocking',
            '--disable-prompt-on-repost',
            '--disable-renderer-backgrounding',
            '--disable-sync',
            '--force-color-profile=srgb',
            '--metrics-recording-only',
            '--no-first-run',
            '--enable-automation',
            '--password-store=basic',
            '--use-mock-keychain',
            // TODO(sadym): remove '--enable-blink-features=IdleDetection'
            // once IdleDetection is turned on by default.
            '--enable-blink-features=IdleDetection',
            '--export-tagged-pdf',
        ];
frank-dspeed commented 2 years ago

Final Solution!

import

import Puppeteer from 'puppeteer';

in case of firefox inside docker set product before running the next part:

Puppeteer._productName = 'firefox'

finaly launch

Puppeteer.launch({
    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
}
andyearnshaw commented 2 years ago

Hey, this is nice work! I will take a look as soon as I can. Thanks again!

andyearnshaw commented 1 year ago

I'm going to close this as it's not really actionable for docketeer itself. The ergonomics don't quite work the same for me, but I can see how it is useful for someone who wants to avoid using docketeer or customise how docker is launched.

Thanks again for your input.