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 all commits
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"]
}
49 changes: 49 additions & 0 deletions apps/browser-extension/src/background/background.ts
Original file line number Diff line number Diff line change
@@ -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);
11 changes: 11 additions & 0 deletions apps/browser-extension/src/utils/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,14 @@ export default function usePluginSettings() {
export async function getPluginSettings() {
return (await chrome.storage.sync.get("settings")).settings as Settings;
}

export function subscribeToSettingsChanges(
callback: (settings: Settings) => void,
) {
chrome.storage.sync.onChanged.addListener((changes) => {
if (changes.settings === undefined) {
return;
}
callback(changes.settings.newValue as Settings);
});
}
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