Kaynağa Gözat

fix truncating not working with node screenshot

Mike L 3 yıl önce
ebeveyn
işleme
8da5cd7ec7
2 değiştirilmiş dosya ile 29 ekleme ve 8 silme
  1. 14 4
      dist/webshot.js
  2. 15 4
      src/webshot.ts

+ 14 - 4
dist/webshot.js

@@ -138,8 +138,11 @@ class Webshot extends CallableInstance {
                         .then(handle => {
                         if (handle === null)
                             throw new puppeteer.errors.TimeoutError();
+                        let cropTop;
                         return (0, utils_1.chainPromises)(morePostProcessings.map(func => () => func(page, handle)))
                             .then(() => (0, util_1.promisify)(setTimeout)(getTimeout()))
+                            .then(() => page.evaluate(() => document.documentElement.scrollTop))
+                            .then(scrollTop => { cropTop = scrollTop * zoomFactor; })
                             .then(() => page.evaluate(() => document.activeElement.blur()))
                             .then(() => handle.evaluateHandle(div => {
                             const minHeight = Number(div.style.transform.match(/translateY\((.+)px\)/)[1]) + div.offsetHeight;
@@ -151,16 +154,23 @@ class Webshot extends CallableInstance {
                             logger.error(`error while parsing content height, failing this webshot`);
                             throw err;
                         })
-                            .then(parentDivHandle => parentDivHandle.screenshot());
+                            .then(parentDivHandle => parentDivHandle.screenshot())
+                            .then(screenshot => [screenshot, cropTop]);
                     })
-                        .then(screenshot => {
+                        .then(([screenshot, cropTop]) => {
                         new pngjs_1.PNG({
                             filterType: 4,
                             deflateLevel: 0,
                         }).on('parsed', function () {
-                            sharpToFile(jpeg(this.pack())).then(path => {
+                            let png = this;
+                            if (cropTop > 0) {
+                                logger.info(`cropping screenshot at y offset ${cropTop}...`);
+                                png = new pngjs_1.PNG({ width: this.width, height: this.height - cropTop });
+                                this.bitblt(png, 0, cropTop, png.width, png.height, 0, 0);
+                            }
+                            sharpToFile(jpeg(png.pack())).then(path => {
                                 logger.info(`finished webshot for ${url}`);
-                                resolve({ path, boundary: this.height });
+                                resolve({ path, boundary: png.height });
                             });
                         }).parse(screenshot);
                     })

+ 15 - 4
src/webshot.ts

@@ -172,8 +172,12 @@ class Webshot extends CallableInstance<[Tweet[], (...args) => void, number], Pro
             })
             .then(handle => {
               if (handle === null) throw new puppeteer.errors.TimeoutError();
+              let cropTop: number;
               return chainPromises(morePostProcessings.map(func => () => func(page, handle)))
                 .then(() => promisify(setTimeout)(getTimeout()))
+                // determine screenshot crop y offset
+                .then(() => page.evaluate(() => document.documentElement.scrollTop))
+                .then(scrollTop => { cropTop = scrollTop * zoomFactor; })
                 // hide highlight of retweet header
                 .then(() => page.evaluate(() => (document.activeElement as unknown as HTMLOrSVGElement).blur()))
                 // determine screenshot height
@@ -187,16 +191,23 @@ class Webshot extends CallableInstance<[Tweet[], (...args) => void, number], Pro
                   logger.error(`error while parsing content height, failing this webshot`);
                   throw err;
                 })
-                .then(parentDivHandle => parentDivHandle.screenshot());
+                .then(parentDivHandle => parentDivHandle.screenshot())
+                .then<[Buffer, number]>(screenshot => [screenshot, cropTop]);
             })
-            .then(screenshot => {
+            .then(([screenshot, cropTop]) => {
               new PNG({
                 filterType: 4,
                 deflateLevel: 0,
               }).on('parsed', function () {
-                sharpToFile(jpeg(this.pack())).then(path => {
+                let png = this;
+                if (cropTop > 0) {
+                  logger.info(`cropping screenshot at y offset ${cropTop}...`);
+                  png = new PNG({width: this.width, height: this.height - cropTop});
+                  this.bitblt(png, 0, cropTop, png.width, png.height, 0, 0);
+                }
+                sharpToFile(jpeg(png.pack())).then(path => {
                   logger.info(`finished webshot for ${url}`);
-                  resolve({path, boundary: this.height});
+                  resolve({path, boundary: png.height});
                 });
               }).parse(screenshot);
             })