aboutsummaryrefslogtreecommitdiff
path: root/apps/extension/content/content.tsx
blob: 1a80774c8995e4c0d2abe6bfea560e17273375a2 (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
import ReactDOM from "react-dom/client";
import ContentApp from "./ContentApp";
import("./base.css");

setTimeout(initial, 1000);

const appendTailwindStyleData = (shadowRoot: ShadowRoot) => {
	const styleSheet = document.createElement("style");

	const path = chrome.runtime.getURL("../public/output.css");

	fetch(path)
		.then((response) => response.text())
		.then((css) => {
			styleSheet.textContent = css;
			shadowRoot.appendChild(styleSheet);
		});
};

function initial() {
	// Create a new div element to host the shadow root.
	// Styles for this div is in `content/content.css`
	const hostDiv = document.createElement("div");
	hostDiv.id = "supermemory-extension-host";
	document.body.appendChild(hostDiv);

	// Attach the shadow DOM to the hostDiv and set the mode to
	// 'open' for accessibility from JavaScript.
	// Ref https://developer.mozilla.org/en-US/docs/Web/API/Element/attachShadow
	const shadowRoot = hostDiv.attachShadow({ mode: "open" });

	// Create a new div element that will be the root container for the React app
	const rootDiv = document.createElement("div");
	rootDiv.id = "supermemory-extension-root";
	shadowRoot.appendChild(rootDiv);

	appendTailwindStyleData(shadowRoot);

	const root = ReactDOM.createRoot(rootDiv);

	const jwt = chrome.storage.local.get("jwt").then((data) => {
		return data.jwt;
	}) as Promise<string | undefined>;

	jwt.then((token) =>
		root.render(<ContentApp shadowRoot={shadowRoot} token={token} />),
	);
}

window.addEventListener("message", (event) => {
	if (event.source !== window) {
		return;
	}
	const jwt = event.data.token;

	if (jwt) {
		if (
			!(
				window.location.hostname === "localhost" ||
				window.location.hostname === "supermemory.ai" ||
				window.location.hostname === "beta.supermemory.ai"
			)
		) {
			console.log(
				"JWT is only allowed to be used on localhost or supermemory.ai",
			);
			return;
		}

		chrome.storage.local.set({ jwt }, () => {});
	}
});