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,
|
2021-11-07 14:58:45 +00:00
|
|
|
pipe: true,
|
2021-11-07 17:30:08 +00:00
|
|
|
headless: true,
|
2021-11-07 18:33:09 +00:00
|
|
|
...(() => {
|
|
|
|
const returnObject: any = {};
|
|
|
|
const googleChrome = plugins.smartshell.which.sync('google-chrome')
|
|
|
|
if(googleChrome) {
|
|
|
|
returnObject.executablePath = googleChrome
|
|
|
|
}
|
|
|
|
return returnObject;
|
|
|
|
})()
|
2019-11-15 23:35:03 +00:00
|
|
|
});
|
2019-11-15 20:44:11 +00:00
|
|
|
|
|
|
|
return headlessBrowser;
|
|
|
|
};
|