generated from chibat/chrome-extension-typescript-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.tsx
70 lines (61 loc) · 1.98 KB
/
popup.tsx
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
import React, { useEffect, useState } from "react";
import { createRoot } from "react-dom/client";
const Popup = () => {
const [count, setCount] = useState(0);
const [standards, setStandards] = useState<string[]>([]);
const [currentURL, setCurrentURL] = useState<string>();
// useEffect(() => {
// chrome.action.setBadgeText({ text: count.toString() });
// }, [count]);
useEffect(() => {
const messageListener = (message: any) => {
if (message.elements) {
const uniqueElements = new Set<string>(message.elements);
setStandards(Array.from(uniqueElements));
setCount(uniqueElements.size);
}
};
chrome.runtime.onMessage.addListener(messageListener);
// Cleanup listener on component unmount
return () => {
chrome.runtime.onMessage.removeListener(messageListener);
};
}, []);
useEffect(() => {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
console.log(`getting info for tab ${tabs[0].id}, url=${tabs[0].url}`);
setCurrentURL(tabs[0].url);
setTimeout(() => {
chrome.tabs.sendMessage(tabs[0].id!, { action: "getElements" }, (response) => {
if (response && response.elements) {
const uniqueElements = new Set<string>(response.elements);
setStandards(Array.from(uniqueElements));
console.log({uniqueElements})
setCount(uniqueElements.size);
}
});
}, 1000)
});
// console.log("ADDING LISTENER");
// const inPage = JSON.parse(
// localStorage.getItem("ethStandards-itemsInPage") || "[]"
// );
// console.log({ inPage });
}, []);
return (
<>
<h2>ERC/EIP Elements: {count}</h2>
<ul style={{ minWidth: "700px" }}>
{standards.map((standard, index) => (
<li key={index}>{standard}</li>
))}
</ul>
</>
);
};
const root = createRoot(document.getElementById("root")!);
root.render(
<React.StrictMode>
<Popup />
</React.StrictMode>
);