-
Notifications
You must be signed in to change notification settings - Fork 5
/
html-to-clipboard.min.js
1 lines (1 loc) · 2.14 KB
/
html-to-clipboard.min.js
1
function HtmlToClipboard(a){var b={limitTarget:null,listenOnCopy:!1,raw:!1},c=!1;if("object"==typeof a)for(option in a)b[option]=a[option];if("boolean"!=typeof b.raw)throw"'raw' option must be boolean";if(b.limitTarget&&"string"!=typeof b.limitTarget&&!(b.limitTarget instanceof HTMLElement))throw"'target' option must be either a selector or a DOM node";var d=function(a){var b=a;if("string"==typeof b){var c=document.querySelector(b);if(null===c)throw"Element with selector '"+b+"' does not exist in the DOM";return c}if(document.contains(b))return b;throw"Target element is not present in the DOM"},e=function(a){var b=window.getSelection();if(a){var c=d(a);if("undefined"!=typeof c.selectionStart)return c.value.substring(c.selectionStart,c.selectionEnd);for(var e=0;e<b.rangeCount;e++){var f=b.getRangeAt(e),g=0,h=null;if(f.intersectsNode(c)){var i=f.toString().length;f.setStart(c,0);var j=f.toString().length;return j>i&&(g=j-i),i=f.toString().length,f.setEnd(c,c.childNodes.length),j=f.toString().length,h=j>i?i:j,f.toString().substr(g,h)}}return""}return"undefined"!=typeof document.activeElement.selectionStart?document.activeElement.value.substring(document.activeElement.selectionStart,document.activeElement.selectionEnd):b.toString()},f=function(a,b){"boolean"!=typeof b&&(b=!0);var d=document.createElement(b?"textarea":"div");d.style.opacity=0,d.style.position="absolute",d.style.pointerEvents="none",d.style.zIndex=-1,d.setAttribute("tabindex","-1"),d.innerHTML=a,document.body.appendChild(d);var e=document.activeElement;if(d.focus(),b)d.select();else{window.getSelection().removeAllRanges();var f=document.createRange();f.setStartBefore(d.firstChild),f.setEndAfter(d.lastChild),window.getSelection().addRange(f)}var g=!1;try{c=!0,setTimeout(function(){c=!1}),g=document.execCommand("copy")}catch(h){console.error(h)}g||console.error("execCommand failed!"),window.getSelection().removeAllRanges(),document.body.removeChild(d),e.focus()};if(b.listenOnCopy){var g=document;b.limitTarget&&(g=d(b.limitTarget));var h=this;g.addEventListener("copy",function(a){c||(a.preventDefault(),h.copy())})}this.copy=function(a){if("string"!=typeof a)var a=e(b.limitTarget);""!==a&&f(a,b.raw)}}