feat(extension): send page URL to import API from context menu
This commit is contained in:
parent
97e55ab6c2
commit
feb10fdb8b
2
TODO.md
2
TODO.md
|
|
@ -43,7 +43,7 @@ MVP is functionally complete (core app + docs + tests).
|
|||
|
||||
### Phase 4: Browser Extension (after URL import stable)
|
||||
- [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
|
||||
|
||||
---
|
||||
|
|
|
|||
|
|
@ -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.contextMenus.create({
|
||||
id: 'send-to-recipe-manager',
|
||||
title: 'Send to Recipe Manager',
|
||||
contexts: ['page']
|
||||
contexts: ['page'],
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -11,6 +49,17 @@ chrome.contextMenus.onClicked.addListener((info, tab) => {
|
|||
return;
|
||||
}
|
||||
|
||||
// Placeholder for task: "Add 'Send to Recipe Manager' action to call import API"
|
||||
console.info('[Recipe Manager Extension] Context menu clicked for URL:', tab.url);
|
||||
sendUrlToRecipeManager(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),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue