Skip to content

Build your personal knowledge base with Trilium Notes

fix(clipper): crop a full-page screenshot to the rows it captured

Elian Dorane8bfb6701f25
diff --git a/apps/web-clipper/screenshot.spec.ts b/apps/web-clipper/screenshot.spec.ts
index e357c6cceb..c49901591e 100644
--- a/apps/web-clipper/screenshot.spec.ts
+++ b/apps/web-clipper/screenshot.spec.ts
@@ -107,6 +107,21 @@ describe("ScreenshotStitcher", () => {
expect(canvas.events).toEqual([ "draw 0: slice 0+692 at 7500", "encode 0" ]);
});
+ it("crops the last image to the rows captured when the page stops scrolling early", async () => {
+ stubImages({ naturalWidth: 100, naturalHeight: 500 });
+ const canvas = stubCanvas();
+ const stitcher = new ScreenshotStitcher({ pageHeight: 5000, devicePixelRatio: 1 });
+
+ await stitcher.add({ dataUrl: "top", y: 0 });
+ await stitcher.add({ dataUrl: "next", y: 500 });
+
+ expect(stitcher.finish()).toEqual([ "part-1" ]);
+ expect(canvas.sizes()).toEqual([ [ 100, 5000 ], [ 100, 1000 ] ]);
+ expect(canvas.events).toEqual([
+ "draw 0: top 0+500 at 0", "draw 0: next 0+500 at 500", "draw 1: canvas 0 0+1000 at 0", "encode 1"
+ ]);
+ });
+
it("fails without captures, without a canvas, or on a capture that cannot be read", async () => {
const request = { pageHeight: 100, devicePixelRatio: 1 };
stubImages({ naturalWidth: 100, naturalHeight: 100 });
@@ -150,8 +165,12 @@ function stubCanvas() {
const index = canvases.push(this) - 1;
return {
drawImage: (
- image: { src: string }, _sx: number, sy: number, _sw: number, sh: number, _dx: number, dy: number
- ) => events.push(`draw ${index}: ${image.src} ${sy}+${sh} at ${dy}`)
+ image: { src: string } | HTMLCanvasElement,
+ _sx: number, sy: number, _sw: number, sh: number, _dx: number, dy: number
+ ) => {
+ const source = "src" in image ? image.src : `canvas ${canvases.indexOf(image)}`;
+ events.push(`draw ${index}: ${source} ${sy}+${sh} at ${dy}`);
+ }
} as unknown as CanvasRenderingContext2D;
});
vi.spyOn(HTMLCanvasElement.prototype, "toDataURL").mockImplementation(function (this: HTMLCanvasElement) {
diff --git a/apps/web-clipper/screenshot.ts b/apps/web-clipper/screenshot.ts
index 779642d30b..27ee9c1884 100644
--- a/apps/web-clipper/screenshot.ts
+++ b/apps/web-clipper/screenshot.ts
@@ -60,12 +60,23 @@ export class ScreenshotStitcher {
this.drawnTo = Math.max(this.drawnTo, bottom);
}
- /** Returns the images as PNG data URLs, from the top of the page. */
+ /**
+ * Returns the images as PNG data URLs, from the top of the page. The last one ends at the last
+ * row captured, which is short of the page's height when the page stopped scrolling early.
+ */
finish() {
if (!this.part) {
throw new Error("There is no screenshot to stitch.");
}
- this.closePart(this.part);
+ const { canvas } = this.part;
+ const rows = this.drawnTo - this.part.index * MAX_PART_HEIGHT;
+ if (rows < canvas.height) {
+ const cropped = createCanvas(canvas.width, rows);
+ cropped.context.drawImage(canvas, 0, 0, canvas.width, rows, 0, 0, canvas.width, rows);
+ this.closePart(cropped);
+ } else {
+ this.closePart(this.part);
+ }
return this.parts;
}
@@ -73,15 +84,9 @@ export class ScreenshotStitcher {
if (this.part?.index === index) return this.part.context;
if (this.part) this.closePart(this.part);
- const canvas = document.createElement("canvas");
- canvas.width = this.width;
- canvas.height = Math.min(MAX_PART_HEIGHT, this.height - index * MAX_PART_HEIGHT);
- const context = canvas.getContext("2d");
- if (!context) {
- throw new Error("The screenshot could not be drawn.");
- }
- this.part = { index, canvas, context };
- return context;
+ const height = Math.min(MAX_PART_HEIGHT, this.height - index * MAX_PART_HEIGHT);
+ this.part = { index, ...createCanvas(this.width, height) };
+ return this.part.context;
}
private closePart(part: { canvas: HTMLCanvasElement }) {
@@ -91,6 +96,17 @@ export class ScreenshotStitcher {
}
+function createCanvas(width: number, height: number) {
+ const canvas = document.createElement("canvas");
+ canvas.width = width;
+ canvas.height = height;
+ const context = canvas.getContext("2d");
+ if (!context) {
+ throw new Error("The screenshot could not be drawn.");
+ }
+ return { canvas, context };
+}
+
function loadImage(src: string) {
return new Promise<HTMLImageElement>((resolve, reject) => {
const image = new Image();