added context menu saving

This commit is contained in:
Dhravya 2024-07-04 19:58:26 -05:00
parent 6ef746da54
commit d4f9aca010
3 changed files with 138 additions and 3 deletions

View file

@ -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.");
});
}
},
);
}
});

View file

@ -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");

View file

@ -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>"]
}