Skip to content

Build your personal knowledge base with Trilium Notes

fix(clipper): show a save's outcome on the page when its popup was closed

Elian Doranf16fd759f833
diff --git a/apps/web-clipper/entrypoints/background/index.spec.ts b/apps/web-clipper/entrypoints/background/index.spec.ts
index 6d9421414a..9fc00e653d 100644
--- a/apps/web-clipper/entrypoints/background/index.spec.ts
+++ b/apps/web-clipper/entrypoints/background/index.spec.ts
@@ -1173,6 +1173,18 @@ describe("background", () => {
facade.callService.mockRejectedValueOnce(TRILIUM_FAILURE);
await sendRuntimeMessage({ name: "save-whole-page", page });
expect(await lastToast()).toMatchObject(FAILURE_TOAST);
+
+ // A popup opened while the save runs did not ask for it, and receives no answer to show.
+ const asker = connect("popup");
+ let finishSave: ((resp: unknown) => void) | undefined;
+ facade.callService.mockImplementationOnce(() => new Promise((resolve) => finishSave = resolve));
+ const answer = sendRuntimeMessage({ name: "save-whole-page", page });
+ await vi.waitFor(() => expect(finishSave).toBeDefined());
+ asker.disconnect();
+ connect("popup");
+ finishSave?.({ noteId: "late" });
+ await answer;
+ expect(await lastToast()).toMatchObject({ message: "Page has been saved to Trilium.", noteId: "late" });
});
it("answers nothing for a crop the user cancels", async () => {
diff --git a/apps/web-clipper/entrypoints/background/index.ts b/apps/web-clipper/entrypoints/background/index.ts
index 39dce942af..130cd4430c 100644
--- a/apps/web-clipper/entrypoints/background/index.ts
+++ b/apps/web-clipper/entrypoints/background/index.ts
@@ -77,8 +77,8 @@ type ContextMenuInfo = Browser.contextMenus.OnClickData & { linkText?: string };
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 open popups: each keeps a port named "popup" connected while it is. */
+ const openPopups = new Set<Browser.runtime.Port>();
/**
* 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.
@@ -87,8 +87,8 @@ export default defineBackground(() => {
browser.runtime.onConnect.addListener((port) => {
if (port.name !== "popup") return;
- openPopups++;
- port.onDisconnect.addListener(() => openPopups--);
+ openPopups.add(port);
+ port.onDisconnect.addListener(() => openPopups.delete(port));
});
// Keyboard shortcuts
@@ -410,16 +410,18 @@ export default defineBackground(() => {
/**
* Runs a save the popup asked for, and answers it with the outcome, which the popup shows. A
* popup closed before the save ends (as it is to crop a screenshot) shows nothing, so the page
- * shows a toast instead.
+ * shows a toast instead, also when another popup was opened since.
*/
async function saveForPopup(save: () => Promise<Saved | undefined>): Promise<SaveOutcome | undefined> {
+ const askers = [ ...openPopups ];
+ const askerOpen = () => askers.some((port) => openPopups.has(port));
try {
const saved = await save();
- if (!openPopups) notify(saved);
+ if (!askerOpen()) notify(saved);
return saved;
} catch (e) {
console.error("Web clipper action failed", e);
- if (!openPopups) toast(failureMessage(e));
+ if (!askerOpen()) toast(failureMessage(e));
return { error: failureMessage(e) };
}
}