|
@@ -1,12 +1,13 @@
|
|
|
import * as log4js from 'log4js';
|
|
|
import * as webshot from 'webshot';
|
|
|
import * as read from 'read-all-stream';
|
|
|
+import {PNG} from 'pngjs';
|
|
|
|
|
|
const logger = log4js.getLogger('webshot');
|
|
|
logger.level = 'info';
|
|
|
|
|
|
function renderWebshot(url: string, height: number): Promise<string> {
|
|
|
- let promise = new Promise<string>(resolve => {
|
|
|
+ let promise = new Promise<{ data: string, boundary: null | number }>(resolve => {
|
|
|
const options = {
|
|
|
windowSize: {
|
|
|
width: 1080,
|
|
@@ -17,14 +18,33 @@ function renderWebshot(url: string, height: number): Promise<string> {
|
|
|
quality: 100,
|
|
|
customCSS: 'html{zoom:2}header{display:none!important}',
|
|
|
};
|
|
|
- const renderStream = webshot(url, options);
|
|
|
- read(renderStream, 'base64').then(data => {
|
|
|
- resolve(data);
|
|
|
- });
|
|
|
+ webshot(url, options).pipe(new PNG({
|
|
|
+ filterType: 4
|
|
|
+ }))
|
|
|
+ .on('parsed', function () {
|
|
|
+ let boundary = null;
|
|
|
+ for (let y = 0; y < this.height; y++) {
|
|
|
+ const x = 0;
|
|
|
+ let idx = (this.width * y + x) << 2;
|
|
|
+ if (this.data[idx] !== 255) {
|
|
|
+ boundary = y;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (boundary != null) {
|
|
|
+ this.data = this.data.slice(0, (this.width * boundary) << 2);
|
|
|
+ this.height = boundary;
|
|
|
+ read(this.pack(), 'base64').then(data => {
|
|
|
+ resolve({ data, boundary });
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ resolve({ data: '', boundary });
|
|
|
+ }
|
|
|
+ });
|
|
|
});
|
|
|
return promise.then(data => {
|
|
|
- if (height < 2048) return renderWebshot(url, height * 2);
|
|
|
- else return data;
|
|
|
+ if (data.boundary != null) return renderWebshot(url, height * 2);
|
|
|
+ else return data.data;
|
|
|
})
|
|
|
}
|
|
|
|