31 lines
678 B
JavaScript
31 lines
678 B
JavaScript
|
let codes = document.querySelectorAll("pre")
|
||
|
|
||
|
codes.forEach(code => {
|
||
|
const elWrapper = document.createElement("div")
|
||
|
elWrapper.classList.add("copy-code-wrapper")
|
||
|
|
||
|
|
||
|
const el = document.createElement("span")
|
||
|
el.textContent = "Copy"
|
||
|
el.classList.add("copy-code")
|
||
|
|
||
|
elWrapper.appendChild(el)
|
||
|
|
||
|
code.parentNode.insertBefore(elWrapper, code)
|
||
|
|
||
|
let textContent = ""
|
||
|
code.childNodes.forEach(child => {
|
||
|
textContent += child.textContent;
|
||
|
})
|
||
|
|
||
|
elWrapper.addEventListener("click", (e) => {
|
||
|
e.preventDefault()
|
||
|
navigator.clipboard.writeText(textContent)
|
||
|
el.textContent = "Copied!"
|
||
|
setTimeout(() => {
|
||
|
el.textContent = "Copy"
|
||
|
}, 5000);
|
||
|
})
|
||
|
})
|
||
|
|