smartpuppeteer/ts/smartpuppeteer.classes.smartpuppeteer.ts

32 lines
882 B
TypeScript
Raw Permalink Normal View History

2019-11-15 20:44:11 +00:00
import * as plugins from './smartpuppeteer.plugins';
export interface IEnvAwareOptions {
forceNoSandbox?: boolean;
}
export const getEnvAwareBrowserInstance = async (
optionsArg: IEnvAwareOptions = {}
): Promise<plugins.puppeteer.Browser> => {
const smartenv = new plugins.smartenv.Smartenv();
const options: IEnvAwareOptions = {
...{
2021-01-08 21:16:25 +00:00
forceNoSandbox: false,
2019-11-15 20:44:11 +00:00
},
2021-01-08 21:16:25 +00:00
...optionsArg,
2019-11-15 20:44:11 +00:00
};
let chromeArgs: string[] = [];
2021-01-08 21:16:25 +00:00
if (process.env.CI || options.forceNoSandbox || plugins.os.userInfo().username === 'root') {
2019-11-15 23:35:03 +00:00
chromeArgs = chromeArgs.concat(['--no-sandbox', '--disable-setuid-sandbox']);
2019-11-15 20:44:11 +00:00
}
let headlessBrowser: plugins.puppeteer.Browser;
2019-11-15 23:35:03 +00:00
console.log('launching puppeteer bundled chrome with arguments:');
console.log(chromeArgs);
headlessBrowser = await plugins.puppeteer.launch({
2021-01-08 21:16:25 +00:00
args: chromeArgs,
2019-11-15 23:35:03 +00:00
});
2019-11-15 20:44:11 +00:00
return headlessBrowser;
};