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

Function copyCodeFunc

contents/jianshu.tsx:80–138  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

78
79 // 一键复制
80 function copyCodeFunc() {
81 const codes = document.querySelectorAll<HTMLElement>(".hljs")
82
83 codes.forEach((code) => {
84 const button = document.createElement("button")
85 button.innerText = i18n("copy")
86 button.style.position = "absolute"
87 button.style.top = "0"
88 button.style.right = "0"
89 button.style.background = "#fff"
90 button.title = i18n("copyCode")
91 button.classList.add("Button")
92 button.classList.add("VoteButton")
93
94 code.appendChild(button)
95 code.style.position = "relative"
96
97 button.addEventListener("click", (e) => {
98 const target = e.target as HTMLElement
99 const parentPreBlock = target.closest(".hljs")
100 const codeBlock = parentPreBlock.querySelector<HTMLElement>("code")
101
102 navigator.clipboard.writeText(codeBlock.innerText)
103 setHistory((prevData) =>
104 prevData
105 ? [
106 {
107 id: uuidv4(),
108 value: codeBlock.innerText,
109 createdAt: new Date(),
110 from: "简书",
111 link: location.href,
112 tags: [],
113 remark: ""
114 },
115 ...prevData
116 ]
117 : [
118 {
119 id: uuidv4(),
120 value: codeBlock.innerText,
121 createdAt: new Date(),
122 from: "简书",
123 link: location.href,
124 tags: [],
125 remark: ""
126 }
127 ]
128 )
129
130 target.innerText = i18n("copied")
131 setTimeout(() => {
132 target.innerText = i18n("copy")
133 }, 1000)
134 e.stopPropagation()
135 e.preventDefault()
136 })
137 })

Callers 1

PlasmoOverlayFunction · 0.70

Calls 1

i18nFunction · 0.85

Tested by

no test coverage detected