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

Function copyCodeFunc

contents/51cto.tsx:91–213  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

89 }
90
91 function copyCodeFunc() {
92 copyCodeCssFunc()
93 // 内容区开启复制
94 const content_views = document.querySelector("article")
95 content_views && content_views.replaceWith(content_views.cloneNode(true))
96
97 // 功能一: 修改复制按钮,支持一键复制
98 const buttons = document.querySelectorAll<HTMLElement>(
99 ".hljs-cto .copy_btn"
100 )
101
102 if (buttons.length > 0) {
103 buttons.forEach((btn) => {
104 btn.innerText = i18n("copy")
105 btn.setAttribute("onclick", "")
106 const elClone = btn.cloneNode(true)
107
108 btn.parentNode.replaceChild(elClone, btn)
109 elClone.addEventListener("click", (e) => {
110 const target = e.target as HTMLElement
111 const parentPreBlock = target.closest(".hljs-cto")
112 const codeBlock = parentPreBlock.querySelector<HTMLElement>("pre")
113 const codeIndex =
114 codeBlock.querySelector<HTMLElement>(".pre-numbering")
115 codeBlock.removeChild(codeIndex)
116
117 navigator.clipboard.writeText(codeBlock.innerText)
118 setHistory((prevData) =>
119 prevData
120 ? [
121 {
122 id: uuidv4(),
123 value: codeBlock.innerText,
124 createdAt: new Date(),
125 from: "51CTO",
126 link: location.href,
127 tags: [],
128 remark: ""
129 },
130 ...prevData
131 ]
132 : [
133 {
134 id: uuidv4(),
135 value: codeBlock.innerText,
136 createdAt: new Date(),
137 from: "51CTO",
138 link: location.href,
139 tags: [],
140 remark: ""
141 }
142 ]
143 )
144 target.innerText = i18n("copied")
145 setTimeout(() => {
146 target.innerText = i18n("copy")
147 }, 1000)
148 e.stopPropagation()

Callers 1

PlasmoOverlayFunction · 0.70

Calls 2

i18nFunction · 0.85
copyCodeCssFuncFunction · 0.70

Tested by

no test coverage detected