Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
2e95f1db7f | |||
35e5ded808 | |||
f2430d095f | |||
d79a5af51a | |||
318bdd1bd8 | |||
54a0ec6c71 | |||
8853eecbb9 | |||
1bfca4456c |
26
package-lock.json
generated
26
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartbrowser",
|
"name": "@pushrocks/smartbrowser",
|
||||||
"version": "1.0.10",
|
"version": "1.0.14",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@ -153,7 +153,6 @@
|
|||||||
"version": "2.0.3",
|
"version": "2.0.3",
|
||||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartdelay/-/smartdelay-2.0.3.tgz",
|
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartdelay/-/smartdelay-2.0.3.tgz",
|
||||||
"integrity": "sha512-TXKDDqsc7sBTLl+oiYNaF6IdNk1n70i8ur8QfwcUU6tegTnrEkvMWy9h5Zdty/fq1ioCNpKLvuXoA+fgYVwKGQ==",
|
"integrity": "sha512-TXKDDqsc7sBTLl+oiYNaF6IdNk1n70i8ur8QfwcUU6tegTnrEkvMWy9h5Zdty/fq1ioCNpKLvuXoA+fgYVwKGQ==",
|
||||||
"dev": true,
|
|
||||||
"requires": {
|
"requires": {
|
||||||
"@pushrocks/smartpromise": "^3.0.2"
|
"@pushrocks/smartpromise": "^3.0.2"
|
||||||
}
|
}
|
||||||
@ -264,9 +263,9 @@
|
|||||||
"integrity": "sha512-MaI0+uLQPCr2V3WGnbdgb0pWa9xkWyrP4qYcbsHIjeismGLbn9s3jmP/HIXU8LkgzRgaVb+BJxmZJHOwl32DyA=="
|
"integrity": "sha512-MaI0+uLQPCr2V3WGnbdgb0pWa9xkWyrP4qYcbsHIjeismGLbn9s3jmP/HIXU8LkgzRgaVb+BJxmZJHOwl32DyA=="
|
||||||
},
|
},
|
||||||
"@pushrocks/smartpdf": {
|
"@pushrocks/smartpdf": {
|
||||||
"version": "1.0.6",
|
"version": "1.0.17",
|
||||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpdf/-/smartpdf-1.0.6.tgz",
|
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpdf/-/smartpdf-1.0.17.tgz",
|
||||||
"integrity": "sha512-zA6jmOgi+0tOX8kFCYqv+dxgXIPcmWjw7oVdE4THfh50uHKxubFpZkxq0MF+H8O16P3Akx3qQUYpnGhCkiTxxA==",
|
"integrity": "sha512-LOZOluCQeh7u4dWZ7J7Ufz9XPMXTwgC86NGTonr5tu7YB0bpcxnwA4aCZmi/nGtd1NnSpyyxSz3x+xONDqI8bA==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"@pushrocks/smartfile": "^7.0.2",
|
"@pushrocks/smartfile": "^7.0.2",
|
||||||
"@pushrocks/smartnetwork": "^1.1.6",
|
"@pushrocks/smartnetwork": "^1.1.6",
|
||||||
@ -283,6 +282,15 @@
|
|||||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpromise/-/smartpromise-3.0.2.tgz",
|
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpromise/-/smartpromise-3.0.2.tgz",
|
||||||
"integrity": "sha512-jmrJMUEmBCWChWK8CIcx4Vw3wv/8OgVNmkaxJrbs+WMaoRUfJtpWWJfrAwwHWt9ZXJbarJ+CwfwfYiiZXymndQ=="
|
"integrity": "sha512-jmrJMUEmBCWChWK8CIcx4Vw3wv/8OgVNmkaxJrbs+WMaoRUfJtpWWJfrAwwHWt9ZXJbarJ+CwfwfYiiZXymndQ=="
|
||||||
},
|
},
|
||||||
|
"@pushrocks/smartpuppeteer": {
|
||||||
|
"version": "1.0.5",
|
||||||
|
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpuppeteer/-/smartpuppeteer-1.0.5.tgz",
|
||||||
|
"integrity": "sha512-7iD2mxQHXG5/6PwZY2wU6GQjNXy9dR5cLP/nvfjxXaelX/x3N1yagjPAuMYxuIqiTtVfrHC4Ehq+gnTZ4mSPow==",
|
||||||
|
"requires": {
|
||||||
|
"@types/puppeteer": "^1.12.4",
|
||||||
|
"puppeteer": "^1.17.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"@pushrocks/smartrequest": {
|
"@pushrocks/smartrequest": {
|
||||||
"version": "1.1.15",
|
"version": "1.1.15",
|
||||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartrequest/-/smartrequest-1.1.15.tgz",
|
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartrequest/-/smartrequest-1.1.15.tgz",
|
||||||
@ -2273,9 +2281,9 @@
|
|||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"tslint": {
|
"tslint": {
|
||||||
"version": "5.16.0",
|
"version": "5.17.0",
|
||||||
"resolved": "https://verdaccio.lossless.one/tslint/-/tslint-5.16.0.tgz",
|
"resolved": "https://verdaccio.lossless.one/tslint/-/tslint-5.17.0.tgz",
|
||||||
"integrity": "sha512-UxG2yNxJ5pgGwmMzPMYh/CCnCnh0HfPgtlVRDs1ykZklufFBL1ZoTlWFRz2NQjcoEiDoRp+JyT0lhBbbH/obyA==",
|
"integrity": "sha512-pflx87WfVoYepTet3xLfDOLDm9Jqi61UXIKePOuca0qoAZyrGWonDG9VTbji58Fy+8gciUn8Bt7y69+KEVjc/w==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"@babel/code-frame": "^7.0.0",
|
"@babel/code-frame": "^7.0.0",
|
||||||
@ -2284,7 +2292,7 @@
|
|||||||
"commander": "^2.12.1",
|
"commander": "^2.12.1",
|
||||||
"diff": "^3.2.0",
|
"diff": "^3.2.0",
|
||||||
"glob": "^7.1.1",
|
"glob": "^7.1.1",
|
||||||
"js-yaml": "^3.13.0",
|
"js-yaml": "^3.13.1",
|
||||||
"minimatch": "^3.0.4",
|
"minimatch": "^3.0.4",
|
||||||
"mkdirp": "^0.5.1",
|
"mkdirp": "^0.5.1",
|
||||||
"resolve": "^1.3.2",
|
"resolve": "^1.3.2",
|
||||||
|
14
package.json
14
package.json
@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartbrowser",
|
"name": "@pushrocks/smartbrowser",
|
||||||
"version": "1.0.10",
|
"version": "1.0.14",
|
||||||
"description": "simplified puppeteer",
|
"description": "simplified puppeteer",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(npmts)",
|
"test": "tstest test/",
|
||||||
"build": "echo \"Not needed for now\""
|
"build": "tsbuild"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -19,14 +19,16 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/smartbrowser#README",
|
"homepage": "https://gitlab.com/pushrocks/smartbrowser#README",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartpdf": "^1.0.6",
|
"@pushrocks/smartdelay": "^2.0.3",
|
||||||
"puppeteer": "^1.17.0"
|
"@pushrocks/smartpdf": "^1.0.17",
|
||||||
|
"@pushrocks/smartpuppeteer": "^1.0.5",
|
||||||
|
"@pushrocks/smartunique": "^3.0.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.11",
|
"@gitzone/tsbuild": "^2.1.11",
|
||||||
"@gitzone/tstest": "^1.0.24",
|
"@gitzone/tstest": "^1.0.24",
|
||||||
"@pushrocks/tapbundle": "^3.0.9",
|
"@pushrocks/tapbundle": "^3.0.9",
|
||||||
"tslint": "^5.16.0",
|
"tslint": "^5.17.0",
|
||||||
"tslint-config-prettier": "^1.18.0"
|
"tslint-config-prettier": "^1.18.0"
|
||||||
},
|
},
|
||||||
"private": true,
|
"private": true,
|
||||||
|
@ -1,6 +0,0 @@
|
|||||||
<head>
|
|
||||||
<title>Test</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
Test
|
|
||||||
</body>
|
|
33
test/test.js
33
test/test.js
@ -1,33 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
require("typings-test");
|
|
||||||
const should = require("should");
|
|
||||||
const smartbrowser = require("../dist/index");
|
|
||||||
let testSmartBrowser;
|
|
||||||
describe('smartbrowser', () => {
|
|
||||||
it('should instanstiate a new browser ', function () {
|
|
||||||
testSmartBrowser = new smartbrowser.Smartbrowser({
|
|
||||||
webroot: './test/assets/',
|
|
||||||
watchFiles: ['./test/assets/']
|
|
||||||
});
|
|
||||||
should(testSmartBrowser).be.instanceof(smartbrowser.Smartbrowser);
|
|
||||||
});
|
|
||||||
it('should start the browser ', function (done) {
|
|
||||||
testSmartBrowser.start().then((bsInstance) => {
|
|
||||||
done();
|
|
||||||
}).catch((err) => { console.log(err); });
|
|
||||||
});
|
|
||||||
it('should stop the browser ', function (done) {
|
|
||||||
this.timeout(10000);
|
|
||||||
setTimeout(() => {
|
|
||||||
testSmartBrowser.stop().then(() => {
|
|
||||||
done();
|
|
||||||
}).catch((err) => { console.log(err); });
|
|
||||||
}, 2000);
|
|
||||||
});
|
|
||||||
it('should exit correctly', function () {
|
|
||||||
setTimeout(() => {
|
|
||||||
process.exit(0);
|
|
||||||
}, 2000);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFxQjtBQUNyQixpQ0FBZ0M7QUFFaEMsOENBQTZDO0FBRTdDLElBQUksZ0JBQTJDLENBQUE7Q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QTtJQUNaLENBQUMsQ0FBQyxDQUFBO0lBQ0YsRUFBRSxDQUFDLHVCQUF1QixFQUFDO1FBQ3ZCLFVBQVUsQ0FBQztZQUNQLE9BQU8sQ0FBQyxJQUFJLENBQUMsQ0FBQyxDQUFDLENBQUE7UUFDbkIsQ0FBQyxFQUFDLElBQUksQ0FBQyxDQUFBO0lBQ1gsQ0FBQyxDQUFDLENBQUE7QUFDTixDQUFDLENBQUMsQ0FBQSJ9
|
|
49
test/test.ts
49
test/test.ts
@ -1,32 +1,29 @@
|
|||||||
import { tap, expect } from 'tapbundle';
|
import { tap, expect } from '@pushrocks/tapbundle';
|
||||||
import * as smartdelay from 'smartdelay';
|
|
||||||
|
|
||||||
import * as smartbrowser from '../dist/index';
|
import * as smartbrowser from '../ts/index';
|
||||||
let testSmartBrowser: smartbrowser.Smartbrowser;
|
let testSmartBrowser: smartbrowser.SmartBrowser;
|
||||||
|
|
||||||
tap
|
tap.test('should instanstiate a new browser ', async () => {
|
||||||
.test('should instanstiate a new browser ', async () => {
|
testSmartBrowser = new smartbrowser.SmartBrowser();
|
||||||
testSmartBrowser = new smartbrowser.Smartbrowser({
|
return expect(testSmartBrowser).to.be.instanceof(smartbrowser.SmartBrowser);
|
||||||
webroot: './test/assets/',
|
});
|
||||||
watchFiles: ['./test/assets/']
|
|
||||||
});
|
|
||||||
return expect(testSmartBrowser).to.be.instanceof(smartbrowser.Smartbrowser);
|
|
||||||
})
|
|
||||||
.catch(tap.threw);
|
|
||||||
|
|
||||||
tap
|
tap.test('should start the browser ', async () => {
|
||||||
.test('should start the browser ', async () => {
|
await expect(testSmartBrowser.start()).to.eventually.be.fulfilled;
|
||||||
return await expect(testSmartBrowser.start()).to.eventually.be.fulfilled;
|
});
|
||||||
})
|
|
||||||
.catch(tap.threw);
|
tap.test('should create a PDF from a page', async tools => {
|
||||||
|
const result = await testSmartBrowser.pdfFromPage('https://lossless.com');
|
||||||
|
expect(result.buffer).to.be.instanceOf(Buffer);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should produce a valid screenshot', async tools => {
|
||||||
|
const result = await testSmartBrowser.screenshotFromPage('https://lossless.com');
|
||||||
|
expect(result.buffer).to.be.instanceOf(Buffer);
|
||||||
|
});
|
||||||
|
|
||||||
tap.test('should stop the browser ', async () => {
|
tap.test('should stop the browser ', async () => {
|
||||||
return await smartdelay.delayFor(2000).then(() => {
|
await expect(testSmartBrowser.stop()).to.eventually.be.fulfilled;
|
||||||
return expect(testSmartBrowser.stop()).to.eventually.be.fulfilled;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
tap.test('should exit correctly', async () => {
|
|
||||||
smartdelay.delayFor(2000).then(() => {
|
|
||||||
process.exit(0);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.start();
|
||||||
|
69
ts/index.ts
69
ts/index.ts
@ -1,7 +1,72 @@
|
|||||||
import * as plugins from './smartbrowser.plugins';
|
import * as plugins from './smartbrowser.plugins';
|
||||||
|
|
||||||
|
import * as interfaces from './interfaces';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SmartBrowser
|
||||||
|
*/
|
||||||
export class SmartBrowser {
|
export class SmartBrowser {
|
||||||
pdfFromPage() {
|
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'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
1
ts/interfaces/index.ts
Normal file
1
ts/interfaces/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './interfaces.screenshotresult';
|
5
ts/interfaces/interfaces.screenshotresult.ts
Normal file
5
ts/interfaces/interfaces.screenshotresult.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
export interface IScreenShotResult {
|
||||||
|
name: string;
|
||||||
|
id: string;
|
||||||
|
buffer: Buffer;
|
||||||
|
}
|
@ -1,3 +1,8 @@
|
|||||||
import * as puppeteer from 'puppeteer';
|
// pushrocks scope
|
||||||
|
import * as smartpdf from '@pushrocks/smartpdf';
|
||||||
|
import * as smartpuppeteer from '@pushrocks/smartpuppeteer';
|
||||||
|
import * as smartunique from '@pushrocks/smartunique';
|
||||||
|
|
||||||
export { puppeteer };
|
export { smartpdf, smartpuppeteer, smartunique };
|
||||||
|
|
||||||
|
// third party
|
||||||
|
Reference in New Issue
Block a user