MCPcopy Create free account
hub / github.com/027xiguapi/code-box / copyCodeFunc

Function copyCodeFunc

contents/zhihu.tsx:107–167  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

105
106 // 功能一: 修改复制按钮,支持一键复制
107 function copyCodeFunc() {
108 const codes = document.querySelectorAll<HTMLElement>(
109 ".RichContent .highlight"
110 )
111
112 codes.forEach((code) => {
113 const button = document.createElement("button")
114 button.innerText = i18n("copy")
115 button.style.position = "absolute"
116 button.style.top = "0"
117 button.style.right = "0"
118 button.title = i18n("copyCode")
119 button.classList.add("Button")
120 button.classList.add("VoteButton")
121
122 code.appendChild(button)
123 code.style.position = "relative"
124
125 button.addEventListener("click", (e) => {
126 // 实现复制
127 const target = e.target as HTMLElement
128 const parentPreBlock = target.closest(".highlight")
129 const codeBlock = parentPreBlock.querySelector<HTMLElement>("pre")
130
131 navigator.clipboard.writeText(codeBlock.innerText)
132 setHistory((prevData) =>
133 prevData
134 ? [
135 {
136 id: uuidv4(),
137 value: codeBlock.innerText,
138 createdAt: new Date(),
139 from: "知乎",
140 link: location.href,
141 tags: [],
142 remark: ""
143 },
144 ...prevData
145 ]
146 : [
147 {
148 id: uuidv4(),
149 value: codeBlock.innerText,
150 createdAt: new Date(),
151 from: "知乎",
152 link: location.href,
153 tags: [],
154 remark: ""
155 }
156 ]
157 )
158
159 target.innerText = i18n("copied")
160 setTimeout(() => {
161 target.innerText = i18n("copy")
162 }, 1000)
163 e.stopPropagation()
164 e.preventDefault()

Callers 1

PlasmoOverlayFunction · 0.70

Calls 1

i18nFunction · 0.85

Tested by

no test coverage detected