Skip to content

Build your personal knowledge base with Trilium Notes

fix(web-clipper): duplicate context menu error

Elian Doran810563b3f998
diff --git a/apps/web-clipper/entrypoints/background/context_menu.ts b/apps/web-clipper/entrypoints/background/context_menu.ts
new file mode 100644
index 0000000000..6685651fbd
--- /dev/null
+++ b/apps/web-clipper/entrypoints/background/context_menu.ts
@@ -0,0 +1,42 @@
+const CONTEXT_MENU_ITEMS: Browser.contextMenus.CreateProperties[] = [
+ {
+ id: "trilium-save-selection",
+ title: "Save selection to Trilium",
+ contexts: ["selection"]
+ },
+ {
+ id: "trilium-save-cropped-screenshot",
+ title: "Crop screen shot to Trilium",
+ contexts: ["page"]
+ },
+ {
+ id: "trilium-save-whole-screenshot",
+ title: "Save whole screen shot to Trilium",
+ contexts: ["page"]
+ },
+ {
+ id: "trilium-save-page",
+ title: "Save whole page to Trilium",
+ contexts: ["page"]
+ },
+ {
+ id: "trilium-save-link",
+ title: "Save link to Trilium",
+ contexts: ["link"]
+ },
+ {
+ id: "trilium-save-image",
+ title: "Save image to Trilium",
+ contexts: ["image"]
+ }
+];
+
+export default function setupContextMenu() {
+ // Context menu items need to be registered only once.
+ // https://stackoverflow.com/questions/64318529/cannot-create-item-with-duplicate-context-menu-id-in-extension
+ browser.runtime.onInstalled.addListener(() => {
+ for (const item of CONTEXT_MENU_ITEMS) {
+ browser.contextMenus.create(item);
+ }
+ });
+}
diff --git a/apps/web-clipper/entrypoints/background/index.ts b/apps/web-clipper/entrypoints/background/index.ts
index 8657ee5117..7d99abc997 100644
--- a/apps/web-clipper/entrypoints/background/index.ts
+++ b/apps/web-clipper/entrypoints/background/index.ts
@@ -1,5 +1,6 @@
import { randomString, Rect } from "@/utils";
+import setupContextMenu from "./context_menu";
import TriliumServerFacade from "./trilium_server_facade";
type BackgroundMessage = {
@@ -40,6 +41,8 @@ export default defineBackground(() => {
}
});
+ setupContextMenu();
+
function cropImageManifestV2(newArea: Rect, dataUrl: string) {
return new Promise((resolve, reject) => {
const img = new Image();
@@ -115,42 +118,6 @@ export default defineBackground(() => {
return await browser.tabs.captureVisibleTab({ format: 'png' });
}
- browser.contextMenus.create({
- id: "trilium-save-selection",
- title: "Save selection to Trilium",
- contexts: ["selection"]
- });
-
- browser.contextMenus.create({
- id: "trilium-save-cropped-screenshot",
- title: "Crop screen shot to Trilium",
- contexts: ["page"]
- });
-
- browser.contextMenus.create({
- id: "trilium-save-whole-screenshot",
- title: "Save whole screen shot to Trilium",
- contexts: ["page"]
- });
-
- browser.contextMenus.create({
- id: "trilium-save-page",
- title: "Save whole page to Trilium",
- contexts: ["page"]
- });
-
- browser.contextMenus.create({
- id: "trilium-save-link",
- title: "Save link to Trilium",
- contexts: ["link"]
- });
-
- browser.contextMenus.create({
- id: "trilium-save-image",
- title: "Save image to Trilium",
- contexts: ["image"]
- });
-
async function getActiveTab() {
const tabs = await browser.tabs.query({
active: true,