2019-05-28 21:27:55 +00:00
|
|
|
import * as plugins from './smartbrowser.plugins';
|
2016-09-19 18:00:19 +00:00
|
|
|
|
2019-06-03 15:16:47 +00:00
|
|
|
import * as interfaces from './interfaces';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SmartBrowser
|
|
|
|
*/
|
2019-05-29 06:57:17 +00:00
|
|
|
export class SmartBrowser {
|
2019-06-04 06:29:05 +00:00
|
|
|
public headlessBrowser: plugins.smartpuppeteer.puppeteer.Browser;
|
2019-06-03 15:16:47 +00:00
|
|
|
public smartpdf: plugins.smartpdf.SmartPdf;
|
|
|
|
|
2019-06-04 06:29:05 +00:00
|
|
|
/**
|
|
|
|
* start the SmartBrowser instance
|
|
|
|
*/
|
|
|
|
public async start() {
|
|
|
|
this.headlessBrowser = await plugins.smartpuppeteer.getEnvAwareBrowserInstance();
|
|
|
|
this.smartpdf = new plugins.smartpdf.SmartPdf();
|
|
|
|
await this.smartpdf.start(this.headlessBrowser);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* stop the SmartBrowser instance
|
|
|
|
*/
|
|
|
|
public async stop() {
|
|
|
|
await this.headlessBrowser.close();
|
|
|
|
await this.smartpdf.stop();
|
|
|
|
}
|
|
|
|
|
2019-06-03 15:16:47 +00:00
|
|
|
/**
|
|
|
|
* create a pdf from page
|
|
|
|
* @param urlArg
|
|
|
|
*/
|
|
|
|
public pdfFromPage(urlArg: string) {
|
|
|
|
const result = this.smartpdf.getFullWebsiteAsSinglePdf(urlArg);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* make a screenshot from a page
|
2019-06-03 15:17:20 +00:00
|
|
|
* @param urlArg
|
2019-06-03 15:16:47 +00:00
|
|
|
*/
|
|
|
|
public async screenshotFromPage(urlArg: string): Promise<interfaces.IScreenShotResult> {
|
|
|
|
const pageId = plugins.smartunique.shortId();
|
|
|
|
const page = await this.headlessBrowser.newPage();
|
|
|
|
await page.goto(urlArg, {
|
|
|
|
waitUntil: 'networkidle2'
|
|
|
|
});
|
|
|
|
const screenshotBuffer = await page.screenshot({
|
|
|
|
encoding: 'binary'
|
|
|
|
});
|
|
|
|
await page.close();
|
|
|
|
return {
|
|
|
|
name: pageId,
|
|
|
|
id: `${pageId}.js`,
|
|
|
|
buffer: screenshotBuffer
|
|
|
|
};
|
2019-05-29 12:14:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 15:16:47 +00:00
|
|
|
/**
|
|
|
|
* evalutes an expression on a page
|
2019-06-03 15:17:20 +00:00
|
|
|
* @param urlArg
|
|
|
|
* @param funcArg
|
2019-06-03 15:16:47 +00:00
|
|
|
*/
|
2019-06-03 15:17:20 +00:00
|
|
|
public async evaluateOnPage(urlArg: string, funcArg: () => Promise<any>) {
|
2019-06-03 15:16:47 +00:00
|
|
|
const page = await this.headlessBrowser.newPage();
|
|
|
|
await page.goto(urlArg, {
|
|
|
|
waitUntil: 'networkidle2'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-04 06:29:05 +00:00
|
|
|
|
2019-05-29 06:57:17 +00:00
|
|
|
}
|