73 lines
1.7 KiB
TypeScript
73 lines
1.7 KiB
TypeScript
import * as plugins from './smartbrowser.plugins';
|
|
|
|
import * as interfaces from './interfaces';
|
|
|
|
/**
|
|
* SmartBrowser
|
|
*/
|
|
export class SmartBrowser {
|
|
public headlessBrowser: plugins.smartpuppeteer.puppeteer.Browser;
|
|
public smartpdf: plugins.smartpdf.SmartPdf;
|
|
|
|
/**
|
|
* 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();
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
* @param urlArg
|
|
*/
|
|
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
|
|
};
|
|
}
|
|
|
|
/**
|
|
* evalutes an expression on a page
|
|
* @param urlArg
|
|
* @param funcArg
|
|
*/
|
|
public async evaluateOnPage(urlArg: string, funcArg: () => Promise<any>) {
|
|
const page = await this.headlessBrowser.newPage();
|
|
await page.goto(urlArg, {
|
|
waitUntil: 'networkidle2'
|
|
});
|
|
}
|
|
|
|
|
|
}
|