forked from 027xiguapi/code-box
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnblogs.tsx
139 lines (120 loc) · 3.86 KB
/
cnblogs.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
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 { Readability } from "~node_modules/@mozilla/readability"
import {
getMetaContentByProperty,
saveHtml,
saveMarkdown,
setIcon
} from "~tools"
import Turndown from "~utils/turndown"
export const config: PlasmoCSConfig = {
matches: ["https://*.cnblogs.com/*"],
all_frames: true
}
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
export default function cnblogs() {
const [copyCode] = useStorage<boolean>("cnblogs-copyCode")
const [history, setHistory] = useStorage<any[]>("codebox-history")
const [closeLog] = useStorage("config-closeLog", true)
useEffect(() => {
closeLog || console.log("cnblogs copyCode", copyCode)
copyCode && copyCodeFunc()
setIcon(copyCode)
}, [copyCode])
useMessage(async (req, res) => {
if (req.name == "cnblogs-isShow") {
res.send({ isShow: true })
}
if (req.name == "cnblogs-downloadMarkdown") {
downloadMarkdown()
}
if (req.name == "cnblogs-downloadHtml") {
downloadHtml()
}
})
// 功能一: 修改复制按钮,支持一键复制
function copyCodeFunc() {
const toolbars = document.querySelectorAll<HTMLElement>(
".cnblogs_code_toolbar"
)
toolbars.forEach((toolbar) => {
const button = document.createElement("button")
button.innerText = "复制"
button.style.float = "right"
button.title = "一键复制代码"
button.classList.add("copy-btn")
toolbar.appendChild(button)
})
const buttons = document.querySelectorAll<HTMLElement>(
".cnblogs_code_toolbar .copy-btn"
)
buttons.forEach((btn) => {
// 移除点击事件
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(".cnblogs_code")
const codeBlock = parentPreBlock.querySelector<HTMLElement>("pre")
navigator.clipboard.writeText(codeBlock.innerText)
setHistory((prevData) =>
prevData
? [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "博客园",
link: location.href,
tags: [],
remark: ""
},
...prevData
]
: [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "博客园",
link: location.href,
tags: [],
remark: ""
}
]
)
target.innerText = "复制成功"
setTimeout(() => {
target.innerText = "复制"
}, 1000)
e.stopPropagation()
e.preventDefault()
})
})
}
function downloadMarkdown() {
const html = document.querySelector("#post_detail")
const markdown = turndownService.turndown(html)
saveMarkdown(markdown, article.title)
}
function downloadHtml() {
const dom = document.querySelector("#post_detail")
saveHtml(dom, article.title)
}
return <div style={{ display: "none" }}></div>
}