Never mind, I figured it out! I just needed to place the button in the surrounding element instead of the pre itself. Perhaps that's why you had .parentNode in the code, I didn’t get that. Thanks again!
In case it’s of any interest, what I did to make it move in my context, which is a Wordpress site where the html is generated by Emacs ORG Mode using Org2Blog, was to use the WPCode plugin to place this in the footer:
<script>
for (const src of document.getElementsByClassName("src")) {
const copy = document.createElement('span');
copy.classList.add("copy-button");
copy.addEventListener("click", () => {
navigator.clipboard.writeText(src.innerText);
copy.classList.add("copied-button");
setTimeout(() => { copy.classList.remove("copied-button") }, 200);
});
src.appendChild(copy);
}
</script>
And add this to “additional CSS” under Customize:
.org-src-container {
position: relative;
}
.copy-button {
position: absolute;
inset: .5em .5em auto auto;
width: 1em;
height: 1em;
border: 1px solid;
box-shadow: -.3em .3em #c0c0c0;
border-radius: .25em;
cursor: pointer;
display: none;
color: #000000;
}
.copied-button {
box-shadow: none;
background: #c0c0c0;
}
:is(:focus, :hover) > .copy-button {
display: inline-block;
}
I don’t get all the CSS magic I copied from you there, and “inset” is red in the CSS code display on the Wordpress web interface for some reason, but it works. :)