aboutsummaryrefslogtreecommitdiff
path: root/apps/browser-extension/entrypoints/content/shared.ts
blob: 68d117a1adb2e46ff7cec20afd1163a092585a02 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { MESSAGE_TYPES } from "../../utils/constants"
import { bearerToken, userData } from "../../utils/storage"
import { DOMUtils } from "../../utils/ui-components"
import { default as TurndownService } from "turndown"

export async function saveMemory() {
	try {
		DOMUtils.showToast("loading")

		const highlightedText = window.getSelection()?.toString() || ""
		const url = window.location.href

		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,
			actionSource: "context_menu",
		})

		console.log("Response from enxtension:", response)
		if (response.success) {
			DOMUtils.showToast("success")
		} else {
			DOMUtils.showToast("error")
		}
	} catch (error) {
		console.error("Error saving memory:", error)
		DOMUtils.showToast("error")
	}
}

export function setupGlobalKeyboardShortcut() {
	document.addEventListener("keydown", async (event) => {
		if (
			(event.ctrlKey || event.metaKey) &&
			event.shiftKey &&
			event.key === "m"
		) {
			event.preventDefault()
			await saveMemory()
		}
	})
}

export function setupStorageListener() {
	window.addEventListener("message", async (event) => {
		if (event.source !== window) {
			return
		}
		const token = event.data.token
		const user = event.data.userData
		if (token && user) {
			if (
				!(
					window.location.hostname === "localhost" ||
					window.location.hostname === "supermemory.ai" ||
					window.location.hostname === "app.supermemory.ai"
				)
			) {
				console.log(
					"Bearer token and user data is only allowed to be used on localhost or supermemory.ai",
				)
				return
			}

			try {
				await Promise.all([
					bearerToken.setValue(token),
					userData.setValue(user),
				])
			} catch {
				// Do nothing
			}
		}
	})
}