|
1 | 1 | import './index.css' |
2 | 2 |
|
3 | 3 | document.addEventListener('DOMContentLoaded', () => { |
4 | | - const appElement = document.getElementById('app')! |
5 | | - const link = 'https://github.com/guocaoyi/create-chrome-ext' |
| 4 | + const siderStatus = document.getElementById('siderStatus') |
| 5 | + const loginStatus = document.getElementById('loginStatus') |
6 | 6 |
|
7 | | - // Create the main element |
8 | | - const mainElement = document.createElement('main') |
| 7 | + let hideSider = true |
| 8 | + let hideLogin = true |
9 | 9 |
|
10 | | - // Create the title element |
11 | | - const h3Element = document.createElement('h3') |
12 | | - h3Element.textContent = 'Popup Page' |
13 | | - |
14 | | - // Create the counter element |
15 | | - const divElement = document.createElement('div') |
16 | | - divElement.className = 'calc' |
17 | | - const minusButton = document.createElement('button') |
18 | | - minusButton.textContent = '-' |
19 | | - const countLabel = document.createElement('label') |
20 | | - countLabel.textContent = '0' |
21 | | - const addButton = document.createElement('button') |
22 | | - addButton.textContent = '+' |
23 | | - divElement.appendChild(minusButton) |
24 | | - divElement.appendChild(countLabel) |
25 | | - divElement.appendChild(addButton) |
26 | | - |
27 | | - // Create the link element |
28 | | - const aElement = document.createElement('a') |
29 | | - aElement.href = link |
30 | | - aElement.target = '_blank' |
31 | | - aElement.textContent = 'generated by create-chrome-ext' |
| 10 | + // Get the count value from Chrome storage |
| 11 | + chrome.storage.sync.get(['hideSider'], function (result) { |
| 12 | + hideSider = !!result.hideSider |
| 13 | + siderStatus!.style.visibility = hideSider ? 'visible' : 'hidden' |
| 14 | + }) |
32 | 15 |
|
33 | | - // Append all elements to the main element |
34 | | - mainElement.appendChild(h3Element) |
35 | | - mainElement.appendChild(divElement) |
36 | | - mainElement.appendChild(aElement) |
| 16 | + chrome.storage.sync.get(['hideLogin'], function (result) { |
| 17 | + hideLogin = !!result.hideLogin |
| 18 | + loginStatus!.style.visibility = hideLogin ? 'visible' : 'hidden' |
| 19 | + }) |
37 | 20 |
|
38 | | - // Append the main element to the page |
39 | | - appElement.appendChild(mainElement) |
| 21 | + const siderBtn = document.getElementById('siderBtn') |
| 22 | + const loginBtn = document.getElementById('loginBtn') |
40 | 23 |
|
41 | | - let count = 0 |
| 24 | + siderBtn?.addEventListener('click', async () => { |
| 25 | + hideSider = !hideSider |
| 26 | + siderStatus!.style.visibility = hideSider ? 'visible' : 'hidden' |
| 27 | + await chrome.storage.sync.set({ hideSider }) |
42 | 28 |
|
43 | | - // Get the count value from Chrome storage |
44 | | - chrome.storage.sync.get(['count'], function (result) { |
45 | | - count = result.count || 0 |
46 | | - countLabel.textContent = `${count}` |
| 29 | + const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true }) |
| 30 | + // await chrome.tabs.sendMessage(activeTab.id!, { hideSider }) |
| 31 | + chrome.runtime.sendMessage({ id: activeTab.id!, hideSider }) |
47 | 32 | }) |
48 | 33 |
|
49 | | - // Decrement the count |
50 | | - minusButton.addEventListener('click', function () { |
51 | | - if (count > 0) { |
52 | | - count-- |
53 | | - countLabel.textContent = `${count}` |
54 | | - chrome.storage.sync.set({ count }) |
55 | | - chrome.runtime.sendMessage({ type: 'COUNT', count }) |
56 | | - } |
57 | | - }) |
| 34 | + loginBtn?.addEventListener('click', async () => { |
| 35 | + hideLogin = !hideLogin |
| 36 | + loginStatus!.style.visibility = hideLogin ? 'visible' : 'hidden' |
| 37 | + await chrome.storage.sync.set({ hideLogin }) |
58 | 38 |
|
59 | | - // Increment the count |
60 | | - addButton.addEventListener('click', function () { |
61 | | - count++ |
62 | | - countLabel.textContent = `${count}` |
63 | | - chrome.storage.sync.set({ count }) |
64 | | - chrome.runtime.sendMessage({ type: 'COUNT', count }) |
| 39 | + const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true }) |
| 40 | + // await chrome.tabs.sendMessage(activeTab.id!, { hideLogin }) |
| 41 | + chrome.runtime.sendMessage({ id: activeTab.id!, hideLogin }) |
65 | 42 | }) |
66 | 43 | }) |
0 commit comments