Skip to content

Commit

Permalink
πŸ› add missing js file
Browse files Browse the repository at this point in the history
  • Loading branch information
mickm3n committed Jul 28, 2024
1 parent 175cdf6 commit 88fa91f
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions static/js/copy-code-button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// This script adds copy buttons to all pre > code elements
document.addEventListener('DOMContentLoaded', (event) => {
document.querySelectorAll('pre > code').forEach((codeBlock) => {
const container = codeBlock.parentNode;
const copyButton = document.createElement('button');
copyButton.className = 'copy-code-button';
copyButton.type = 'button';
copyButton.innerText = 'Copy';

copyButton.addEventListener('click', () => {
navigator.clipboard.writeText(codeBlock.innerText).then(
() => {
copyButton.innerText = 'Copied!';
setTimeout(() => {
copyButton.innerText = 'Copy';
}, 2000);
},
(error) => {
console.error('Failed to copy code: ', error);
}
);
});

container.appendChild(copyButton);
});
});

0 comments on commit 88fa91f

Please sign in to comment.