Skip to content

Build your personal knowledge base with Trilium Notes

feat(clipper): limit a full-page screenshot to 50 captures

Elian Doranb7db32f26bbe
diff --git a/apps/web-clipper/entrypoints/background/index.spec.ts b/apps/web-clipper/entrypoints/background/index.spec.ts
index ad41a8cfe7..8a10008f98 100644
--- a/apps/web-clipper/entrypoints/background/index.spec.ts
+++ b/apps/web-clipper/entrypoints/background/index.spec.ts
@@ -602,10 +602,10 @@ describe("background", () => {
vi.useFakeTimers({ toFake: [ "setTimeout", "Date" ] });
offscreen.slices = [];
- simulatePage({ viewportHeight: 2000, pageHeight: 100000, devicePixelRatio: 4 });
+ simulatePage({ viewportHeight: 2000, pageHeight: 100001, devicePixelRatio: 4 });
await run(sendRuntimeMessage({ name: "save-full-page-screenshot" }));
- expect(offscreen.request).toEqual({ pageHeight: 15000, devicePixelRatio: 4 });
- expect(sliceTops()).toEqual([ 0, 2000, 4000, 6000, 8000, 10000, 12000, 14000 ]);
+ expect(offscreen.request).toEqual({ pageHeight: 100000, devicePixelRatio: 4 });
+ expect(sliceTops()).toEqual(Array.from({ length: 50 }, (_, index) => index * 2000));
expect(await lastToast()).toMatchObject({
message: "Screenshot has been saved to Trilium, but the page was too long to capture in full."
});
diff --git a/apps/web-clipper/entrypoints/background/index.ts b/apps/web-clipper/entrypoints/background/index.ts
index b839051b3f..4b9f8d5c1e 100644
--- a/apps/web-clipper/entrypoints/background/index.ts
+++ b/apps/web-clipper/entrypoints/background/index.ts
@@ -1,5 +1,5 @@
import {
- MAX_SCREENSHOT_HEIGHT, type ScreenshotSlice, ScreenshotStitcher, type StitchMessage, type StitchRequest
+ type ScreenshotSlice, ScreenshotStitcher, type StitchMessage, type StitchRequest
} from "@/screenshot";
import { extractPage, randomString, type Rect } from "@/utils";
@@ -50,6 +50,8 @@ interface Stitcher {
const CAPTURE_INTERVAL_MS = 550;
/** How long a scrolled page gets to draw what came into view before it is captured. */
const SCROLL_SETTLE_MS = 150;
+/** The most captures a full-page screenshot takes, about 30 seconds; a longer page is cut off. */
+const MAX_SCREENSHOT_CAPTURES = 50;
/** An image of a clipping; `content` references it by `imageId` until Trilium stores it. */
type ClippedImage = { imageId: string, src: string, dataUrl?: string };
@@ -177,7 +179,7 @@ export default defineBackground(() => {
await setFloatingElements(tabId, "first");
// Measured once the page is prepared: without its scrollbars, it can lay out differently.
const page = await scrollTab(tabId, start.scrollX, 0);
- const height = Math.min(page.pageHeight, MAX_SCREENSHOT_HEIGHT / page.devicePixelRatio);
+ const height = Math.min(page.pageHeight, MAX_SCREENSHOT_CAPTURES * page.viewportHeight);
const stitcher = await createStitcher({ pageHeight: height, devicePixelRatio: page.devicePixelRatio });
let position = page;
diff --git a/apps/web-clipper/screenshot.ts b/apps/web-clipper/screenshot.ts
index e93f3a0957..54035700a2 100644
--- a/apps/web-clipper/screenshot.ts
+++ b/apps/web-clipper/screenshot.ts
@@ -24,12 +24,6 @@ export type StitchMessage =
*/
export const MAX_PART_HEIGHT = 8192;
-/**
- * The longest page a screenshot captures, in device pixels. It bounds the memory and the time a
- * capture takes: about a second per two viewports.
- */
-export const MAX_SCREENSHOT_HEIGHT = 60_000;
-
/**
* Draws the captures of a scrolled page, given in order from the top, into images of at most
* {@link MAX_PART_HEIGHT}. Each image is encoded as soon as the next one begins, so only one is