fix: ensure code exists inside pre before adding clipboard
This commit is contained in:
parent
3eba87a512
commit
3b9ed2ab7b
@ -7,25 +7,27 @@ document.addEventListener("nav", () => {
|
|||||||
const els = document.getElementsByTagName("pre")
|
const els = document.getElementsByTagName("pre")
|
||||||
for (let i = 0; i < els.length; i++) {
|
for (let i = 0; i < els.length; i++) {
|
||||||
const codeBlock = els[i].getElementsByTagName("code")[0]
|
const codeBlock = els[i].getElementsByTagName("code")[0]
|
||||||
const source = codeBlock.innerText.replace(/\n\n/g, "\n")
|
if (codeBlock) {
|
||||||
const button = document.createElement("button")
|
const source = codeBlock.innerText.replace(/\n\n/g, "\n")
|
||||||
button.className = "clipboard-button"
|
const button = document.createElement("button")
|
||||||
button.type = "button"
|
button.className = "clipboard-button"
|
||||||
button.innerHTML = svgCopy
|
button.type = "button"
|
||||||
button.ariaLabel = "Copy source"
|
button.innerHTML = svgCopy
|
||||||
button.addEventListener("click", () => {
|
button.ariaLabel = "Copy source"
|
||||||
navigator.clipboard.writeText(source).then(
|
button.addEventListener("click", () => {
|
||||||
() => {
|
navigator.clipboard.writeText(source).then(
|
||||||
button.blur()
|
() => {
|
||||||
button.innerHTML = svgCheck
|
button.blur()
|
||||||
setTimeout(() => {
|
button.innerHTML = svgCheck
|
||||||
button.innerHTML = svgCopy
|
setTimeout(() => {
|
||||||
button.style.borderColor = ""
|
button.innerHTML = svgCopy
|
||||||
}, 2000)
|
button.style.borderColor = ""
|
||||||
},
|
}, 2000)
|
||||||
(error) => console.error(error),
|
},
|
||||||
)
|
(error) => console.error(error),
|
||||||
})
|
)
|
||||||
els[i].prepend(button)
|
})
|
||||||
|
els[i].prepend(button)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user