diff options
| author | Dhravya <[email protected]> | 2024-07-04 19:58:26 -0500 |
|---|---|---|
| committer | Dhravya <[email protected]> | 2024-07-04 19:58:26 -0500 |
| commit | d4f9aca010b0c7bc2ab81358eb73b0413bdeea03 (patch) | |
| tree | fc401f17bcabb48a5f5566a98ef44a91f89c226a /apps | |
| parent | revamped extention (diff) | |
| download | supermemory-d4f9aca010b0c7bc2ab81358eb73b0413bdeea03.tar.xz supermemory-d4f9aca010b0c7bc2ab81358eb73b0413bdeea03.zip | |
added context menu saving
Diffstat (limited to 'apps')
| -rw-r--r-- | apps/extension/background.ts | 130 | ||||
| -rw-r--r-- | apps/extension/content/ContentApp.tsx | 7 | ||||
| -rw-r--r-- | apps/extension/manifest.json | 4 |
3 files changed, 138 insertions, 3 deletions
diff --git a/apps/extension/background.ts b/apps/extension/background.ts index df4d0375..406493f1 100644 --- a/apps/extension/background.ts +++ b/apps/extension/background.ts @@ -364,3 +364,133 @@ chrome.runtime.onInstalled.addListener(function (details) { }); } }); + +chrome.runtime.onInstalled.addListener(() => { + chrome.contextMenus.create({ + id: "saveSelection", + title: "Save note to Supermemory", + contexts: ["selection"], + }); + + chrome.contextMenus.create({ + id: "savePage", + title: "Save page to Supermemory", + contexts: ["page"], + }); + + // TODO + // chrome.contextMenus.create({ + // id: 'saveLink', + // title: 'Save link to Supermemory', + // contexts: ['link'], + // }); +}); + +interface FetchDataParams { + content: string; + url: string; + title: string; + description: string; + ogImage: string; + favicon: string; + isExternalContent: boolean; // Indicates if the content is from an external API +} + +const fetchData = ({ + content, + url, + title, + description, + ogImage, + favicon, + isExternalContent, +}: FetchDataParams) => { + // Construct the URL + const finalUrl = isExternalContent + ? url + : `${url}#supermemory-stuff-${Math.random()}`; + + // Construct the body + const body = JSON.stringify({ + pageContent: content, + url: finalUrl, + title, + spaces: [], + description, + ogImage, + image: favicon, + }); + + // Make the fetch call + fetch(`${BACKEND_URL}/api/store`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: body, + }) + .then((response) => { + console.log("Data saved successfully"); + }) + .catch((error) => { + console.error("Error saving data:", error); + }); + + return Promise.resolve(); +}; + +chrome.contextMenus.onClicked.addListener((info, tab) => { + if (!tab || !tab.id) return; + + const tabId = tab.id; + + const sendMessageToTab = (message: string) => { + chrome.tabs.sendMessage(tabId, { message, type: "supermemory-message" }); + }; + + if (info.menuItemId === "saveSelection" && info.selectionText) { + sendMessageToTab("Saving selection..."); + fetchData({ + content: info.selectionText || "No content", + url: info.pageUrl, + title: tab.title || "Selection Title", + description: "User-selected content from the page", + ogImage: "", + favicon: "", + isExternalContent: false, + }) + .then(() => { + sendMessageToTab("Selection saved successfully."); + }) + .catch(() => { + sendMessageToTab("Failed to save selection."); + }); + } else if (info.menuItemId === "savePage") { + sendMessageToTab("Saving page..."); + chrome.scripting.executeScript( + { + target: { tabId: tabId }, + func: () => document.body.innerText, + }, + (results) => { + if (results.length > 0 && results[0].result) { + fetchData({ + content: results[0].result as string, + url: info.pageUrl, + title: tab.title || "Page Title", + description: "Full page content", + ogImage: "", + favicon: "", + isExternalContent: false, + }) + .then(() => { + sendMessageToTab("Page saved successfully."); + }) + .catch(() => { + sendMessageToTab("Failed to save page."); + }); + } + }, + ); + } +}); diff --git a/apps/extension/content/ContentApp.tsx b/apps/extension/content/ContentApp.tsx index 1dea7645..d82857e2 100644 --- a/apps/extension/content/ContentApp.tsx +++ b/apps/extension/content/ContentApp.tsx @@ -116,7 +116,6 @@ export default function ContentApp({ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { if (request.type === "import-update") { - console.log(request); setIsImporting(true); setImportedCount(request.importedCount); } @@ -125,6 +124,12 @@ export default function ContentApp({ setIsImporting(false); setImportDone(true); } + + if (request.type === "supermemory-message") { + toast({ + title: request.message, + }); + } }); const portalDiv = document.createElement("div"); diff --git a/apps/extension/manifest.json b/apps/extension/manifest.json index 144f9d5e..69d32ac2 100644 --- a/apps/extension/manifest.json +++ b/apps/extension/manifest.json @@ -22,6 +22,6 @@ "matches": ["<all_urls>"] } ], - "permissions": ["webRequest", "storage"], - "host_permissions": ["https://x.com/*", "https://twitter.com/*"] + "permissions": ["webRequest", "storage", "contextMenus"], + "host_permissions": ["<all_urls>"] } |