-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.tsx
63 lines (57 loc) · 1.37 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
import React, { useEffect, useState } from 'react';
import ReactDOM from 'react-dom';
function Popup() {
const [count, setCount] = useState(0);
const [currentURL, setCurrentURL] = useState<string>();
useEffect(() => {
chrome.action.setBadgeText({ text: count.toString() });
}, [count]);
useEffect(() => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
setCurrentURL(tabs[0].url);
});
}, []);
const changeBackground = () => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const tab = tabs[0];
if (tab.id) {
chrome.tabs.sendMessage(
tab.id,
{
color: '#555555',
},
(msg) => {
// console.log('result message:', msg);
},
);
}
});
};
return (
<>
<ul style={{ minWidth: '700px' }}>
<li>
Current URL:
{currentURL}
</li>
<li>
Current Time:
{new Date().toLocaleTimeString()}
</li>
</ul>
<button
onClick={() => setCount(count + 1)}
style={{ marginRight: '5px' }}
>
count up
</button>
<button onClick={changeBackground}>change background</button>
</>
);
}
ReactDOM.render(
<React.StrictMode>
<Popup />
</React.StrictMode>,
document.getElementById('root'),
);