()
| 84 | } |
| 85 | |
| 86 | function copyCodeFunc() { |
| 87 | copyCodeCssFunc() |
| 88 | // 内容区开启复制 |
| 89 | var content_views = document.querySelector("#article") |
| 90 | content_views && content_views.replaceWith(content_views.cloneNode(true)) |
| 91 | |
| 92 | // 功能一: 修改复制按钮,支持一键复制 |
| 93 | const buttons = document.querySelectorAll<HTMLElement>(".codetool .copy") |
| 94 | |
| 95 | if (buttons.length > 0) { |
| 96 | buttons.forEach((btn) => { |
| 97 | // 更改标题 |
| 98 | btn.innerText = i18n("copy") |
| 99 | |
| 100 | // 移除点击事件 |
| 101 | btn.setAttribute("onclick", "") |
| 102 | |
| 103 | // 克隆按钮 |
| 104 | var elClone = btn.cloneNode(true) |
| 105 | |
| 106 | // 替回按钮 |
| 107 | btn.parentNode.replaceChild(elClone, btn) |
| 108 | |
| 109 | // 重新添加点击事件 |
| 110 | elClone.addEventListener("click", (e) => { |
| 111 | // 实现复制 |
| 112 | const target = e.target as HTMLElement |
| 113 | const parentPreBlock = target.closest(".jb51code") |
| 114 | const codeBlock = parentPreBlock.querySelector<HTMLElement>(".code") |
| 115 | |
| 116 | navigator.clipboard.writeText(codeBlock.innerText) |
| 117 | setHistory((prevData) => |
| 118 | prevData |
| 119 | ? [ |
| 120 | { |
| 121 | id: uuidv4(), |
| 122 | value: codeBlock.innerText, |
| 123 | createdAt: new Date(), |
| 124 | from: "脚本之家", |
| 125 | link: location.href, |
| 126 | tags: [], |
| 127 | remark: "" |
| 128 | }, |
| 129 | ...prevData |
| 130 | ] |
| 131 | : [ |
| 132 | { |
| 133 | id: uuidv4(), |
| 134 | value: codeBlock.innerText, |
| 135 | createdAt: new Date(), |
| 136 | from: "脚本之家", |
| 137 | link: location.href, |
| 138 | tags: [], |
| 139 | remark: "" |
| 140 | } |
| 141 | ] |
| 142 | ) |
| 143 |
no test coverage detected