()
| 114 | } |
| 115 | |
| 116 | function copyCodeFunc() { |
| 117 | copyCodeCssFunc() |
| 118 | // 内容区开启复制 |
| 119 | var content_views = document.querySelector("#content_views") |
| 120 | content_views?.replaceWith(content_views.cloneNode(true)) |
| 121 | |
| 122 | // 功能一: 修改复制按钮,支持一键复制 |
| 123 | const buttons = document.querySelectorAll<HTMLElement>(".hljs-button") |
| 124 | |
| 125 | buttons.forEach((btn) => { |
| 126 | btn.dataset.title = i18n("copyCode") |
| 127 | |
| 128 | // 移除点击事件 |
| 129 | btn.setAttribute("onclick", "") |
| 130 | |
| 131 | // 克隆按钮 |
| 132 | var elClone = btn.cloneNode(true) |
| 133 | |
| 134 | // 替回按钮 |
| 135 | btn.parentNode.replaceChild(elClone, btn) |
| 136 | |
| 137 | // 重新添加点击事件 |
| 138 | elClone.addEventListener("click", (e) => { |
| 139 | // 实现复制 |
| 140 | const target = e.target as HTMLElement |
| 141 | const parentPreBlock = target.closest("pre") |
| 142 | const codeBlock = parentPreBlock.querySelector("code") |
| 143 | |
| 144 | navigator.clipboard.writeText(codeBlock.innerText) |
| 145 | setHistory((prevData) => |
| 146 | prevData |
| 147 | ? [ |
| 148 | { |
| 149 | id: uuidv4(), |
| 150 | value: codeBlock.innerText, |
| 151 | createdAt: new Date(), |
| 152 | from: "CSDN", |
| 153 | link: location.href, |
| 154 | tags: [], |
| 155 | remark: "" |
| 156 | }, |
| 157 | ...prevData |
| 158 | ] |
| 159 | : [ |
| 160 | { |
| 161 | id: uuidv4(), |
| 162 | value: codeBlock.innerText, |
| 163 | createdAt: new Date(), |
| 164 | from: "CSDN", |
| 165 | link: location.href, |
| 166 | tags: [], |
| 167 | remark: "" |
| 168 | } |
| 169 | ] |
| 170 | ) |
| 171 | |
| 172 | target.dataset.title = i18n("copied") |
| 173 | setTimeout(() => { |
no test coverage detected