Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(clipper): take a screenshot of the whole page
Elian Doran52c90f56fbf4
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Web Clipper.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Web Clipper.htmlindex ba2d2f4d0d..8b7067667b 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Web Clipper.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Web Clipper.html@@ -24,7 +24,8 @@<li>select text and clip it with the right-click context menu</li><li>click on an image or link and save it through context menu</li><li>save whole page from the popup or context menu</li>- <li>save screenshot (with crop tool) from either popup or context menu</li>+ <li>save a screenshot of an area you select, of the visible part of the page+ or of the whole page, from either popup or context menu</li><li>create short text note from popup</li></ul><h2>The popup</h2>@@ -82,11 +83,19 @@<li><p>Below the preview, a row of buttons offers the other actions: <em>Crop</em> (ascreenshot of an area you select), <em>Screenshot</em> (of the visible part- of the page) and <em>Tabs</em> (saves the links of every tab in the current- window as a list of <a class="reference-link" href="#root/_help_UmgZAfcltxkS">Link Previews</a>,- or of plain links when <em>Autogenerate link previews</em> is turned off).- Hovering over a button shows its full name and, if it has one, its keyboard- shortcut.</p>+ of the page), <em>Full page</em> (a screenshot of the whole page) and <em>Tabs</em> (saves+ the links of every tab in the current window as a list of <a class="reference-link"+ href="#root/_help_UmgZAfcltxkS">Link Previews</a>, or of plain links when <em>Autogenerate link previews</em> is+ turned off). Hovering over a button shows its full name and, if it has+ one, its keyboard shortcut.</p>+ </li>+ <li>+ <p><em>Full page</em> scrolls through the page, takes a screenshot of each+ part and joins them into one image, then scrolls back to where the page+ was. Stay on the tab until it is saved: switching to another tab stops+ the screenshot. Elements that stay in place while the page scrolls (such+ as a fixed header) appear in every part of the image, and a very long page+ is cut off.</p></li><li><p>If the current page was already clipped, a notice at the top offers to@@ -143,6 +152,8 @@<li>Save screenshot: <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>E</kbd> (Mac: <kbd>⌘</kbd>+<kbd>⇧</kbd>+<kbd>E</kbd>)</li><li>Save all tabs of the current window: no default shortcut, but one canbe assigned as described below.</li>+ <li>Save a screenshot of the whole page: no default shortcut, but one can+ be assigned as described below.</li></ul><p>To set custom shortcuts, follow the directions for your browser.</p><ul>diff --git a/apps/web-clipper/entrypoints/background/context_menu.ts b/apps/web-clipper/entrypoints/background/context_menu.tsindex 967466a0ad..6eacfe9752 100644--- a/apps/web-clipper/entrypoints/background/context_menu.ts+++ b/apps/web-clipper/entrypoints/background/context_menu.ts@@ -11,7 +11,12 @@ const CONTEXT_MENU_ITEMS: Browser.contextMenus.CreateProperties[] = [},{id: "trilium-save-whole-screenshot",- title: "Save whole screenshot to Trilium",+ title: "Save visible area screenshot to Trilium",+ contexts: ["page"]+ },+ {+ id: "trilium-save-full-page-screenshot",+ title: "Save full page screenshot to Trilium",contexts: ["page"]},{diff --git a/apps/web-clipper/entrypoints/background/index.spec.ts b/apps/web-clipper/entrypoints/background/index.spec.tsindex 0e17f9c81f..17bb122d59 100644--- a/apps/web-clipper/entrypoints/background/index.spec.ts+++ b/apps/web-clipper/entrypoints/background/index.spec.ts@@ -17,6 +17,12 @@ const { facade, TriliumError } = vi.hoisted(() => ({}}));+const stitchScreenshots = vi.hoisted(() => vi.fn());+vi.mock("@/screenshot", async (importOriginal) => ({+ ...await importOriginal<typeof import("@/screenshot")>(),+ stitchScreenshots+}));+vi.mock("./trilium_server_facade", () => ({TriliumError,default: class {@@ -107,7 +113,7 @@ describe("background", () => {Object.assign(fakeBrowser, { offscreen: { createDocument }, scripting: { executeScript } });background.main();- await vi.waitFor(() => expect(contextMenusCreate).toHaveBeenCalledTimes(6));+ await vi.waitFor(() => expect(contextMenusCreate).toHaveBeenCalledTimes(7));});afterEach(() => {@@ -122,6 +128,7 @@ describe("background", () => {"trilium-save-selection","trilium-save-cropped-screenshot","trilium-save-whole-screenshot",+ "trilium-save-full-page-screenshot","trilium-save-page","trilium-save-link","trilium-save-image"@@ -490,6 +497,138 @@ describe("background", () => {});});+ describe("full page screenshots", () => {+ const captureVisibleTab = vi.fn(async () => PNG);+ let tabActive: boolean;++ beforeEach(() => {+ vi.useFakeTimers({ toFake: [ "setTimeout", "Date" ] });+ tabActive = true;+ Object.assign(fakeBrowser.tabs, {+ get: async () => ({ active: tabActive }),+ captureVisibleTab+ });+ runtimeSendMessage.mockImplementation(async (message) => (+ (message as { type?: string }).type === "STITCH_IMAGES" ? { dataUrl: CROPPED_PNG } : PNG+ ));+ });++ afterEach(() => {+ vi.useRealTimers();+ runtimeSendMessage.mockImplementation(async () => PNG);+ });++ /** Runs an action to its end, letting the waits between captures pass. */+ async function run(action: Promise<unknown>) {+ await vi.runAllTimersAsync();+ await action;+ vi.useRealTimers();+ }++ /** The positions the page was asked to scroll to; `undefined` only asks where it is. */+ function scrolls() {+ return tabsSendMessage.mock.calls+ .map(([ , message ]) => message as { name: string, y?: number })+ .filter((message) => message.name === "trilium-scroll-to")+ .map((message) => message.y);+ }++ function stitchRequest() {+ const call = runtimeSendMessage.mock.calls.find(([ message ]) =>+ (message as { type?: string }).type === "STITCH_IMAGES");+ return (call?.[0] as { request?: unknown } | undefined)?.request;+ }++ it("captures the page a viewport at a time, then stitches the captures offscreen", async () => {+ simulatePage({ startY: 300, viewportHeight: 500, pageHeight: 1200 });+ const captureTimes: number[] = [];+ captureVisibleTab.mockImplementation(async () => {+ captureTimes.push(Date.now());+ return PNG;+ });++ await run(onCommand("saveFullPageScreenshot"));++ expect(scrolls()).toEqual([ undefined, 0, 500, 1000, 300 ]);+ expect(stitchRequest()).toEqual({+ slices: [ { dataUrl: PNG, y: 0 }, { dataUrl: PNG, y: 500 }, { dataUrl: PNG, y: 700 } ],+ viewportHeight: 500,+ pageHeight: 1200+ });+ const intervals = captureTimes.slice(1).map((time, index) => time - (captureTimes[index] ?? 0));+ expect(intervals.every((interval) => interval >= 550)).toBe(true);+ expect(lastPayload()).toMatchObject({+ images: [ { src: "inline.png", dataUrl: CROPPED_PNG } ],+ pageUrl: PAGE_URL+ });+ expect(await lastToast()).toMatchObject({ message: "Screenshot has been saved to Trilium." });+ });++ it("stops where the page stops scrolling, and at the height a screenshot can hold", async () => {+ simulatePage({ viewportHeight: 500, pageHeight: 5000, scrollLimit: 1000 });+ await run(onContextMenuClicked({ menuItemId: "trilium-save-full-page-screenshot", pageUrl: MENU_URL }));+ expect(stitchRequest()).toMatchObject({ slices: [ { y: 0 }, { y: 500 } ] });+ expect(lastPayload()).toMatchObject({ pageUrl: MENU_URL });++ vi.useFakeTimers({ toFake: [ "setTimeout", "Date" ] });+ runtimeSendMessage.mockClear();+ simulatePage({ viewportHeight: 2000, pageHeight: 100000, devicePixelRatio: 4 });+ await run(sendRuntimeMessage({ name: "save-full-page-screenshot" }));+ expect(stitchRequest()).toMatchObject({ slices: [ { y: 0 }, { y: 2000 }, { y: 4000 } ] });+ });++ it("stops and scrolls back when another tab is opened, even if scrolling back fails", async () => {+ simulatePage({ startY: 300, viewportHeight: 500, pageHeight: 1200 });+ const scrollPage = tabMessageHandler;+ tabMessageHandler = (message) => {+ if ((message as { y?: number }).y === 300) throw new Error("The tab is gone.");+ return scrollPage(message);+ };+ tabActive = false;++ await run(sendRuntimeMessage({ name: "save-full-page-screenshot" }));++ expect(captureVisibleTab).not.toHaveBeenCalled();+ expect(scrolls()).toEqual([ undefined, 0, 300 ]);+ expect(await lastToast()).toMatchObject({+ message: "Saving to Trilium failed: The screenshot was stopped because another tab was opened."+ });+ });++ it("reports a page that does not answer, a failed stitch, and a tab without an ID", async () => {+ await run(sendRuntimeMessage({ name: "save-full-page-screenshot" }));+ expect(await lastToast()).toMatchObject({+ message: "Saving to Trilium failed: The page could not be read. Reload it and try again."+ });++ vi.useFakeTimers({ toFake: [ "setTimeout", "Date" ] });+ simulatePage({ viewportHeight: 500, pageHeight: 500 });+ runtimeSendMessage.mockImplementation(async () => ({ error: "Too tall" }));+ await run(sendRuntimeMessage({ name: "save-full-page-screenshot" }));+ expect(await lastToast()).toMatchObject({ message: "Saving to Trilium failed: Too tall" });++ tabs = [ { title: "Tab without id" } ];+ await sendRuntimeMessage({ name: "save-full-page-screenshot" });+ expect(facade.callService).not.toHaveBeenCalled();+ });++ it("stitches in the background page under Manifest V2", async () => {+ vi.stubGlobal("__MANIFEST_VERSION__", 2);+ stitchScreenshots.mockResolvedValue(CROPPED_PNG);+ simulatePage({ viewportHeight: 500, pageHeight: 500 });++ await run(sendRuntimeMessage({ name: "save-full-page-screenshot" }));++ expect(runtimeSendMessage).not.toHaveBeenCalled();+ expect(stitchScreenshots).toHaveBeenCalledWith({+ slices: [ { dataUrl: PNG, y: 0 } ],+ viewportHeight: 500,+ pageHeight: 500+ });+ expect(lastPayload()).toMatchObject({ images: [ { dataUrl: CROPPED_PNG } ] });+ });+ });+describe("context menu", () => {it("saves an image by fetching it", async () => {await onContextMenuClicked({ menuItemId: "trilium-save-image", pageUrl: PAGE_URL });@@ -802,6 +941,24 @@ async function lastToast() {.at(-1);}+/**+ * Answers the content script's scroll messages for a page of `pageHeight` that scrolls no further+ * than `scrollLimit`, starting at `startY`.+ */+function simulatePage({ startY = 0, viewportHeight, pageHeight, scrollLimit = pageHeight, devicePixelRatio = 1 }: {+ startY?: number, viewportHeight: number, pageHeight: number, scrollLimit?: number, devicePixelRatio?: number+}) {+ let scrollY = startY;+ tabMessageHandler = (message) => {+ if (message.name !== "trilium-scroll-to") return undefined;+ const { y } = message as { y?: number };+ if (y !== undefined) {+ scrollY = Math.max(0, Math.min(y, scrollLimit - viewportHeight));+ }+ return { scrollX: 0, scrollY, viewportHeight, pageHeight, devicePixelRatio };+ };+}+function stubImageLoading() {const state = { fail: false, instances: [] as object[] };vi.stubGlobal("Image", class {diff --git a/apps/web-clipper/entrypoints/background/index.ts b/apps/web-clipper/entrypoints/background/index.tsindex 4883beac39..5065c08f43 100644--- a/apps/web-clipper/entrypoints/background/index.ts+++ b/apps/web-clipper/entrypoints/background/index.ts@@ -1,3 +1,6 @@+import {+ MAX_SCREENSHOT_HEIGHT, type ScreenshotSlice, stitchScreenshots, type StitchRequest+} from "@/screenshot";import { extractPage, randomString, type Rect } from "@/utils";import setupContextMenu from "./context_menu";@@ -14,8 +17,27 @@ type BackgroundMessage = {name: "trilium-get-rectangle-for-screenshot";} | {name: "trilium-get-page-metadata";+} | {+ /** Scrolls the page to `x`, `y` when given, and answers with a {@link ScrollPosition}. */+ name: "trilium-scroll-to";+ x?: number;+ y?: number;+};++/** Where the page is scrolled to and how large it is, in CSS pixels. */+type ScrollPosition = {+ scrollX: number;+ scrollY: number;+ viewportHeight: number;+ pageHeight: number;+ devicePixelRatio: number;};+/** Chrome allows two `captureVisibleTab()` calls a second, and fails a third. */+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;+/** An image of a clipping; `content` references it by `imageId` until Trilium stores it. */type ClippedImage = { imageId: string, src: string, dataUrl?: string };@@ -42,6 +64,11 @@ export default defineBackground(() => {await saveCroppedScreenshot(activeTab.url);break;}+ case "saveFullPageScreenshot": {+ const activeTab = await getActiveTab();+ await saveScreenshot(await takeFullPageScreenshot(), activeTab.url);+ break;+ }default:console.log("Unrecognized command", command);}@@ -117,13 +144,73 @@ export default defineBackground(() => {}async function takeWholeScreenshot() {- // this saves only visible portion of the page- // workaround to save the whole page is to scroll & stitch- // example in https://github.com/mrcoles/full-page-screen-capture-chrome-extension- // see page.js and popup.jsreturn await browser.tabs.captureVisibleTab({ format: 'png' });}+ /**+ * Scrolls the active tab from the top, one viewport at a time, captures each, and stitches the+ * captures into one image. The tab is scrolled back to where it was afterwards.+ */+ async function takeFullPageScreenshot() {+ const { id: tabId, windowId } = await getActiveTab();+ if (tabId === undefined) {+ throw new Error("No active tab.");+ }++ const start = await scrollTab(tabId);+ const height = Math.min(start.pageHeight, MAX_SCREENSHOT_HEIGHT / start.devicePixelRatio);+ const slices: ScreenshotSlice[] = [];+ let lastCaptureAt = 0;++ try {+ let y = 0;+ while (y < height) {+ const position = await scrollTab(tabId, start.scrollX, y);+ const previous = slices.at(-1);+ // The page stopped scrolling short of the height it reported.+ if (previous && position.scrollY <= previous.y) break;++ await delay(Math.max(SCROLL_SETTLE_MS, lastCaptureAt + CAPTURE_INTERVAL_MS - Date.now()));+ const tab = await browser.tabs.get(tabId);+ if (!tab.active) {+ throw new Error("The screenshot was stopped because another tab was opened.");+ }++ lastCaptureAt = Date.now();+ const dataUrl = await browser.tabs.captureVisibleTab(windowId, { format: "png" });+ slices.push({ dataUrl, y: position.scrollY });+ y = position.scrollY + start.viewportHeight;+ }+ } finally {+ await scrollTab(tabId, start.scrollX, start.scrollY).catch(() => undefined);+ }++ return await stitch({ slices, viewportHeight: start.viewportHeight, pageHeight: start.pageHeight });+ }++ /** Scrolls the tab when given a position, and answers where it is scrolled to. */+ async function scrollTab(tabId: number, x?: number, y?: number): Promise<ScrollPosition> {+ const message: BackgroundMessage = { name: "trilium-scroll-to", x, y };+ const position = await browser.tabs.sendMessage(tabId, message);+ if (!position) {+ throw new Error("The page could not be read. Reload it and try again.");+ }+ return position;+ }++ async function stitch(request: StitchRequest) {+ if (import.meta.env.MANIFEST_VERSION !== 3) {+ return await stitchScreenshots(request);+ }++ await ensureOffscreenDocument();+ const { dataUrl, error } = await browser.runtime.sendMessage({ type: "STITCH_IMAGES", request });+ if (error) {+ throw new Error(error);+ }+ return dataUrl as string;+ }+async function getActiveTab() {const tabs = await browser.tabs.query({active: true,@@ -304,18 +391,10 @@ export default defineBackground(() => {});if (!rect) return;- const src = await takeCroppedScreenshot(rect, devicePixelRatio);-- const { payload } = await getImagePayloadFromSrc(src, pageUrl);-- const resp = await triliumServerFacade.callService("POST", "clippings", payload);-- toast("Screenshot has been saved to Trilium.", resp.noteId);+ await saveScreenshot(await takeCroppedScreenshot(rect, devicePixelRatio), pageUrl);}- async function saveWholeScreenshot(pageUrl: string | null | undefined) {- const src = await takeWholeScreenshot();-+ async function saveScreenshot(src: string, pageUrl: string | null | undefined) {const { payload } = await getImagePayloadFromSrc(src, pageUrl);const resp = await triliumServerFacade.callService("POST", "clippings", payload);@@ -442,7 +521,10 @@ export default defineBackground(() => {await saveCroppedScreenshot(info.pageUrl);}else if (info.menuItemId === 'trilium-save-whole-screenshot') {- await saveWholeScreenshot(info.pageUrl);+ await saveScreenshot(await takeWholeScreenshot(), info.pageUrl);+ }+ else if (info.menuItemId === 'trilium-save-full-page-screenshot') {+ await saveScreenshot(await takeFullPageScreenshot(), info.pageUrl);}else if (info.menuItemId === 'trilium-save-image') {if (!info.srcUrl) return;@@ -506,7 +588,12 @@ export default defineBackground(() => {else if (request.name === 'save-whole-screenshot') {const activeTab = await getActiveTab();- return await saveWholeScreenshot(activeTab.url);+ return await saveScreenshot(await takeWholeScreenshot(), activeTab.url);+ }+ else if (request.name === 'save-full-page-screenshot') {+ const activeTab = await getActiveTab();++ return await saveScreenshot(await takeFullPageScreenshot(), activeTab.url);}else if (request.name === 'save-whole-page') {return await saveWholePage(request.page);@@ -554,3 +641,7 @@ function failureMessage(error: unknown) {const details = error instanceof Error ? error.message : "";return details ? `Saving to Trilium failed: ${details}` : "Saving to Trilium failed.";}++function delay(ms: number) {+ return new Promise<void>((resolve) => setTimeout(resolve, ms));+}diff --git a/apps/web-clipper/entrypoints/content/index.spec.ts b/apps/web-clipper/entrypoints/content/index.spec.tsindex c005006d5d..91ab7f6a20 100644--- a/apps/web-clipper/entrypoints/content/index.spec.ts+++ b/apps/web-clipper/entrypoints/content/index.spec.ts@@ -241,6 +241,25 @@ describe("content script", () => {});});+ it("scrolls the page when given a position, and answers where it is scrolled to", async () => {+ const scrollTo = vi.spyOn(window, "scrollTo").mockImplementation(() => {});+ vi.spyOn(window, "scrollY", "get").mockReturnValue(240);+ vi.spyOn(document.documentElement, "scrollHeight", "get").mockReturnValue(3000);+ const position = {+ scrollX: window.scrollX,+ scrollY: 240,+ viewportHeight: window.innerHeight,+ pageHeight: 3000,+ devicePixelRatio: window.devicePixelRatio+ };++ expect(await send({ name: "trilium-scroll-to" })).toEqual(position);+ expect(scrollTo).not.toHaveBeenCalled();++ expect(await send({ name: "trilium-scroll-to", x: 5, y: 240 })).toEqual(position);+ expect(scrollTo).toHaveBeenCalledWith({ left: 5, top: 240, behavior: "instant" });+ });+it("answers unknown messages with nothing", async () => {expect(await send({ name: "unknown" })).toBeUndefined();expect(console.error).toHaveBeenCalledOnce();diff --git a/apps/web-clipper/entrypoints/content/index.ts b/apps/web-clipper/entrypoints/content/index.tsindex b1fcf67d65..76766780e9 100644--- a/apps/web-clipper/entrypoints/content/index.ts+++ b/apps/web-clipper/entrypoints/content/index.ts@@ -141,7 +141,9 @@ export default defineContentScript({});}- async function prepareMessageResponse(message: {name: string, noteId?: string, message?: string, tabIds?: string[]}) {+ async function prepareMessageResponse(message: {+ name: string, noteId?: string, message?: string, tabIds?: string[], x?: number, y?: number+ }) {console.info(`Message: ${ message.name}`);if (message.name === "toast") {@@ -212,6 +214,18 @@ export default defineContentScript({devicePixelRatio: window.devicePixelRatio};}+ else if (message.name === "trilium-scroll-to") {+ if (message.y !== undefined) {+ window.scrollTo({ left: message.x, top: message.y, behavior: "instant" });+ }+ return {+ scrollX: window.scrollX,+ scrollY: window.scrollY,+ viewportHeight: window.innerHeight,+ pageHeight: document.documentElement.scrollHeight,+ devicePixelRatio: window.devicePixelRatio+ };+ }else if (message.name === "trilium-get-page-metadata") {return { description: getPageDescription() };}diff --git a/apps/web-clipper/entrypoints/offscreen/index.spec.ts b/apps/web-clipper/entrypoints/offscreen/index.spec.tsindex b60f06b937..8e7c7c64b3 100644--- a/apps/web-clipper/entrypoints/offscreen/index.spec.ts+++ b/apps/web-clipper/entrypoints/offscreen/index.spec.ts@@ -1,6 +1,9 @@import { afterEach, describe, expect, it, vi } from "vitest";import { fakeBrowser } from "wxt/testing/fake-browser";+const stitchScreenshots = vi.hoisted(() => vi.fn());+vi.mock("@/screenshot", () => ({ stitchScreenshots }));+import "./index";const CROPPED_PNG = "data:image/png;base64,Q1JPUA==";@@ -53,6 +56,16 @@ describe("offscreen document", () => {await expect(sendMessage(emptyCrop)).resolves.toBe(CROPPED_PNG);});+ it("stitches screenshots, answering with the image or why it failed", async () => {+ const request = { slices: [ { dataUrl: "a", y: 0 } ], viewportHeight: 10, pageHeight: 10 };+ stitchScreenshots.mockResolvedValueOnce(CROPPED_PNG);+ await expect(sendMessage({ type: "STITCH_IMAGES", request })).resolves.toEqual({ dataUrl: CROPPED_PNG });+ expect(stitchScreenshots).toHaveBeenCalledWith(request);++ stitchScreenshots.mockRejectedValueOnce(new Error("Too tall"));+ await expect(sendMessage({ type: "STITCH_IMAGES", request })).resolves.toEqual({ error: "Too tall" });+ });+it("ignores other messages", async () => {const other = await fakeBrowser.runtime.onMessage.trigger({ type: "OTHER" }, {}, () => {});expect(other).toEqual([ undefined ]);diff --git a/apps/web-clipper/entrypoints/offscreen/index.ts b/apps/web-clipper/entrypoints/offscreen/index.tsindex 6c63f5f4cd..6889821e21 100644--- a/apps/web-clipper/entrypoints/offscreen/index.ts+++ b/apps/web-clipper/entrypoints/offscreen/index.ts@@ -1,8 +1,18 @@+import { stitchScreenshots } from "@/screenshot";+browser.runtime.onMessage.addListener((message, _sender, sendResponse) => {if (message.type === 'CROP_IMAGE') {cropImage(message.cropRect, message.dataUrl).then(sendResponse);return true; // Keep channel open for async response}+ if (message.type === 'STITCH_IMAGES') {+ // A failure answers its message, which the background script throws again.+ stitchScreenshots(message.request).then(+ (dataUrl) => sendResponse({ dataUrl }),+ (e: Error) => sendResponse({ error: e.message })+ );+ return true;+ }});function cropImage(newArea: { x: number, y: number, width: number, height: number }, dataUrl: string) {diff --git a/apps/web-clipper/entrypoints/popup/main.tsx b/apps/web-clipper/entrypoints/popup/main.tsxindex ae59c5876c..ca14085c10 100644--- a/apps/web-clipper/entrypoints/popup/main.tsx+++ b/apps/web-clipper/entrypoints/popup/main.tsx@@ -12,6 +12,7 @@ import Globe from "@boxicons/js/icons/Globe";import HelpCircle from "@boxicons/js/icons/HelpCircle";import Highlight from "@boxicons/js/icons/Highlight";import History from "@boxicons/js/icons/History";+import ImagePortrait from "@boxicons/js/icons/ImagePortrait";import Lock from "@boxicons/js/icons/Lock";import Paperclip from "@boxicons/js/icons/Paperclip";import RefreshCw from "@boxicons/js/icons/RefreshCw";@@ -296,6 +297,11 @@ function CaptureActions({ disabled, shortcuts, draft, onDraftChange }: {disabledReason={pageDisabledReason}onClick={() => sendAndClose("save-whole-screenshot")}/>+ <ToolbarAction+ icon={ImagePortrait} label="Full page" name="Full page screenshot"+ shortcut={shortcuts.saveFullPageScreenshot} disabledReason={pageDisabledReason}+ onClick={() => sendAndClose("save-full-page-screenshot")}+ /><ToolbarActionicon={Tabs} label="Tabs" name="All tabs in window"shortcut={shortcuts.saveTabs} disabledReason={disabledReason}diff --git a/apps/web-clipper/entrypoints/popup/popup.css b/apps/web-clipper/entrypoints/popup/popup.cssindex 6d489810aa..36927a6e52 100644--- a/apps/web-clipper/entrypoints/popup/popup.css+++ b/apps/web-clipper/entrypoints/popup/popup.css@@ -322,7 +322,7 @@ body {.toolbar {display: grid;- grid-template-columns: repeat(3, 1fr);+ grid-template-columns: repeat(4, 1fr);gap: 4px;}diff --git a/apps/web-clipper/entrypoints/popup/popup.spec.tsx b/apps/web-clipper/entrypoints/popup/popup.spec.tsxindex 17075a1237..336f589efa 100644--- a/apps/web-clipper/entrypoints/popup/popup.spec.tsx+++ b/apps/web-clipper/entrypoints/popup/popup.spec.tsx@@ -86,7 +86,7 @@ describe("popup", () => {it("asks for the connection status and shows it in the footer", async () => {expect(sendMessage).toHaveBeenCalledWith({ name: "send-trilium-search-status" });expect(status()).toEqual({ dot: "status-dot status-dot-pending", text: "Looking for Trilium…" });- expect(captureButtons()).toHaveLength(4);+ expect(captureButtons()).toHaveLength(5);expect(container.querySelector(".popup-header")).toBeNull();expect([ ...container.querySelectorAll(".connection button") ].map((b) => b.getAttribute("aria-label"))).toEqual([ "Check the connection again", "Options", "Help" ]);@@ -182,7 +182,9 @@ describe("popup", () => {expect(sendMessage).toHaveBeenCalledWith({ name: "save-cropped-screenshot" });await click("Visible area screenshot");expect(sendMessage).toHaveBeenCalledWith({ name: "save-whole-screenshot" });- expect(closeWindow).toHaveBeenCalledTimes(2);+ await click("Full page screenshot");+ expect(sendMessage).toHaveBeenCalledWith({ name: "save-full-page-screenshot" });+ expect(closeWindow).toHaveBeenCalledTimes(3);await click("All tabs in window");expect(sendMessage).toHaveBeenCalledWith({ name: "save-tabs" });@@ -190,7 +192,7 @@ describe("popup", () => {expect(sendMessage).toHaveBeenCalledWith({ name: "trigger-trilium-search" });await click("Options");expect(openOptionsPage).toHaveBeenCalledOnce();- expect(closeWindow).toHaveBeenCalledTimes(2);+ expect(closeWindow).toHaveBeenCalledTimes(3);const openWindow = vi.spyOn(window, "open").mockImplementation(() => null);await click("Help");@@ -203,6 +205,7 @@ describe("popup", () => {expect(toolbar()).toEqual([{ label: "Crop", title: "Crop screenshot (Ctrl+Shift+E)" },{ label: "Screenshot", title: "Visible area screenshot" },+ { label: "Full page", title: "Full page screenshot" },{ label: "Tabs", title: "All tabs in window" }]);expect(container.querySelectorAll(".toolbar-action kbd")).toHaveLength(0);@@ -355,7 +358,7 @@ describe("popup", () => {expect(document.activeElement).toBe(container.querySelector(".page-body .note-text"));expect(container.querySelector(".page-meta")).toBeNull();expect(button("Save bookmark")?.disabled).toBe(false);- expect(captureButtons().map((action) => action.disabled)).toEqual([ false, false, false, false ]);+ expect(captureButtons().map((action) => action.disabled)).toEqual([ false, false, false, false, false ]);});it("keeps only the actions that need no access to a page out of reach", async () => {@@ -366,7 +369,7 @@ describe("popup", () => {"The extension cannot access this page.\nIf it is a regular web page, reload it.");expect(container.querySelector(".page-preview")).toBeNull();- expect(captureButtons().map((action) => action.disabled)).toEqual([ true, true, false ]);+ expect(captureButtons().map((action) => action.disabled)).toEqual([ true, true, true, false ]);expect(button("Crop screenshot")?.title).toBe(UNREACHABLE);expect(button("Visible area screenshot")?.title).toBe(UNREACHABLE);@@ -785,7 +788,7 @@ describe("icons", () => {await act(() => render(<Popup />, container));const icons = container.querySelectorAll("button > svg.icon");- expect(icons).toHaveLength(7);+ expect(icons).toHaveLength(8);for (const icon of icons) {expect(icon.getAttribute("aria-hidden")).toBe("true");expect(icon.getAttribute("viewBox")).toBe("0 0 24 24");diff --git a/apps/web-clipper/screenshot.spec.ts b/apps/web-clipper/screenshot.spec.tsnew file mode 100644index 0000000000..13d732e2e7--- /dev/null+++ b/apps/web-clipper/screenshot.spec.ts@@ -0,0 +1,88 @@+import { afterEach, describe, expect, it, vi } from "vitest";++import { MAX_SCREENSHOT_HEIGHT, stitchScreenshots } from "./screenshot";++const STITCHED = "data:image/png;base64,U1RJVENI";++describe("stitchScreenshots", () => {+ afterEach(() => {+ vi.unstubAllGlobals();+ vi.restoreAllMocks();+ });++ it("draws each capture where it was taken, scaled to device pixels", async () => {+ const images = stubImages({ naturalWidth: 1600, naturalHeight: 1000 });+ const { canvas, drawImage } = stubCanvas();++ const result = await stitchScreenshots({+ slices: [ { dataUrl: "first", y: 0 }, { dataUrl: "second", y: 500 }, { dataUrl: "last", y: 700 } ],+ viewportHeight: 500,+ pageHeight: 1200+ });++ expect(result).toBe(STITCHED);+ expect(images.map((image) => image.src)).toEqual([ "first", "second", "last" ]);+ expect([ canvas().width, canvas().height ]).toEqual([ 1600, 2400 ]);+ expect(drawImage.mock.calls).toEqual([+ [ images[0], 0, 0 ],+ [ images[1], 0, 1000 ],+ [ images[2], 0, 1400 ]+ ]);+ });++ it("keeps the canvas within the height a browser can draw", async () => {+ stubImages({ naturalWidth: 800, naturalHeight: 4000 });+ const { canvas } = stubCanvas();++ await stitchScreenshots({ slices: [ { dataUrl: "a", y: 0 } ], viewportHeight: 1000, pageHeight: 10000 });++ expect(canvas().height).toBe(MAX_SCREENSHOT_HEIGHT);+ });++ it("fails without captures, without a canvas, or on a capture that cannot be read", async () => {+ const request = { slices: [ { dataUrl: "a", y: 0 } ], viewportHeight: 100, pageHeight: 100 };+ stubImages({ naturalWidth: 100, naturalHeight: 100 });++ await expect(stitchScreenshots({ ...request, slices: [] }))+ .rejects.toThrow("There is no screenshot to stitch.");++ vi.spyOn(HTMLCanvasElement.prototype, "getContext").mockReturnValue(null);+ await expect(stitchScreenshots(request)).rejects.toThrow("The screenshot could not be drawn.");++ stubImages({ naturalWidth: 100, naturalHeight: 100, fail: true });+ await expect(stitchScreenshots(request)).rejects.toThrow("A part of the screenshot could not be read.");+ });+});++/** Replaces `Image` with one that loads at the given size, or fails to load. */+function stubImages({ naturalWidth, naturalHeight, fail = false }: {+ naturalWidth: number, naturalHeight: number, fail?: boolean+}) {+ const images: { src?: string }[] = [];+ vi.stubGlobal("Image", class {++ naturalWidth = naturalWidth;+ naturalHeight = naturalHeight;+ onload?: () => void;+ onerror?: () => void;++ constructor() {+ images.push(this);+ }++ set src(value: string) {+ Object.defineProperty(this, "src", { value });+ setTimeout(() => (fail ? this.onerror?.() : this.onload?.()));+ }++ });+ return images;+}++function stubCanvas() {+ const drawImage = vi.fn();+ const getContext = vi.spyOn(HTMLCanvasElement.prototype, "getContext")+ .mockReturnValue({ drawImage } as unknown as CanvasRenderingContext2D);+ vi.spyOn(HTMLCanvasElement.prototype, "toDataURL").mockReturnValue(STITCHED);+ return { drawImage, canvas: () => getContext.mock.contexts[0] as HTMLCanvasElement };+}diff --git a/apps/web-clipper/screenshot.ts b/apps/web-clipper/screenshot.tsnew file mode 100644index 0000000000..91d3812a54--- /dev/null+++ b/apps/web-clipper/screenshot.ts@@ -0,0 +1,62 @@+/** One capture of the visible part of a page, taken with the page scrolled to `y` CSS pixels. */+export interface ScreenshotSlice {+ dataUrl: string;+ y: number;+}++export interface StitchRequest {+ slices: ScreenshotSlice[];+ /** The height of the page's viewport, in CSS pixels. */+ viewportHeight: number;+ /** The height of the whole page, in CSS pixels. */+ pageHeight: number;+}++/**+ * The tallest image the stitched screenshot can be, in device pixels. Browsers refuse to draw a+ * canvas much taller than this, and Chrome's limit is the lowest.+ */+export const MAX_SCREENSHOT_HEIGHT = 16384;++/**+ * Draws the captures of a scrolled page onto one canvas, each at the position it was taken at, and+ * returns the result as a PNG data URL. The last capture overlaps the one before it when the page+ * height is not a multiple of the viewport height, and is drawn over it.+ */+export async function stitchScreenshots({ slices, viewportHeight, pageHeight }: StitchRequest) {+ const loaded = await Promise.all(slices.map(async (slice) => ({+ image: await loadImage(slice.dataUrl),+ y: slice.y+ })));+ const first = loaded[0]?.image;+ if (!first) {+ throw new Error("There is no screenshot to stitch.");+ }++ // The captures are in device pixels, which include the page zoom and the display's scaling.+ const scale = first.naturalHeight / viewportHeight;++ const canvas = document.createElement("canvas");+ canvas.width = first.naturalWidth;+ canvas.height = Math.min(Math.round(pageHeight * scale), MAX_SCREENSHOT_HEIGHT);++ const context = canvas.getContext("2d");+ if (!context) {+ throw new Error("The screenshot could not be drawn.");+ }++ for (const { image, y } of loaded) {+ context.drawImage(image, 0, Math.round(y * scale));+ }++ return canvas.toDataURL("image/png");+}++function loadImage(src: string) {+ return new Promise<HTMLImageElement>((resolve, reject) => {+ const image = new Image();+ image.onload = () => resolve(image);+ image.onerror = () => reject(new Error("A part of the screenshot could not be read."));+ image.src = src;+ });+}diff --git a/apps/web-clipper/wxt.config.ts b/apps/web-clipper/wxt.config.tsindex 13b32f51e4..138b69818b 100644--- a/apps/web-clipper/wxt.config.ts+++ b/apps/web-clipper/wxt.config.ts@@ -70,6 +70,9 @@ export default defineConfig({},saveTabs: {description: "Save all tabs of the current window into a note"+ },+ saveFullPageScreenshot: {+ description: "Take a screenshot of the whole page"}}}),diff --git a/docs/User Guide/User Guide/Installation & Setup/Web Clipper.md b/docs/User Guide/User Guide/Installation & Setup/Web Clipper.mdindex 81307b6738..54a2498e38 100644--- a/docs/User Guide/User Guide/Installation & Setup/Web Clipper.md+++ b/docs/User Guide/User Guide/Installation & Setup/Web Clipper.md@@ -22,7 +22,7 @@ The extension is available from the official browser web stores:* select text and clip it with the right-click context menu* click on an image or link and save it through context menu* save whole page from the popup or context menu-* save screenshot (with crop tool) from either popup or context menu+* save a screenshot of an area you select, of the visible part of the page or of the whole page, from either popup or context menu* create short text note from popup## The popup@@ -39,7 +39,8 @@ Clicking the extension's button in the browser toolbar opens its popup:The popup opens on the selection if there is one, otherwise on _Page_, showing the article, or the whole page when the page has no article. On a page that shows nothing to save, it opens on _Bookmark_ and says so. The keyboard shortcuts and the context menu still save right away, without a preview: saving the whole page that way saves the article, or the whole page when the page has no article.* Pages the extension cannot access (the browser's own pages, the extension stores, and pages that were open before the extension was installed until they are reloaded) cannot be saved or captured, so the popup says so in place of the preview, and only _Tabs_ remains available there.-* Below the preview, a row of buttons offers the other actions: _Crop_ (a screenshot of an area you select), _Screenshot_ (of the visible part of the page) and _Tabs_ (saves the links of every tab in the current window as a list of <a class="reference-link" href="../Note%20Types/Text/Link%20Previews.md">Link Previews</a>, or of plain links when _Autogenerate link previews_ is turned off). Hovering over a button shows its full name and, if it has one, its keyboard shortcut.+* Below the preview, a row of buttons offers the other actions: _Crop_ (a screenshot of an area you select), _Screenshot_ (of the visible part of the page), _Full page_ (a screenshot of the whole page) and _Tabs_ (saves the links of every tab in the current window as a list of <a class="reference-link" href="../Note%20Types/Text/Link%20Previews.md">Link Previews</a>, or of plain links when _Autogenerate link previews_ is turned off). Hovering over a button shows its full name and, if it has one, its keyboard shortcut.+* _Full page_ scrolls through the page, takes a screenshot of each part and joins them into one image, then scrolls back to where the page was. Stay on the tab until it is saved: switching to another tab stops the screenshot. Elements that stay in place while the page scrolls (such as a fixed header) appear in every part of the image, and a very long page is cut off.* If the current page was already clipped, a notice at the top offers to open the note in Trilium.* If Trilium cannot be found, the popup explains why instead of showing the actions, with buttons to look for Trilium again and to open the options.* The bottom of the popup shows whether the extension is connected to Trilium, as a colored dot (green when connected, amber when the versions are not compatible, red when Trilium was not found) next to a description of the connection, followed by the buttons for the extension's options and for this help page. A button to check the connection again appears next to them when hovering over the bar, and stays visible while Trilium cannot be reached or is not compatible.@@ -75,6 +76,7 @@ Keyboard shortcuts are available for most functions:* Save whole page: <kbd>Alt</kbd>+<kbd>Shift</kbd>+<kbd>S</kbd> (Mac: <kbd>⌥</kbd>+<kbd>⇧</kbd>+<kbd>S</kbd>)* Save screenshot: <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>E</kbd> (Mac: <kbd>⌘</kbd>+<kbd>⇧</kbd>+<kbd>E</kbd>)* Save all tabs of the current window: no default shortcut, but one can be assigned as described below.+* Save a screenshot of the whole page: no default shortcut, but one can be assigned as described below.To set custom shortcuts, follow the directions for your browser.