Skip to content

Commit 172dd7d

Browse files
xupeaxupea
authored andcommitted
feat: support config to decide which part should be hidden
1 parent fb81542 commit 172dd7d

File tree

9 files changed

+119
-154
lines changed

9 files changed

+119
-154
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "clean-csdn",
33
"displayName": "Clean CSDN Blog",
4-
"version": "1.0.2",
4+
"version": "1.0.3",
55
"author": "Peter Xu",
66
"description": "Just make csdn blog as clean as it should be",
77
"type": "module",

popup.html

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,19 @@
44
<meta charset="UTF-8" />
55
<link rel="icon" href="/icon/logo.ico" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7-
<title>Chrome Extension + Vanilla + TS + Vite</title>
7+
<title>Clean CSDN Blog</title>
88
</head>
99
<body>
10-
<div id="app"></div>
10+
<div id="app">
11+
<div id="siderBtn" class="item">
12+
<div class="status"><div id="siderStatus">⚫️</div></div>
13+
<div class="content">隐藏侧边栏</div>
14+
</div>
15+
<div id="loginBtn" class="item">
16+
<div class="status"><div id="loginStatus">⚫️</div></div>
17+
<div class="content">隐藏登录提示窗口</div>
18+
</div>
19+
</div>
1120
<script type="module" src="/src/popup/index.ts"></script>
1221
</body>
1322
</html>

src/background/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
console.log('background is running')
22

33
chrome.runtime.onMessage.addListener((request) => {
4-
if (request.type === 'COUNT') {
5-
console.log('background has received a message from popup, and count is ', request?.count)
6-
}
4+
chrome.tabs.reload(request.id)
75
})

src/contentScript/index.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,42 @@
1-
import './overrides.css'
1+
function injectCSS(context: string) {
2+
const style = document.createElement('style')
3+
style.type = 'text/css'
4+
5+
style.appendChild(document.createTextNode(context))
6+
7+
const head = document.getElementsByTagName('head')[0]
8+
head.appendChild(style)
9+
}
10+
11+
// Get the count value from Chrome storage
12+
chrome.storage.sync.get(['hideSider'], function (result) {
13+
const hideSider = !!result.hideSider
14+
15+
if (hideSider) {
16+
injectCSS(
17+
`aside {
18+
display: none !important;
19+
}
20+
21+
@media screen and (min-width: 1380px) {
22+
.nodata .container {
23+
width: unset !important;
24+
}
25+
}
26+
`,
27+
)
28+
}
29+
})
30+
31+
chrome.storage.sync.get(['hideLogin'], function (result) {
32+
const hideLogin = !!result.hideLogin
33+
34+
if (hideLogin) {
35+
injectCSS(
36+
`.passport-login-tip-container {
37+
display: none !important;
38+
}
39+
`,
40+
)
41+
}
42+
})

src/contentScript/overrides.css

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,11 @@
11
aside {
22
display: none !important;
33
}
4-
54
.passport-login-tip-container {
65
display: none !important;
76
}
8-
97
@media screen and (min-width: 1380px) {
108
.nodata .container {
119
width: unset !important;
1210
}
13-
14-
.nodata .container.container-concision {
15-
width: 1010px;
16-
}
17-
18-
.nodata .container main {
19-
width: 1010px;
20-
}
21-
22-
.nodata .container main #pcCommentBox pre > ol.hljs-ln {
23-
width: 490px !important;
24-
}
25-
26-
.nodata .container main .articleConDownSource {
27-
width: 560px;
28-
}
2911
}

src/manifest.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ export default defineManifest({
1515
48: 'img/logo-48.png',
1616
128: 'img/logo-128.png',
1717
},
18-
// action: {
19-
// default_popup: 'popup.html',
20-
// default_icon: 'img/logo-48.png',
21-
// },
18+
action: {
19+
default_popup: 'popup.html',
20+
default_icon: 'img/logo-48.png',
21+
},
2222
// options_page: 'options.html',
2323
// devtools_page: 'devtools.html',
24-
// background: {
25-
// service_worker: 'src/background/index.ts',
26-
// type: 'module',
27-
// },
24+
background: {
25+
service_worker: 'src/background/index.ts',
26+
type: 'module',
27+
},
2828
content_scripts: [
2929
{
3030
matches: ['https://blog.csdn.net/*'],
@@ -40,7 +40,7 @@ export default defineManifest({
4040
matches: [],
4141
},
4242
],
43-
// permissions: ['sidePanel', 'storage'],
43+
permissions: ['activeTab', 'tabs', 'storage'],
4444
// chrome_url_overrides: {
4545
// newtab: 'newtab.html',
4646
// },

src/popup/index.css

Lines changed: 18 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,30 @@
1-
:root {
2-
font-family:
3-
system-ui,
4-
-apple-system,
5-
BlinkMacSystemFont,
6-
'Segoe UI',
7-
Roboto,
8-
Oxygen,
9-
Ubuntu,
10-
Cantarell,
11-
'Open Sans',
12-
'Helvetica Neue',
13-
sans-serif;
14-
15-
color-scheme: light dark;
16-
background-color: #242424;
17-
}
18-
19-
@media (prefers-color-scheme: light) {
20-
:root {
21-
background-color: #fafafa;
22-
}
23-
24-
a:hover {
25-
color: #f3e5ab;
26-
}
27-
}
28-
291
body {
30-
min-width: 20rem;
2+
min-width: 200px;
313
margin: 0;
324
}
335

34-
main {
35-
text-align: center;
36-
padding: 1em;
37-
margin: 0 auto;
38-
}
39-
40-
h3 {
41-
color: #f3e5ab;
42-
text-transform: uppercase;
43-
font-size: 1.5rem;
44-
font-weight: 200;
45-
line-height: 1.2rem;
46-
margin: 2rem auto;
6+
#app {
7+
display: flex;
8+
flex-direction: column;
9+
font-size: 14px;
4710
}
4811

49-
.calc {
12+
.item {
5013
display: flex;
51-
justify-content: center;
14+
flex-direction: row;
15+
cursor: pointer;
16+
padding: 6px 12px;
5217
align-items: center;
53-
margin: 2rem;
54-
55-
& > button {
56-
font-size: 1rem;
57-
padding: 0.5rem 1rem;
58-
border: 1px solid #f3e5ab;
59-
border-radius: 0.25rem;
60-
background-color: transparent;
61-
color: #f3e5ab;
62-
cursor: pointer;
63-
outline: none;
64-
65-
width: 3rem;
66-
margin: 0 a;
67-
}
18+
}
6819

69-
& > label {
70-
font-size: 1.5rem;
71-
margin: 0 1rem;
72-
}
20+
.item:hover {
21+
background: linear-gradient(to bottom, #eeeeec 2.4em, #00000000 2.4em);
7322
}
7423

75-
a {
76-
font-size: 0.5rem;
77-
margin: 0.5rem;
78-
color: #cccccc;
79-
text-decoration: none;
24+
.status {
25+
width: 30px;
8026
}
27+
28+
.content {
29+
flex: 1;
30+
}

src/popup/index.ts

Lines changed: 29 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,43 @@
11
import './index.css'
22

33
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')
66

7-
// Create the main element
8-
const mainElement = document.createElement('main')
7+
let hideSider = true
8+
let hideLogin = true
99

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+
})
3215

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+
})
3720

38-
// Append the main element to the page
39-
appElement.appendChild(mainElement)
21+
const siderBtn = document.getElementById('siderBtn')
22+
const loginBtn = document.getElementById('loginBtn')
4023

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 })
4228

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 })
4732
})
4833

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 })
5838

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 })
6542
})
6643
})

vite.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,13 @@ export default defineConfig(({ mode }) => {
1616
},
1717

1818
plugins: [crx({ manifest })],
19+
20+
server: {
21+
port: 5173,
22+
strictPort: true,
23+
hmr: {
24+
port: 5173,
25+
},
26+
},
1927
}
2028
})

0 commit comments

Comments
 (0)