From 0710147ab1a3274b517acefffdaa17ece7cdd744 Mon Sep 17 00:00:00 2001 From: kamtschatka Date: Sun, 18 Aug 2024 20:52:51 +0200 Subject: [extension] Add context menu item in the browser extension. Fixes #155 (#278) * Add context menu item in the browser extension #155 Added a context menu entry to add links directly to hoarder * Formalize protocol between extension and service worker, add support for text/images beside links * fix build --------- Co-authored-by: MohamedBassem --- .../browser-extension/src/background/background.ts | 50 +++++++++++++++++++--- 1 file changed, 45 insertions(+), 5 deletions(-) (limited to 'apps/browser-extension/src/background/background.ts') diff --git a/apps/browser-extension/src/background/background.ts b/apps/browser-extension/src/background/background.ts index 9c4604af..cab58aa9 100644 --- a/apps/browser-extension/src/background/background.ts +++ b/apps/browser-extension/src/background/background.ts @@ -1,40 +1,79 @@ +import { + BookmarkTypes, + ZNewBookmarkRequest, +} from "@hoarder/shared/types/bookmarks.ts"; + import { getPluginSettings, Settings, subscribeToSettingsChanges, } from "../utils/settings.ts"; +import { NEW_BOOKMARK_REQUEST_KEY_NAME } from "./protocol.ts"; const OPEN_HOARDER_ID = "open-hoarder"; +const ADD_LINK_TO_HOARDER_ID = "add-link"; function checkSettingsState(settings: Settings) { if (settings?.address) { - registerContextMenu(); + registerContextMenus(); } else { - chrome.contextMenus.remove(OPEN_HOARDER_ID); + removeContextMenus(); } } +function removeContextMenus() { + chrome.contextMenus.remove(OPEN_HOARDER_ID); + chrome.contextMenus.remove(ADD_LINK_TO_HOARDER_ID); +} + /** - * Registers a context menu button to open a tab with the currently configured hoarder instance + * Registers + * * a context menu button to open a tab with the currently configured hoarder instance + * * a context menu button to add a link to hoarder without loading the page */ -function registerContextMenu() { +function registerContextMenus() { chrome.contextMenus.create({ id: OPEN_HOARDER_ID, title: "Open Hoarder", contexts: ["action"], }); + chrome.contextMenus.create({ + id: ADD_LINK_TO_HOARDER_ID, + title: "Add to Hoarder", + contexts: ["link", "page", "selection", "image"], + }); } /** * Reads the current settings and opens a new tab with hoarder * @param info the information about the click in the context menu */ -function handleContextMenuClick(info: chrome.contextMenus.OnClickData) { +async function handleContextMenuClick(info: chrome.contextMenus.OnClickData) { const { menuItemId } = info; if (menuItemId === OPEN_HOARDER_ID) { getPluginSettings().then((settings: Settings) => { chrome.tabs.create({ url: settings.address, active: true }); }); + } else if (menuItemId === ADD_LINK_TO_HOARDER_ID) { + let newBookmark: ZNewBookmarkRequest | null = null; + if (info.selectionText) { + newBookmark = { + type: BookmarkTypes.TEXT, + text: info.selectionText, + // TODO: Include a source url in the snippet + }; + } else if (info.srcUrl ?? info.linkUrl ?? info.pageUrl) { + newBookmark = { + type: BookmarkTypes.LINK, + url: info.srcUrl ?? info.linkUrl ?? info.pageUrl, + }; + } + if (newBookmark) { + await chrome.storage.session.set({ + [NEW_BOOKMARK_REQUEST_KEY_NAME]: newBookmark, + }); + await chrome.action.openPopup(); + } } } @@ -46,4 +85,5 @@ subscribeToSettingsChanges((settings) => { checkSettingsState(settings); }); +// eslint-disable-next-line @typescript-eslint/no-misused-promises -- Manifest V3 allows async functions for all callbacks chrome.contextMenus.onClicked.addListener(handleContextMenuClick); -- cgit v1.2.3-70-g09d2