Copy to clipboard works fine in Windows PC on Google Chrome Browser, but it doesn't work with MacOS in Google Chrome Browser inside Wyswig editor of Email Marketing tools like Snovio.
Here is my code:
let htmlBlock = `<a style="padding: 10px 0px; max-width: 360px;" href="https://commons.wikimedia.org/wiki/Category:Images#/media/File:Volkswagen_Beetle_7.jpg" alt="Car Image"> <img height="168" width="300" src="https://commons.wikimedia.org/wiki/Category:Images#/media/File:Volkswagen_Beetle_7.jpg" alt="Car Image" /></a>`
copyToClipboard(htmlBlock);
function copyToClipboard(element) {
// Create container for the HTML
// [1]
let html = element
var container = document.createElement('div')
container.innerHTML = html
// Hide element
// [2]
container.style.position = 'fixed'
container.style.pointerEvents = 'none'
container.style.opacity = 0
// Detect all style sheets of the page
var activeSheets = Array.prototype.slice.call(document.styleSheets)
.filter(function (sheet) {
return !sheet.disabled
})
// Mount the container to the DOM to make `contentWindow` available
// [3]
document.body.appendChild(container)
// Copy to clipboard
// [4]
window.getSelection().removeAllRanges()
var range = document.createRange()
range.selectNode(container)
window.getSelection().addRange(range)
// [5.1]
document.execCommand('copy')
// [5.2]
for (var i = 0; i < activeSheets.length; i++) activeSheets[i].disabled = true
// [5.3]
document.execCommand('copy')
// [5.4]
for (var i = 0; i < activeSheets.length; i++) activeSheets[i].disabled = false
// Remove the container
// [6]
document.body.removeChild(container)
}
The same code works with Gmail editor in MacOS as well.
Any idea on what I could be missing or could it be very specific to Snovio platform?