-
Notifications
You must be signed in to change notification settings - Fork 458
Description
Like this highlighter a lot. I tried integrating a copy to clipboard button on each code snippet with highlighting. The copy function only works if I remove Rainbow from the page. Any advice is welcomed. Here is the script I wrote that adds a button and copies to the clipboard...
`const copyButtonLabel = "Copy";
// use a class selector if available
let blocks = document.querySelectorAll("pre.lang-html");
blocks.forEach((block) => {
// only add button if browser supports Clipboard API
if (navigator.clipboard) {
let button = document.createElement("button");
$(button).addClass( "btn btn-secondary2" );
button.innerText = copyButtonLabel;
block.appendChild(button);
button.addEventListener("click", async () => {
await copyCode(block, button);
});
}
});
async function copyCode(block, button) {
let code = block.querySelector("code");
let text = code.innerText;
await navigator.clipboard.writeText(text);
// visual feedback that task is completed
button.innerText = "Copied";
setTimeout(() => {
button.innerText = copyButtonLabel;
}, 700);
}`
<pre class="lang-html" tabindex="0"><code data-language="html"> <div class="container"> <div class="row"> <div class="col">Column 1</div> <div class="col">Column 2</div> <div class="col">Column 3</div> </div> </div></code></pre>