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
fix(clipper): take one full-page screenshot at a time
Elian Doran63b85ef7d368
diff --git a/apps/web-clipper/entrypoints/background/index.spec.ts b/apps/web-clipper/entrypoints/background/index.spec.tsindex da6ab748fb..f3974a8cf4 100644--- a/apps/web-clipper/entrypoints/background/index.spec.ts+++ b/apps/web-clipper/entrypoints/background/index.spec.ts@@ -774,6 +774,25 @@ describe("background", () => {});});+ it("takes one full-page screenshot at a time, the next once the last one ends or fails", async () => {+ simulatePage({ startY: 300, viewportHeight: 500, pageHeight: 1200 });+ Object.assign(fakeBrowser.tabs, {+ get: vi.fn().mockResolvedValueOnce({ active: false }).mockResolvedValue({ active: true })+ });++ await run(Promise.all([+ sendRuntimeMessage({ name: "save-full-page-screenshot" }),+ onCommand("saveFullPageScreenshot")+ ]));++ expect(pageSteps()).toEqual([+ undefined, 0, "first", 0, "restore", 300,+ undefined, 0, "first", 0, "rest", 500, 1000, "restore", 300+ ]);+ expect(sliceTops()).toEqual([ 0, 500, 700 ]);+ expect(await lastToast()).toMatchObject({ message: "Screenshot has been saved to Trilium." });+ });+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({diff --git a/apps/web-clipper/entrypoints/background/index.ts b/apps/web-clipper/entrypoints/background/index.tsindex 53e576114e..b4d275d2b1 100644--- a/apps/web-clipper/entrypoints/background/index.ts+++ b/apps/web-clipper/entrypoints/background/index.ts@@ -77,6 +77,11 @@ export default defineBackground(() => {const triliumServerFacade = new TriliumServerFacade();/** How many popups are open: each keeps a port named "popup" connected while it is. */let openPopups = 0;+ /**+ * The last full-page screenshot asked for, which the next one waits for: both would scroll the tab,+ * and under Manifest V3 they share the offscreen document's stitcher.+ */+ let lastFullPageScreenshot: Promise<unknown> = Promise.resolve();browser.runtime.onConnect.addListener((port) => {if (port.name !== "popup") return;@@ -189,7 +194,13 @@ export default defineBackground(() => {* captures into images of the page from the top. The tab is scrolled back to where it was* afterwards. `cutOff` says the page was longer than a screenshot holds.*/- async function takeFullPageScreenshot() {+ function takeFullPageScreenshot() {+ const screenshot = lastFullPageScreenshot.then(captureFullPage);+ lastFullPageScreenshot = screenshot.catch(() => undefined);+ return screenshot;+ }++ async function captureFullPage() {const { id: tabId, windowId } = await getActiveTab();if (tabId === undefined) {throw new Error("No active tab.");