aboutsummaryrefslogtreecommitdiffstats
path: root/apps/browser-extension/src/background/background.ts
diff options
context:
space:
mode:
authorkamtschatka <sschatka@gmail.com>2024-06-09 12:23:26 +0200
committerGitHub <noreply@github.com>2024-06-09 11:23:26 +0100
commit2d0af0b10cf58140a947837d4a2504417f357406 (patch)
tree53d6221110a0438df2d100981615704a877c5fa6 /apps/browser-extension/src/background/background.ts
parent049e79d7fb8313da6d2f2289b080e67de2279109 (diff)
downloadkarakeep-2d0af0b10cf58140a947837d4a2504417f357406.tar.zst
feature(extension): Add a context menu to open your hoarder saves. Fixes #188 (#206)
* Feature request: Browser extension "Open your Hoarder saves" #188 Adds a context menu entry when a hoarder instance is configured and removes it again, if it is not configured anymore * Feature request: Browser extension "Open your Hoarder saves" #188 moved storage related functions to settings.ts fixed issues with context menu registration * only fire callback when the settings change --------- Co-authored-by: kamtschatka <simon.schatka@gmx.at> Co-authored-by: MohamedBassem <me@mbassem.com>
Diffstat (limited to 'apps/browser-extension/src/background/background.ts')
-rw-r--r--apps/browser-extension/src/background/background.ts49
1 files changed, 49 insertions, 0 deletions
diff --git a/apps/browser-extension/src/background/background.ts b/apps/browser-extension/src/background/background.ts
new file mode 100644
index 00000000..9c4604af
--- /dev/null
+++ b/apps/browser-extension/src/background/background.ts
@@ -0,0 +1,49 @@
+import {
+ getPluginSettings,
+ Settings,
+ subscribeToSettingsChanges,
+} from "../utils/settings.ts";
+
+const OPEN_HOARDER_ID = "open-hoarder";
+
+function checkSettingsState(settings: Settings) {
+ if (settings?.address) {
+ registerContextMenu();
+ } else {
+ chrome.contextMenus.remove(OPEN_HOARDER_ID);
+ }
+}
+
+/**
+ * Registers a context menu button to open a tab with the currently configured hoarder instance
+ */
+function registerContextMenu() {
+ chrome.contextMenus.create({
+ id: OPEN_HOARDER_ID,
+ title: "Open Hoarder",
+ contexts: ["action"],
+ });
+}
+
+/**
+ * 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) {
+ const { menuItemId } = info;
+ if (menuItemId === OPEN_HOARDER_ID) {
+ getPluginSettings().then((settings: Settings) => {
+ chrome.tabs.create({ url: settings.address, active: true });
+ });
+ }
+}
+
+getPluginSettings().then((settings: Settings) => {
+ checkSettingsState(settings);
+});
+
+subscribeToSettingsChanges((settings) => {
+ checkSettingsState(settings);
+});
+
+chrome.contextMenus.onClicked.addListener(handleContextMenuClick);