Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Browser extension "Open your Hoarder saves" #188 #206

Merged
merged 3 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion apps/browser-extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"action": {
"default_popup": "index.html"
},
"background": {
"service_worker": "src/background/background.ts"
},
"options_ui": {
"page": "index.html#options",
"open_in_tab": false
Expand All @@ -23,5 +26,5 @@
"content_security_policy": {
"extension_pages": "script-src 'self'; object-src 'self'"
},
"permissions": ["storage", "tabs"]
"permissions": ["storage", "tabs", "contextMenus"]
}
45 changes: 45 additions & 0 deletions apps/browser-extension/src/background/background.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Settings } from "../utils/settings.ts";

const OPEN_HOARDER_ID = "open-hoarder";

function checkSettingsState(settings: Settings) {
if (settings?.address) {
registerContextMenu(settings.address);
} else {
chrome.contextMenus.remove(OPEN_HOARDER_ID);
}
}

/**
* Registers a context menu button to open a tab with the currently configured hoarder instance
* @param address The address of the configured hoarder instance
*/
function registerContextMenu(address: string) {
chrome.contextMenus.create({
id: OPEN_HOARDER_ID,
title: "Open Hoarder",
contexts: ["action"],
});
chrome.contextMenus.onClicked.addListener(createContextClickHandler(address));
}
kamtschatka marked this conversation as resolved.
Show resolved Hide resolved

/**
*
* @param address the address of the hoarder instance
* @returns an event handler that will open a tab with the hoarder instance
*/
function createContextClickHandler(address: string) {
return (info: chrome.contextMenus.OnClickData) => {
const { menuItemId } = info;
if (menuItemId === OPEN_HOARDER_ID) {
chrome.tabs.create({ url: address, active: true });
}
};
}

chrome.storage.sync.get("settings", (items) =>
checkSettingsState(items.settings as Settings),
);
kamtschatka marked this conversation as resolved.
Show resolved Hide resolved
chrome.storage.sync.onChanged.addListener((changes) => {
checkSettingsState(changes.settings.newValue as Settings);
});
kamtschatka marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions apps/browser-extension/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"module": "ESNext",
"skipLibCheck": true,

"types": ["chrome"],

"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
Expand Down
Loading