window.getSelection return html [duplicate]

The following will do this in all major browsers and is an exact duplicate of this answer: function getSelectionHtml() { var html = “”; if (typeof window.getSelection != “undefined”) { var sel = window.getSelection(); if (sel.rangeCount) { var container = document.createElement(“div”); for (var i = 0, len = sel.rangeCount; i < len; ++i) { container.appendChild(sel.getRangeAt(i).cloneContents()); … Read more

Cannot use `document.execCommand(‘copy’);` from developer console

document.execCommand(‘copy’) must be triggered by the user. It’s not only from the console, it’s anywhere that’s not inside an event triggered by the user. See below, the click event will return true, but a call without event won’t and a call in a dispatched event also. console.log(‘no event’, document.execCommand(‘bold’)); document.getElementById(‘test’).addEventListener(‘click’, function(){ console.log(‘user click’, document.execCommand(‘copy’)); }); … Read more