forked from 027xiguapi/code-box
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjb51.tsx
128 lines (105 loc) · 3.52 KB
/
jb51.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
import type { PlasmoCSConfig } from "plasmo"
import { useEffect } from "react"
import { useStorage } from "@plasmohq/storage/hook"
import { addCss } from "~tools"
export const config: PlasmoCSConfig = {
matches: ["https://*.jb51.net/*"]
}
window.addEventListener("load", () => {
console.log("脚本之家加载完成,执行代码")
})
export default function jb51() {
const [closeAds] = useStorage<boolean>("jb51-closeAds")
const [copyCode] = useStorage<boolean>("jb51-copyCode")
useEffect(() => {
console.log("jb51 status", { closeAds, copyCode })
closeAds && closeAdsFunc()
copyCode && copyCodeFunc()
}, [closeAds, copyCode])
/* 未登录复制代码 */
function copyCodeCssFunc() {
addCss(`
#article .jb51code,
#article .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;
}`)
}
function copyCodeFunc() {
copyCodeCssFunc()
// 内容区开启复制
var content_views = document.querySelector("#article")
content_views && content_views.replaceWith(content_views.cloneNode(true))
// 功能一: 修改复制按钮,支持一键复制
const buttons = document.querySelectorAll<HTMLElement>(".codetool .copy")
if (buttons.length > 0) {
buttons.forEach((btn) => {
// 更改标题
btn.innerText = "复制"
// 移除点击事件
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(".jb51code")
const codeBlock = parentPreBlock.querySelector<HTMLElement>(".code")
navigator.clipboard.writeText(codeBlock.innerText)
target.innerText = "复制成功"
setTimeout(() => {
target.innerText = "复制"
}, 1000)
e.stopPropagation()
e.preventDefault()
})
})
} else {
const codes = document.querySelectorAll<HTMLElement>("article .jb51code")
codes.forEach((code) => {
const button = document.createElement("button")
button.innerText = "复制"
button.style.position = "absolute"
button.style.top = "0"
button.style.right = "0"
button.title = "一键复制代码"
button.classList.add("Button")
button.classList.add("VoteButton")
code.appendChild(button)
code.style.position = "relative"
button.addEventListener("click", (e) => {
// 实现复制
const target = e.target as HTMLElement
const parentPreBlock = target.closest(".jb51code")
const codeBlock = parentPreBlock.querySelector<HTMLElement>("pre")
navigator.clipboard.writeText(codeBlock.innerText)
target.innerText = "复制成功"
setTimeout(() => {
target.innerText = "复制"
}, 1000)
e.stopPropagation()
e.preventDefault()
})
})
}
}
// 关闭广告
function closeAdsFunc() {
addCss(`
.tipsa_ds,
#txtlink,
#r1gg,
#idctu,
#rbbd {
display:none !important;
}`)
}
return <div style={{ display: "none" }}></div>
}