improvement:改进复制功能

This commit is contained in:
mason369 2023-04-13 00:15:01 +08:00
parent 9fa1ce533e
commit 3d9cf6aa07

View File

@ -1,5 +1,39 @@
// .vitepress/theme/index.js
import DefaultTheme from 'vitepress/theme'
import './style/custom.css'
import DefaultTheme from "vitepress/theme";
import "./style/custom.css";
export default DefaultTheme
function wait(timeout) {
return new Promise((resolve) => setTimeout(resolve, timeout));
}
async function init() {
try {
await wait(1000);
console.log("timeout");
const copyBtnList = document.getElementsByClassName("copy");
console.log(copyBtnList);
console.log(copyBtnList.length);
for (let i = 0; i < copyBtnList.length; i++) {
const btn = copyBtnList[i];
btn.addEventListener("click", () => {
const parentDiv = btn.parentNode;
const codeElem = parentDiv.querySelector("code");
const spans = [...codeElem.querySelectorAll("span")];
const textToCopy = [
...new Set(spans.map((span) => span.textContent)),
].join("");
navigator.clipboard.writeText(textToCopy);
console.log(textToCopy);
});
}
} catch (error) {
console.error(error);
}
}
init();
export default DefaultTheme;