diff options
Diffstat (limited to 'apps/browser-extension/src/background')
| -rw-r--r-- | apps/browser-extension/src/background/background.ts | 49 |
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);
|
