smartbrowser/ts/index.ts

76 lines
1.7 KiB
TypeScript
Raw Normal View History

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-03 15:16:47 +00:00
public headlessBrowser: plugins.puppeteer.Browser;
public smartpdf: plugins.smartpdf.SmartPdf;
/**
* 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'
});
}
/**
* start a page
*/
2019-05-29 12:14:26 +00:00
public async start() {
2019-06-03 15:16:47 +00:00
this.headlessBrowser = await plugins.puppeteer.launch({
defaultViewport: {
width: 1600,
height: 1080
}
});
2019-05-29 12:14:26 +00:00
this.smartpdf = new plugins.smartpdf.SmartPdf();
2019-06-03 15:16:47 +00:00
await this.smartpdf.start(this.headlessBrowser);
2019-05-29 12:14:26 +00:00
}
/**
* stops the smartbrowser instance
*/
2019-06-03 15:16:47 +00:00
public async stop() {
await this.headlessBrowser.close();
await this.smartpdf.stop();
2019-05-29 06:57:17 +00:00
}
}