aboutsummaryrefslogtreecommitdiff
path: root/apps/browser-extension/entrypoints/content
diff options
context:
space:
mode:
authorMaheshtheDev <[email protected]>2025-11-03 00:41:58 +0000
committerMaheshtheDev <[email protected]>2025-11-03 00:41:59 +0000
commit8d8d6d91aa8cdec49e7e6ef87cbc5622d0bc16ed (patch)
tree27862f7558d837615d2af6cea8c30ea03d414096 /apps/browser-extension/entrypoints/content
parentchore: update readme with recent updates and latest demo (#547) (diff)
downloadsupermemory-8d8d6d91aa8cdec49e7e6ef87cbc5622d0bc16ed.tar.xz
supermemory-8d8d6d91aa8cdec49e7e6ef87cbc5622d0bc16ed.zip
feat(browser-extension): webpages capture with markdown conversion (#548)10-31-feat_browser-extension_webpages_capture_with_markdown_conversion
Improved browser extension memory saving with better content handling and added markdown conversion. ### What changed? - Enhanced memory content handling in the background script to prioritize different content types (explicit content, highlighted text, markdown, HTML, or URL) - Added HTML to markdown conversion using TurndownService when saving entire pages - Improved HTML handling by removing script tags before processing - Updated the web app to display the saved URL from metadata when available - Added turndown library and its type definitions as dependencies
Diffstat (limited to 'apps/browser-extension/entrypoints/content')
-rw-r--r--apps/browser-extension/entrypoints/content/shared.ts64
1 files changed, 57 insertions, 7 deletions
diff --git a/apps/browser-extension/entrypoints/content/shared.ts b/apps/browser-extension/entrypoints/content/shared.ts
index d8b665c5..8c3688e0 100644
--- a/apps/browser-extension/entrypoints/content/shared.ts
+++ b/apps/browser-extension/entrypoints/content/shared.ts
@@ -1,5 +1,6 @@
import { MESSAGE_TYPES, STORAGE_KEYS } from "../../utils/constants"
import { DOMUtils } from "../../utils/ui-components"
+import { default as TurndownService } from "turndown"
export async function saveMemory() {
try {
@@ -7,15 +8,64 @@ export async function saveMemory() {
const highlightedText = window.getSelection()?.toString() || ""
const url = window.location.href
- const html = document.documentElement.outerHTML
+
+ const ogImage =
+ document
+ .querySelector('meta[property="og:image"]')
+ ?.getAttribute("content") ||
+ document
+ .querySelector('meta[name="og:image"]')
+ ?.getAttribute("content") ||
+ undefined
+
+ const title =
+ document
+ .querySelector('meta[property="og:title"]')
+ ?.getAttribute("content") ||
+ document
+ .querySelector('meta[name="og:title"]')
+ ?.getAttribute("content") ||
+ document.title ||
+ undefined
+
+ const data: {
+ html?: string
+ markdown?: string
+ highlightedText?: string
+ url: string
+ ogImage?: string
+ title?: string
+ } = {
+ url,
+ }
+
+ if (ogImage) {
+ data.ogImage = ogImage
+ }
+
+ if (title) {
+ data.title = title
+ }
+
+ if (highlightedText) {
+ data.highlightedText = highlightedText
+ } else {
+ const bodyClone = document.body.cloneNode(true) as HTMLElement
+ const scripts = bodyClone.querySelectorAll("script")
+ for (const script of scripts) {
+ script.remove()
+ }
+ const html = bodyClone.innerHTML
+
+ // Convert HTML to markdown
+ const turndownService = new TurndownService()
+ const markdown = turndownService.turndown(html)
+ data.markdown = markdown
+ }
const response = await browser.runtime.sendMessage({
action: MESSAGE_TYPES.SAVE_MEMORY,
- data: {
- html,
- highlightedText,
- url,
- },
+ data,
actionSource: "context_menu",
})
@@ -74,4 +124,4 @@ export function setupStorageListener() {
)
}
})
-} \ No newline at end of file
+}