Copy as HTML

PHOTO EMBED

Fri Sep 15 2023 06:15:59 GMT+0000 (Coordinated Universal Time)

Saved by @harshvasu #javascript

// 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);
});
content_copyCOPY