smartpuppeteer/ts/smartpuppeteer.classes.smartpuppeteer.ts

42 lines
1.2 KiB
TypeScript
Raw Permalink Normal View History

2022-03-24 12:51:12 +00:00
import * as plugins from './smartpuppeteer.plugins.js';
2019-11-15 20:44:11 +00:00
export interface IEnvAwareOptions {
forceNoSandbox?: boolean;
2022-07-18 01:08:32 +00:00
usePipe?: boolean;
2019-11-15 20:44:11 +00:00
}
export const getEnvAwareBrowserInstance = async (
optionsArg: IEnvAwareOptions = {}
): Promise<plugins.puppeteer.Browser> => {
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,
2022-07-18 01:08:32 +00:00
pipe: options.usePipe !== undefined ? options.usePipe : true,
2021-11-07 17:30:08 +00:00
headless: true,
2021-11-07 18:33:09 +00:00
...(() => {
const returnObject: any = {};
2021-11-07 18:51:50 +00:00
const googleChrome = plugins.smartshell.which.sync('google-chrome');
if (googleChrome) {
returnObject.executablePath = googleChrome;
}
2021-11-07 18:33:09 +00:00
return returnObject;
2021-11-07 18:51:50 +00:00
})(),
2019-11-15 23:35:03 +00:00
});
2019-11-15 20:44:11 +00:00
return headlessBrowser;
};