Skip to content

Build your personal knowledge base with Trilium Notes

fix(clipper): take a queued full-page screenshot of the tab it was asked for

Elian Doranb5204afcf6bd
diff --git a/apps/web-clipper/entrypoints/background/index.spec.ts b/apps/web-clipper/entrypoints/background/index.spec.ts
index 95e38392d3..6d9421414a 100644
--- a/apps/web-clipper/entrypoints/background/index.spec.ts
+++ b/apps/web-clipper/entrypoints/background/index.spec.ts
@@ -802,6 +802,29 @@ describe("background", () => {
expect(await lastToast()).toMatchObject({ message: "Screenshot has been saved to Trilium." });
});
+ it("takes a queued screenshot of the tab it was asked for, stopping when another tab is open", async () => {
+ simulatePage({ startY: 300, viewportHeight: 500, pageHeight: 1200 });
+ const otherTab: Tab = { id: 8, title: "Other page", url: "https://example.com/other" };
+ const get = vi.fn(async (tabId: number) => {
+ tabs = [ otherTab ];
+ return { active: tabId === otherTab.id };
+ });
+ Object.assign(fakeBrowser.tabs, { get });
+
+ await run(Promise.all([
+ sendRuntimeMessage({ name: "save-full-page-screenshot" }),
+ sendRuntimeMessage({ name: "save-full-page-screenshot" })
+ ]));
+
+ expect(get.mock.calls).toEqual([ [ 7 ], [ 7 ] ]);
+ const pageTabs = tabsSendMessage.mock.calls
+ .filter(([ , message ]) => (message as { name: string }).name !== "toast")
+ .map(([ tabId ]) => tabId);
+ expect(new Set(pageTabs)).toEqual(new Set([ 7 ]));
+ expect(captureVisibleTab).not.toHaveBeenCalled();
+ expect(facade.callService).not.toHaveBeenCalled();
+ });
+
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.ts
index f1aebc21e3..39dce942af 100644
--- a/apps/web-clipper/entrypoints/background/index.ts
+++ b/apps/web-clipper/entrypoints/background/index.ts
@@ -196,14 +196,15 @@ 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.
*/
- function takeFullPageScreenshot() {
- const screenshot = lastFullPageScreenshot.then(captureFullPage);
+ async function takeFullPageScreenshot() {
+ // A queued screenshot keeps the tab it was asked for, and stops if that tab is no longer active.
+ const tab = await getActiveTab();
+ const screenshot = lastFullPageScreenshot.then(() => captureFullPage(tab));
lastFullPageScreenshot = screenshot.catch(() => undefined);
- return screenshot;
+ return await screenshot;
}
- async function captureFullPage() {
- const { id: tabId, windowId } = await getActiveTab();
+ async function captureFullPage({ id: tabId, windowId }: Browser.tabs.Tab) {
if (tabId === undefined) {
throw new Error("No active tab.");
}