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

Function copyCodeFunc

contents/cnblogs.tsx:75–146  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

73
74 // 功能一: 修改复制按钮,支持一键复制
75 function copyCodeFunc() {
76 const toolbars = document.querySelectorAll<HTMLElement>(
77 ".cnblogs_code_toolbar"
78 )
79
80 toolbars.forEach((toolbar) => {
81 const button = document.createElement("button")
82 button.innerText = i18n("copy")
83 button.style.float = "right"
84 button.title = i18n("copyCode")
85 button.classList.add("copy-btn")
86
87 toolbar.appendChild(button)
88 })
89
90 const buttons = document.querySelectorAll<HTMLElement>(
91 ".cnblogs_code_toolbar .copy-btn"
92 )
93
94 buttons.forEach((btn) => {
95 // 移除点击事件
96 btn.setAttribute("onclick", "")
97
98 // 克隆按钮
99 var elClone = btn.cloneNode(true)
100
101 // 替回按钮
102 btn.parentNode.replaceChild(elClone, btn)
103
104 // 重新添加点击事件
105 elClone.addEventListener("click", (e) => {
106 // 实现复制
107 const target = e.target as HTMLElement
108 const parentPreBlock = target.closest(".cnblogs_code")
109 const codeBlock = parentPreBlock.querySelector<HTMLElement>("pre")
110
111 navigator.clipboard.writeText(codeBlock.innerText)
112 setHistory((prevData) =>
113 prevData
114 ? [
115 {
116 id: uuidv4(),
117 value: codeBlock.innerText,
118 createdAt: new Date(),
119 from: "博客园",
120 link: location.href,
121 tags: [],
122 remark: ""
123 },
124 ...prevData
125 ]
126 : [
127 {
128 id: uuidv4(),
129 value: codeBlock.innerText,
130 createdAt: new Date(),
131 from: "博客园",
132 link: location.href,

Callers 1

PlasmoOverlayFunction · 0.70

Calls 1

i18nFunction · 0.85

Tested by

no test coverage detected