-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple-copy.min.js
2 lines (2 loc) · 2.48 KB
/
simple-copy.min.js
1
2
/* simple-copy.js 0.5.4 | Copyright (c) 2024 Guilherme Nascimento ([email protected]) | Released under the MIT license */
!function(e){var t="undefined"!=typeof window?window:{},n=t.document||{},r=t.jQuery,o=n.implementation.createHTMLDocument("").body;function i(e){return"true"!==e.hasAttribute("simple-copy-ignore")&&!e.offsetWidth&&!e.offsetHeight&&!e.getClientRects().length}function l(t,r,l,u,s){if("string"==typeof t)try{t=n.querySelector(t)}catch(p){return!1}if(!t||1!==t.nodeType)throw Error("Invalid element: <"+t+">");var f="object"==typeof t.form;if(l&&!f)return function t(n){for(var r=n.getElementsByTagName("*"),l=[],c=r.length-1;c>=0;c--){var u=r[c];i(u)&&(u.setAttribute("simple-copy-ignore","true"),l.push(u))}o.innerHTML=n.innerHTML;for(var c=l.length-1;c>=0;c--)l[c].removeAttribute("simple-copy-ignore");l=e;for(var s=o.querySelectorAll("script,noscript,object,link,img,[simple-copy-ignore=true]"),c=s.length-1;c>=0;c--){var u=s[c];u&&u.parentNode&&u.parentNode.removeChild(u)}return a(o.textContent)}(t);var d=t.ownerDocument,m=d.defaultView.getSelection(),y=d.createRange(),v=!r&&!f,g=t.isContentEditable;if(m.removeAllRanges(),v&&(t.contentEditable=!u||"inherit"),f&&!u){if(r)return c(t);if("SELECT"===t.nodeName&&s){for(var h,b=0,C=[],$=t.options,x=$.length;b<x;b++)$[b].selected&&C.push($[b].value);h=C.join(s)}else h=t.value;return a(h)}if(u?y.selectNode(t):y.selectNodeContents(t),m.addRange(y),!r){var T=d.execCommand("copy");return v&&(t.contentEditable=!!g||"inherit"),m.removeAllRanges(),t.blur(),T}}function c(e){e.focus(),e.select&&e.select()}function a(t){var r=n.scrollingElement||n.body,o=n.createElement("textarea"),i=r.scrollLeft,l=r.scrollTop;o.style.cssText="position:absolute;left:"+i+"px;top:"+l+"px;opacity:0",o.value=t,n.body.appendChild(o),c(o);var a=n.execCommand("copy");return n.body.removeChild(o),r.scrollLeft=i,r.scrollTop=l,r=o=e,a}function u(e,t,n){var r=e.getAttribute("data-simplecopy-"+t);return n?r:"true"===r}n.addEventListener("click",function e(t){if(0===t.button){var r,i,c=t.target,a=u(c,"data",!0);if(a)o.innerHTML=a,l(o,!1,!0);else{if(!(i=u(c,"target",!0)))return;(r=n.querySelector(i))&&l(r,u(c,"select"),u(c,"text"),u(c,"node"),u(c,"multiple",!0))}}});var s={select:function(e,t){return l(e,!0,!1,(t=t||{}).node)},copy:function(e,t){return l(e,!1,(t=t||{}).text,t.node,t.multiple)},data:a};r&&r.extend&&(r.fn.simpleCopy=function(t){var n=this[0];n&&(t||(t={}),t.type===e&&(action="copy"),("copy"===action||"select"===action)&&s[action](n,t))}),t.SimpleCopy=s}();