// Iterate over each element with the '.group' class document.querySelectorAll('.markdown').forEach(function(group) { // Create the copy button let copyBtn = document.createElement('button'); copyBtn.innerText = 'Copy'; // Add click event to the copy button copyBtn.addEventListener('click', function() { // Clone the group element to avoid modifying the original content let clonedGroup = group.cloneNode(true); // Remove all button elements from the cloned group clonedGroup.querySelectorAll('button').forEach(function(button) { button.remove(); }); // Copy the innerHTML of the cloned group (without buttons) to the clipboard let textArea = document.createElement('textarea'); textArea.value = clonedGroup.innerHTML; document.body.appendChild(textArea); textArea.select(); document.execCommand('copy'); document.body.removeChild(textArea); }); // Append the copy button to the '.group' class group.appendChild(copyBtn); });
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter