Button for Copying Code
A code that can be inserted into a post so as to allow visitors to copy the code that is posted is here:
It can be pasted in the post in full (using HTML mode, of course), or the script can be inserted in the theme HTML with only the button pasted in the post (as probably not all posts in the weblog would have code to copy). It will copy all text between <code></code> tags (only the first set occurring in the page).
<button onclick="copyCodeSnippet()">Copy Code</button>
<script>
function copyCodeSnippet() {
const text = document.getElementsByTagName('code')[0].innerText;
const tempTextArea = document.createElement('textarea');
tempTextArea.value = text;
document.body.appendChild(tempTextArea);
tempTextArea.select();
document.execCommand('copy');
document.body.removeChild(tempTextArea);
// alert('Code copied to clipboard!');
}
</script>
Comments
Post a Comment