feat(extension): send page URL to import API from context menu

This commit is contained in:
Paul Huliganga 2026-03-25 00:37:18 -04:00
parent 97e55ab6c2
commit feb10fdb8b
2 changed files with 53 additions and 4 deletions

View File

@ -43,7 +43,7 @@ MVP is functionally complete (core app + docs + tests).
### Phase 4: Browser Extension (after URL import stable) ### Phase 4: Browser Extension (after URL import stable)
- [x] Scaffold browser extension project (Manifest v3) - [x] Scaffold browser extension project (Manifest v3)
- [ ] Add “Send to Recipe Manager” action to call import API - [x] Add “Send to Recipe Manager” action to call import API
- [ ] Add extension settings for Recipe Manager base URL - [ ] Add extension settings for Recipe Manager base URL
--- ---

View File

@ -1,8 +1,46 @@
const DEFAULT_BASE_URL = 'http://localhost:3000';
function normalizeBaseUrl(rawBaseUrl) {
if (typeof rawBaseUrl !== 'string' || rawBaseUrl.trim().length === 0) {
return DEFAULT_BASE_URL;
}
return rawBaseUrl.trim().replace(/\/+$/, '');
}
async function getRecipeManagerBaseUrl() {
const { recipeManagerBaseUrl } = await chrome.storage.sync.get({
recipeManagerBaseUrl: DEFAULT_BASE_URL,
});
return normalizeBaseUrl(recipeManagerBaseUrl);
}
async function sendUrlToRecipeManager(pageUrl) {
const baseUrl = await getRecipeManagerBaseUrl();
const importUrl = `${baseUrl}/api/import/url`;
const response = await fetch(importUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ url: pageUrl }),
});
if (!response.ok) {
const responseText = await response.text();
throw new Error(`Import request failed (${response.status}): ${responseText}`);
}
return response.json();
}
chrome.runtime.onInstalled.addListener(() => { chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({ chrome.contextMenus.create({
id: 'send-to-recipe-manager', id: 'send-to-recipe-manager',
title: 'Send to Recipe Manager', title: 'Send to Recipe Manager',
contexts: ['page'] contexts: ['page'],
}); });
}); });
@ -11,6 +49,17 @@ chrome.contextMenus.onClicked.addListener((info, tab) => {
return; return;
} }
// Placeholder for task: "Add 'Send to Recipe Manager' action to call import API" sendUrlToRecipeManager(tab.url)
console.info('[Recipe Manager Extension] Context menu clicked for URL:', tab.url); .then((payload) => {
console.info('[Recipe Manager Extension] Import request sent successfully', {
sourceUrl: tab.url,
success: payload?.success ?? true,
});
})
.catch((error) => {
console.error('[Recipe Manager Extension] Failed to import URL', {
sourceUrl: tab.url,
error: error instanceof Error ? error.message : String(error),
});
});
}); });