clipboard-polyfill
Version:
A polyfill for the asynchronous clipboard API
8 lines • 14.8 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.clipboard=e():t.clipboard=e()}(this,function(){return function(t){function e(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,e),o.l=!0,o.exports}var n={};return e.m=t,e.c=n,e.d=function(t,n,r){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:r})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=2)}([function(t,e,n){"use strict";function r(t,e,n){m("listener called"),t.success=!0,e.forEach(function(e,r){n.clipboardData.setData(r,e),r===_&&n.clipboardData.getData(r)!=e&&(m("setting text/plain failed"),t.success=!1)}),n.preventDefault()}function o(t){var e=new T,n=r.bind(this,e,t);document.addEventListener("copy",n);try{document.execCommand("copy")}finally{document.removeEventListener("copy",n)}return e.success}function i(t,e){s(t);var n=o(e);return a(),n}function u(t){var e=document.createElement("div");e.setAttribute("style","-webkit-user-select: text !important"),e.textContent="temporary element",document.body.appendChild(e);var n=i(e,t);return document.body.removeChild(e),n}function c(t){m("copyTextUsingDOM");var e=document.createElement("div");e.setAttribute("style","-webkit-user-select: text !important");var n=e;e.attachShadow&&(m("Using shadow DOM."),n=e.attachShadow({mode:"open"}));var r=document.createElement("span");r.innerText=t,n.appendChild(r),document.body.appendChild(e),s(r);var o=document.execCommand("copy");return a(),document.body.removeChild(e),o}function s(t){var e=document.getSelection(),n=document.createRange();n.selectNodeContents(t),e.removeAllRanges(),e.addRange(n)}function a(){document.getSelection().removeAllRanges()}function l(t){var e=new y.DT;return e.setData(_,t),e}function f(){return"undefined"==typeof ClipboardEvent&&void 0!==window.clipboardData&&void 0!==window.clipboardData.setData}function d(t){var e=t.getData(_);if(void 0!==e)return window.clipboardData.setData("Text",e);throw"No `text/plain` value was specified."}function p(){return h(this,void 0,void 0,function(){var t;return v(this,function(e){if(""===(t=window.clipboardData.getData("Text")))throw"Empty clipboard or could not read plain text from clipboard";return[2,t]})})}var h=this&&this.__awaiter||function(t,e,n,r){return new(n||(n=Promise))(function(o,i){function u(t){try{s(r.next(t))}catch(t){i(t)}}function c(t){try{s(r.throw(t))}catch(t){i(t)}}function s(t){t.done?o(t.value):new n(function(e){e(t.value)}).then(u,c)}s((r=r.apply(t,e||[])).next())})},v=this&&this.__generator||function(t,e){function n(t){return function(e){return r([t,e])}}function r(n){if(o)throw new TypeError("Generator is already executing.");for(;s;)try{if(o=1,i&&(u=i[2&n[0]?"return":n[0]?"throw":"next"])&&!(u=u.call(i,n[1])).done)return u;switch(i=0,u&&(n=[0,u.value]),n[0]){case 0:case 1:u=n;break;case 4:return s.label++,{value:n[1],done:!1};case 5:s.label++,i=n[1],n=[0];continue;case 7:n=s.ops.pop(),s.trys.pop();continue;default:if(u=s.trys,!(u=u.length>0&&u[u.length-1])&&(6===n[0]||2===n[0])){s=0;continue}if(3===n[0]&&(!u||n[1]>u[0]&&n[1]<u[3])){s.label=n[1];break}if(6===n[0]&&s.label<u[1]){s.label=u[1],u=n;break}if(u&&s.label<u[2]){s.label=u[2],s.ops.push(n);break}u[2]&&s.ops.pop(),s.trys.pop();continue}n=e.call(t,s)}catch(t){n=[6,t],i=0}finally{o=u=0}if(5&n[0])throw n[1];return{value:n[0]?n[1]:void 0,done:!0}}var o,i,u,c,s={label:0,sent:function(){if(1&u[0])throw u[1];return u[1]},trys:[],ops:[]};return c={next:n(0),throw:n(1),return:n(2)},"function"==typeof Symbol&&(c[Symbol.iterator]=function(){return this}),c};Object.defineProperty(e,"__esModule",{value:!0});var y=n(1),m=function(t){},b=!0,w=function(){(console.warn||console.log).apply(console,arguments)},g=w.bind("[clipboard-polyfill]"),_="text/plain",x=function(){function t(){}return t.setDebugLog=function(t){m=t},t.suppressWarnings=function(){b=!1,y.suppressDTWarnings()},t.write=function(t){return h(this,void 0,void 0,function(){var e;return v(this,function(n){if(b&&!t.getData(_)&&g("clipboard.write() was called without a `text/plain` data type. On some platforms, this may result in an empty clipboard. Call clipboard.suppressWarnings() to suppress this warning."),f()){if(d(t))return[2];throw"Copying failed, possibly because the user rejected it."}if(o(t))return m("regular execCopy worked"),[2];if(navigator.userAgent.indexOf("Edge")>-1)return m('UA "Edge" => assuming success'),[2];if(i(document.body,t))return m("copyUsingTempSelection worked"),[2];if(u(t))return m("copyUsingTempElem worked"),[2];if(void 0!==(e=t.getData(_))&&c(e))return m("copyTextUsingDOM worked"),[2];throw"Copy command failed."})})},t.writeText=function(t){return h(this,void 0,void 0,function(){return v(this,function(e){return navigator.clipboard&&navigator.clipboard.writeText?(m("Using `navigator.clipboard.writeText()`."),[2,navigator.clipboard.writeText(t)]):[2,this.write(l(t))]})})},t.read=function(){return h(this,void 0,void 0,function(){var t;return v(this,function(e){switch(e.label){case 0:return t=l,[4,this.readText()];case 1:return[2,t.apply(void 0,[e.sent()])]}})})},t.readText=function(){return h(this,void 0,void 0,function(){return v(this,function(t){if(navigator.clipboard&&navigator.clipboard.readText)return m("Using `navigator.clipboard.readText()`."),[2,navigator.clipboard.readText()];if(f())return m("Reading text using IE strategy."),[2,p()];throw"Read is not supported in your browser."})})},t.DT=y.DT,t}();e.default=x;var T=function(){function t(){this.success=!1}return t}();t.exports=x},function(t,e,n){"use strict";function r(){c=!1}Object.defineProperty(e,"__esModule",{value:!0});var o=["text/plain","text/html"],i=function(){(console.warn||console.log).call(arguments)},u=i.bind(console,"[clipboard-polyfill]"),c=!0;e.suppressDTWarnings=r;var s=function(){function t(){this.m={}}return t.prototype.setData=function(t,e){c&&-1===o.indexOf(t)&&u("Unknown data type: "+t,"Call clipboard.suppressWarnings() to suppress this warning."),this.m[t]=e},t.prototype.getData=function(t){return this.m[t]},t.prototype.forEach=function(t){for(var e in this.m)t(this.m[e],e)},t}();e.DT=s},function(t,e,n){n(3),t.exports=n(0)},function(t,e,n){(function(e,r){/*!
* @overview es6-promise - a tiny implementation of Promises/A+.
* @copyright Copyright (c) 2014 Yehuda Katz, Tom Dale, Stefan Penner and contributors (Conversion to ES6 API by Jake Archibald)
* @license Licensed under MIT license
* See https://raw.githubusercontent.com/stefanpenner/es6-promise/master/LICENSE
* @version 4.1.1
*/
!function(e,n){t.exports=n()}(0,function(){"use strict";function t(t){var e=typeof t;return null!==t&&("object"===e||"function"===e)}function o(t){return"function"==typeof t}function i(t){z=t}function u(t){B=t}function c(){return void 0!==q?function(){q(a)}:s()}function s(){var t=setTimeout;return function(){return t(a,1)}}function a(){for(var t=0;t<K;t+=2){(0,Z[t])(Z[t+1]),Z[t]=void 0,Z[t+1]=void 0}K=0}function l(t,e){var n=arguments,r=this,o=new this.constructor(d);void 0===o[tt]&&k(o);var i=r._state;return i?function(){var t=n[i-1];B(function(){return j(i,o,t,r._result)})}():D(r,o,t,e),o}function f(t){var e=this;if(t&&"object"==typeof t&&t.constructor===e)return t;var n=new e(d);return g(n,t),n}function d(){}function p(){return new TypeError("You cannot resolve a promise with itself")}function h(){return new TypeError("A promises callback cannot return that same promise.")}function v(t){try{return t.then}catch(t){return ot.error=t,ot}}function y(t,e,n,r){try{t.call(e,n,r)}catch(t){return t}}function m(t,e,n){B(function(t){var r=!1,o=y(n,e,function(n){r||(r=!0,e!==n?g(t,n):x(t,n))},function(e){r||(r=!0,T(t,e))},"Settle: "+(t._label||" unknown promise"));!r&&o&&(r=!0,T(t,o))},t)}function b(t,e){e._state===nt?x(t,e._result):e._state===rt?T(t,e._result):D(e,void 0,function(e){return g(t,e)},function(e){return T(t,e)})}function w(t,e,n){e.constructor===t.constructor&&n===l&&e.constructor.resolve===f?b(t,e):n===ot?(T(t,ot.error),ot.error=null):void 0===n?x(t,e):o(n)?m(t,e,n):x(t,e)}function g(e,n){e===n?T(e,p()):t(n)?w(e,n,v(n)):x(e,n)}function _(t){t._onerror&&t._onerror(t._result),A(t)}function x(t,e){t._state===et&&(t._result=e,t._state=nt,0!==t._subscribers.length&&B(A,t))}function T(t,e){t._state===et&&(t._state=rt,t._result=e,B(_,t))}function D(t,e,n,r){var o=t._subscribers,i=o.length;t._onerror=null,o[i]=e,o[i+nt]=n,o[i+rt]=r,0===i&&t._state&&B(A,t)}function A(t){var e=t._subscribers,n=t._state;if(0!==e.length){for(var r=void 0,o=void 0,i=t._result,u=0;u<e.length;u+=3)r=e[u],o=e[u+n],r?j(n,r,o,i):o(i);t._subscribers.length=0}}function E(){this.error=null}function C(t,e){try{return t(e)}catch(t){return it.error=t,it}}function j(t,e,n,r){var i=o(n),u=void 0,c=void 0,s=void 0,a=void 0;if(i){if(u=C(n,r),u===it?(a=!0,c=u.error,u.error=null):s=!0,e===u)return void T(e,h())}else u=r,s=!0;e._state!==et||(i&&s?g(e,u):a?T(e,c):t===nt?x(e,u):t===rt&&T(e,u))}function O(t,e){try{e(function(e){g(t,e)},function(e){T(t,e)})}catch(e){T(t,e)}}function S(){return ut++}function k(t){t[tt]=ut++,t._state=void 0,t._result=void 0,t._subscribers=[]}function M(t,e){this._instanceConstructor=t,this.promise=new t(d),this.promise[tt]||k(this.promise),I(e)?(this.length=e.length,this._remaining=e.length,this._result=new Array(this.length),0===this.length?x(this.promise,this._result):(this.length=this.length||0,this._enumerate(e),0===this._remaining&&x(this.promise,this._result))):T(this.promise,P())}function P(){return new Error("Array Methods must be provided an Array")}function U(t){return new M(this,t).promise}function L(t){var e=this;return new e(I(t)?function(n,r){for(var o=t.length,i=0;i<o;i++)e.resolve(t[i]).then(n,r)}:function(t,e){return e(new TypeError("You must pass an array to race."))})}function R(t){var e=this,n=new e(d);return T(n,t),n}function W(){throw new TypeError("You must pass a resolver function as the first argument to the promise constructor")}function F(){throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.")}function N(t){this[tt]=S(),this._result=this._state=void 0,this._subscribers=[],d!==t&&("function"!=typeof t&&W(),this instanceof N?O(this,t):F())}function Y(){var t=void 0;if(void 0!==r)t=r;else if("undefined"!=typeof self)t=self;else try{t=Function("return this")()}catch(t){throw new Error("polyfill failed because global object is unavailable in this environment")}var e=t.Promise;if(e){var n=null;try{n=Object.prototype.toString.call(e.resolve())}catch(t){}if("[object Promise]"===n&&!e.cast)return}t.Promise=N}var G=void 0;G=Array.isArray?Array.isArray:function(t){return"[object Array]"===Object.prototype.toString.call(t)};var I=G,K=0,q=void 0,z=void 0,B=function(t,e){Z[K]=t,Z[K+1]=e,2===(K+=2)&&(z?z(a):$())},H="undefined"!=typeof window?window:void 0,J=H||{},Q=J.MutationObserver||J.WebKitMutationObserver,V="undefined"==typeof self&&void 0!==e&&"[object process]"==={}.toString.call(e),X="undefined"!=typeof Uint8ClampedArray&&"undefined"!=typeof importScripts&&"undefined"!=typeof MessageChannel,Z=new Array(1e3),$=void 0;$=V?function(){return function(){return e.nextTick(a)}}():Q?function(){var t=0,e=new Q(a),n=document.createTextNode("");return e.observe(n,{characterData:!0}),function(){n.data=t=++t%2}}():X?function(){var t=new MessageChannel;return t.port1.onmessage=a,function(){return t.port2.postMessage(0)}}():void 0===H?function(){try{var t=n(6);return q=t.runOnLoop||t.runOnContext,c()}catch(t){return s()}}():s();var tt=Math.random().toString(36).substring(16),et=void 0,nt=1,rt=2,ot=new E,it=new E,ut=0;return M.prototype._enumerate=function(t){for(var e=0;this._state===et&&e<t.length;e++)this._eachEntry(t[e],e)},M.prototype._eachEntry=function(t,e){var n=this._instanceConstructor,r=n.resolve;if(r===f){var o=v(t);if(o===l&&t._state!==et)this._settledAt(t._state,e,t._result);else if("function"!=typeof o)this._remaining--,this._result[e]=t;else if(n===N){var i=new n(d);w(i,t,o),this._willSettleAt(i,e)}else this._willSettleAt(new n(function(e){return e(t)}),e)}else this._willSettleAt(r(t),e)},M.prototype._settledAt=function(t,e,n){var r=this.promise;r._state===et&&(this._remaining--,t===rt?T(r,n):this._result[e]=n),0===this._remaining&&x(r,this._result)},M.prototype._willSettleAt=function(t,e){var n=this;D(t,void 0,function(t){return n._settledAt(nt,e,t)},function(t){return n._settledAt(rt,e,t)})},N.all=U,N.race=L,N.resolve=f,N.reject=R,N._setScheduler=i,N._setAsap=u,N._asap=B,N.prototype={constructor:N,then:l,catch:function(t){return this.then(null,t)}},N.polyfill=Y,N.Promise=N,N.polyfill(),N})}).call(e,n(4),n(5))},function(t,e){function n(){throw new Error("setTimeout has not been defined")}function r(){throw new Error("clearTimeout has not been defined")}function o(t){if(l===setTimeout)return setTimeout(t,0);if((l===n||!l)&&setTimeout)return l=setTimeout,setTimeout(t,0);try{return l(t,0)}catch(e){try{return l.call(null,t,0)}catch(e){return l.call(this,t,0)}}}function i(t){if(f===clearTimeout)return clearTimeout(t);if((f===r||!f)&&clearTimeout)return f=clearTimeout,clearTimeout(t);try{return f(t)}catch(e){try{return f.call(null,t)}catch(e){return f.call(this,t)}}}function u(){v&&p&&(v=!1,p.length?h=p.concat(h):y=-1,h.length&&c())}function c(){if(!v){var t=o(u);v=!0;for(var e=h.length;e;){for(p=h,h=[];++y<e;)p&&p[y].run();y=-1,e=h.length}p=null,v=!1,i(t)}}function s(t,e){this.fun=t,this.array=e}function a(){}var l,f,d=t.exports={};!function(){try{l="function"==typeof setTimeout?setTimeout:n}catch(t){l=n}try{f="function"==typeof clearTimeout?clearTimeout:r}catch(t){f=r}}();var p,h=[],v=!1,y=-1;d.nextTick=function(t){var e=new Array(arguments.length-1);if(arguments.length>1)for(var n=1;n<arguments.length;n++)e[n-1]=arguments[n];h.push(new s(t,e)),1!==h.length||v||o(c)},s.prototype.run=function(){this.fun.apply(null,this.array)},d.title="browser",d.browser=!0,d.env={},d.argv=[],d.version="",d.versions={},d.on=a,d.addListener=a,d.once=a,d.off=a,d.removeListener=a,d.removeAllListeners=a,d.emit=a,d.prependListener=a,d.prependOnceListener=a,d.listeners=function(t){return[]},d.binding=function(t){throw new Error("process.binding is not supported")},d.cwd=function(){return"/"},d.chdir=function(t){throw new Error("process.chdir is not supported")},d.umask=function(){return 0}},function(t,e){var n;n=function(){return this}();try{n=n||Function("return this")()||(0,eval)("this")}catch(t){"object"==typeof window&&(n=window)}t.exports=n},function(t,e){}])});