-
Notifications
You must be signed in to change notification settings - Fork 1
/
CopyClipboard.min.js
11 lines (11 loc) · 1.91 KB
/
CopyClipboard.min.js
1
2
3
4
5
6
7
8
9
10
11
/**
*
* @Name : CopyClipboard.js
* @Version : 1.0
* @Programmer : Max
* @Date : 2018-06-23
* @Released under : https://github.com/BaseMax/CopyClipboardJs/blob/master/LICENSE
* @Repository : https://github.com/BaseMax/CopyClipboardJs
*
**/
!function(n,a){"use strict";var o,i,c="Text!",r=!1,l=function(e){var t=a.createElement("div");t.textContent=e,a.body.appendChild(t),function(e){if(a.selection)(t=a.body.createTextRange()).moveToElementText(e),t.select();else if(n.getSelection){var t;(t=a.createRange()).selectNode(e),n.getSelection().removeAllRanges(),n.getSelection().addRange(t)}}(t),a.execCommand("copy"),t.remove()};n.addEventListener("paste",function(e){o=e.clipboardData.getData("text/plain"),r=!0,e.preventDefault()}),n.copy={copy_selected:function(){},copy_text:l,paste:function(e){var t=n.clipboardData?n.clipboardData.getData("Text"):((i=a.createElement("textarea")).setAttribute("style","width:0px;border:0;display:none;opacity:0;"),a.body.appendChild(i),i.select(),function e(){if(0!=r){var t=o;return r=!1,a.body.removeChild(i),i=null,t}setTimeout(e,250)}());void 0!==e.val?e.val=t:void 0!==e.innerHTML?e.innerHTML=t:void 0!==e.value&&(e.value=t)},text:function(e){var t=c;e.hasAttribute("data-copy-text")&&(t=e.getAttribute("data-copy-text")),l(t)},from:function(e){var t,n,o=void 0;e.hasAttribute("data-copy-from")&&(o=a.querySelector(e.getAttribute("data-copy-from"))),void 0===o?l("Text!"):(n=c,void 0!==(t=o).val?n=t.val:void 0!==t.innerHTML?n=t.innerHTML:void 0!==t.value&&(n=t.value),l(n))},send:function(e){var t=new XMLHttpRequest;t.onreadystatechange=function(){4==this.readyState&&this.status},t.open("GET",e,!0),t.send()}},n.addEventListener("load",function(){a.querySelectorAll("[data-copy-text]").forEach(function(e){null===e.onclick&&(e.onclick=function(){n.copy.text(this)})}),a.querySelectorAll("[data-copy-from]").forEach(function(e){null===e.onclick&&(e.onclick=function(){n.copy.from(this)})})},!1)}(window,document);