forked from 027xiguapi/code-box
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsdn.tsx
265 lines (234 loc) · 6.81 KB
/
csdn.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import { Readability } from "@mozilla/readability"
import type { PlasmoCSConfig } from "plasmo"
import { useEffect, useRef } from "react"
import { v4 as uuidv4 } from "uuid"
import { useMessage } from "@plasmohq/messaging/hook"
import { useStorage } from "@plasmohq/storage/hook"
import {
addCss,
getMetaContentByProperty,
saveHtml,
saveMarkdown,
setIcon
} from "~tools"
import Turndown from "~utils/turndown"
export const config: PlasmoCSConfig = {
matches: ["https://*.blog.csdn.net/*"]
}
const turndownService = Turndown()
const documentClone = document.cloneNode(true)
const article = new Readability(documentClone as Document, {}).parse()
const articleUrl = window.location.href
const author = article.byline ?? ""
const authorLink = getMetaContentByProperty("article:author")
const domain = window.location.hostname
const csdn = () => {
const [closeAds] = useStorage<boolean>("csdn-closeAds")
const [copyCode] = useStorage<boolean>("csdn-copyCode")
const [closeFollow] = useStorage<boolean>("csdn-closeFollow")
const [closeVip] = useStorage<boolean>("csdn-closeVip")
const [autoOpenCode] = useStorage<boolean>("csdn-autoOpenCode")
const [closeLoginModal] = useStorage<boolean>("csdn-closeLoginModal")
const [closeRedirectModal] = useStorage<boolean>("csdn-closeLoginModal")
const [history, setHistory] = useStorage<any[]>("codebox-history")
const [closeLog] = useStorage("config-closeLog", true)
useEffect(() => {
closeLog ||
console.log("CSDN status", {
closeAds,
copyCode,
autoOpenCode,
closeFollow,
closeVip,
closeLoginModal,
closeRedirectModal
})
closeAds && closeAdsFunc()
copyCode && copyCodeFunc()
autoOpenCode && autoOpenCodeFunc()
closeFollow && followFunc()
closeVip && closeVipFunc()
closeLoginModal && closeLoginModalFunc()
closeRedirectModal && closeRedirectModalFunc()
setIcon(
closeAds ||
copyCode ||
autoOpenCode ||
closeFollow ||
closeLoginModal ||
closeRedirectModal
)
}, [
closeAds,
copyCode,
autoOpenCode,
closeFollow,
closeLoginModal,
closeRedirectModal
])
useMessage(async (req, res) => {
if (req.name == "csdn-isShow") {
res.send({ isShow: true })
}
if (req.name == "csdn-downloadMarkdown") {
downloadMarkdown()
}
if (req.name == "csdn-downloadHtml") {
downloadHtml()
}
})
/* 未登录复制代码 */
function copyCodeCssFunc() {
const css = `
#content_views pre,
#content_views pre code {
-webkit-touch-callout: auto !important;
-webkit-user-select: auto !important;
-khtml-user-select: auto !important;
-moz-user-select: auto !important;
-ms-user-select: auto !important;
user-select: auto !important;
}`
addCss(css)
}
function copyCodeFunc() {
copyCodeCssFunc()
// 内容区开启复制
var content_views = document.querySelector("#content_views")
content_views.replaceWith(content_views.cloneNode(true))
// 功能一: 修改复制按钮,支持一键复制
const buttons = document.querySelectorAll<HTMLElement>(".hljs-button")
buttons.forEach((btn) => {
// 更改标题
btn.dataset.title = "复制"
// 移除点击事件
btn.setAttribute("onclick", "")
// 克隆按钮
var elClone = btn.cloneNode(true)
// 替回按钮
btn.parentNode.replaceChild(elClone, btn)
// 重新添加点击事件
elClone.addEventListener("click", (e) => {
// 实现复制
const target = e.target as HTMLElement
const parentPreBlock = target.closest("pre")
const codeBlock = parentPreBlock.querySelector("code")
navigator.clipboard.writeText(codeBlock.innerText)
setHistory((prevData) =>
prevData
? [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "CSDN",
link: location.href,
tags: [],
remark: ""
},
...prevData
]
: [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "CSDN",
link: location.href,
tags: [],
remark: ""
}
]
)
target.dataset.title = "复制成功"
setTimeout(() => {
target.dataset.title = "复制"
}, 1000)
e.stopPropagation()
e.preventDefault()
})
})
}
// 关闭广告
function closeAdsFunc() {
const css = `
.toolbar-advert,
#recommendAdBox,
.adsbygoogle {
display:none !important;
}`
}
// 解除 关注博主即可阅读全文的提示
const followFunc = () => {
const readMore = document.querySelector(".btn-readmore")
if (readMore) {
const css = `
#article_content{
height: auto !important;
}
.hide-article-box {
z-index: -1 !important;
}`
addCss(css)
}
}
// 隐藏登录弹窗
function closeLoginModalFunc() {
const css = `
.passport-login-container {
display:none !important;
}`
addCss(css)
}
// 自动展开代码块
function autoOpenCodeFunc() {
const pres = Array.from(
document.querySelectorAll<HTMLElement>(
"main div.blog-content-box pre.set-code-hide"
)
)
const presBox = Array.from(
document.querySelectorAll<HTMLElement>(".hide-preCode-box")
)
const readallBox = document.querySelector<HTMLElement>(".readall_box")
pres.forEach((pre) => {
pre.style.height = "unset"
pre.style.maxHeight = "unset"
})
presBox.forEach((box) => {
box.style.display = "none"
})
if (readallBox) {
const articleContent =
document.querySelector<HTMLElement>(".article_content")
articleContent.style.height = "unset"
readallBox.style.display = "none"
}
}
// 隐藏移动端跳转APP弹窗
function closeRedirectModalFunc() {
const css = `
.ios-shadowbox,
.feed-Sign-weixin,
.weixin-shadowbox {
display:none !important;
}`
addCss(css)
}
function closeVipFunc() {
document.querySelectorAll(".hide-article-box").forEach((box) => {
box.remove()
})
}
function downloadMarkdown() {
const html = document.querySelector(".blog-content-box")
const markdown = turndownService.turndown(html)
saveMarkdown(markdown, article.title)
}
function downloadHtml() {
const dom = document.querySelector(".blog-content-box")
saveHtml(dom, article.title)
}
return <div style={{ display: "none" }}></div>
}
export default csdn