兼容app与H5

#H5复制脚本,clipboard.js

  1. // #ifdef H5
  2. /*!
  3. * clipboard.js v2.0.4
  4. * https://zenorocha.github.io/clipboard.js
  5. *
  6. * Licensed MIT © Zeno Rocha
  7. */
  8. ! function(t, e) {
  9. "object" == typeof exports && "object" == typeof module ? module.exports = e() : "function" == typeof define && define
  10. .amd ? define([], e) : "object" == typeof exports ? exports.ClipboardJS = e() : t.ClipboardJS = e()
  11. }(this, function() {
  12. return function(n) {
  13. var o = {};
  14. function r(t) {
  15. if (o[t]) return o[t].exports;
  16. var e = o[t] = {
  17. i: t,
  18. l: !1,
  19. exports: {}
  20. };
  21. return n[t].call(e.exports, e, e.exports, r), e.l = !0, e.exports
  22. }
  23. return r.m = n, r.c = o, r.d = function(t, e, n) {
  24. r.o(t, e) || Object.defineProperty(t, e, {
  25. enumerable: !0,
  26. get: n
  27. })
  28. }, r.r = function(t) {
  29. "undefined" != typeof Symbol && Symbol.toStringTag && Object.defineProperty(t, Symbol.toStringTag, {
  30. value: "Module"
  31. }), Object.defineProperty(t, "__esModule", {
  32. value: !0
  33. })
  34. }, r.t = function(e, t) {
  35. if (1 & t && (e = r(e)), 8 & t) return e;
  36. if (4 & t && "object" == typeof e && e && e.__esModule) return e;
  37. var n = Object.create(null);
  38. if (r.r(n), Object.defineProperty(n, "default", {
  39. enumerable: !0,
  40. value: e
  41. }), 2 & t && "string" != typeof e)
  42. for (var o in e) r.d(n, o, function(t) {
  43. return e[t]
  44. }.bind(null, o));
  45. return n
  46. }, r.n = function(t) {
  47. var e = t && t.__esModule ? function() {
  48. return t.default
  49. } : function() {
  50. return t
  51. };
  52. return r.d(e, "a", e), e
  53. }, r.o = function(t, e) {
  54. return Object.prototype.hasOwnProperty.call(t, e)
  55. }, r.p = "", r(r.s = 0)
  56. }([function(t, e, n) {
  57. "use strict";
  58. var r = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(t) {
  59. return typeof t
  60. } : function(t) {
  61. return t && "function" == typeof Symbol && t.constructor === Symbol && t !== Symbol.prototype ? "symbol" :
  62. typeof t
  63. },
  64. i = function() {
  65. function o(t, e) {
  66. for (var n = 0; n < e.length; n++) {
  67. var o = e[n];
  68. o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(
  69. t, o.key, o)
  70. }
  71. }
  72. return function(t, e, n) {
  73. return e && o(t.prototype, e), n && o(t, n), t
  74. }
  75. }(),
  76. a = o(n(1)),
  77. c = o(n(3)),
  78. u = o(n(4));
  79. function o(t) {
  80. return t && t.__esModule ? t : {
  81. default: t
  82. }
  83. }
  84. var l = function(t) {
  85. function o(t, e) {
  86. ! function(t, e) {
  87. if (!(t instanceof e)) throw new TypeError("Cannot call a class as a function")
  88. }(this, o);
  89. var n = function(t, e) {
  90. if (!t) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  91. return !e || "object" != typeof e && "function" != typeof e ? t : e
  92. }(this, (o.__proto__ || Object.getPrototypeOf(o)).call(this));
  93. return n.resolveOptions(e), n.listenClick(t), n
  94. }
  95. return function(t, e) {
  96. if ("function" != typeof e && null !== e) throw new TypeError(
  97. "Super expression must either be null or a function, not " + typeof e);
  98. t.prototype = Object.create(e && e.prototype, {
  99. constructor: {
  100. value: t,
  101. enumerable: !1,
  102. writable: !0,
  103. configurable: !0
  104. }
  105. }), e && (Object.setPrototypeOf ? Object.setPrototypeOf(t, e) : t.__proto__ = e)
  106. }(o, c.default), i(o, [{
  107. key: "resolveOptions",
  108. value: function() {
  109. var t = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : {};
  110. this.action = "function" == typeof t.action ? t.action : this.defaultAction, this.target = "function" ==
  111. typeof t.target ? t.target : this.defaultTarget, this.text = "function" == typeof t.text ? t.text : this
  112. .defaultText, this.container = "object" === r(t.container) ? t.container : document.body
  113. }
  114. }, {
  115. key: "listenClick",
  116. value: function(t) {
  117. var e = this;
  118. this.listener = (0, u.default)(t, "click", function(t) {
  119. return e.onClick(t)
  120. })
  121. }
  122. }, {
  123. key: "onClick",
  124. value: function(t) {
  125. var e = t.delegateTarget || t.currentTarget;
  126. this.clipboardAction && (this.clipboardAction = null), this.clipboardAction = new a.default({
  127. action: this.action(e),
  128. target: this.target(e),
  129. text: this.text(e),
  130. container: this.container,
  131. trigger: e,
  132. emitter: this
  133. })
  134. }
  135. }, {
  136. key: "defaultAction",
  137. value: function(t) {
  138. return s("action", t)
  139. }
  140. }, {
  141. key: "defaultTarget",
  142. value: function(t) {
  143. var e = s("target", t);
  144. if (e) return document.querySelector(e)
  145. }
  146. }, {
  147. key: "defaultText",
  148. value: function(t) {
  149. return s("text", t)
  150. }
  151. }, {
  152. key: "destroy",
  153. value: function() {
  154. this.listener.destroy(), this.clipboardAction && (this.clipboardAction.destroy(), this.clipboardAction =
  155. null)
  156. }
  157. }], [{
  158. key: "isSupported",
  159. value: function() {
  160. var t = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : ["copy", "cut"],
  161. e = "string" == typeof t ? [t] : t,
  162. n = !!document.queryCommandSupported;
  163. return e.forEach(function(t) {
  164. n = n && !!document.queryCommandSupported(t)
  165. }), n
  166. }
  167. }]), o
  168. }();
  169. function s(t, e) {
  170. var n = "data-clipboard-" + t;
  171. if (e.hasAttribute(n)) return e.getAttribute(n)
  172. }
  173. t.exports = l
  174. }, function(t, e, n) {
  175. "use strict";
  176. var o, r = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(t) {
  177. return typeof t
  178. } : function(t) {
  179. return t && "function" == typeof Symbol && t.constructor === Symbol && t !== Symbol.prototype ? "symbol" :
  180. typeof t
  181. },
  182. i = function() {
  183. function o(t, e) {
  184. for (var n = 0; n < e.length; n++) {
  185. var o = e[n];
  186. o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(
  187. t, o.key, o)
  188. }
  189. }
  190. return function(t, e, n) {
  191. return e && o(t.prototype, e), n && o(t, n), t
  192. }
  193. }(),
  194. a = n(2),
  195. c = (o = a) && o.__esModule ? o : {
  196. default: o
  197. };
  198. var u = function() {
  199. function e(t) {
  200. ! function(t, e) {
  201. if (!(t instanceof e)) throw new TypeError("Cannot call a class as a function")
  202. }(this, e), this.resolveOptions(t), this.initSelection()
  203. }
  204. return i(e, [{
  205. key: "resolveOptions",
  206. value: function() {
  207. var t = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : {};
  208. this.action = t.action, this.container = t.container, this.emitter = t.emitter, this.target = t.target,
  209. this.text = t.text, this.trigger = t.trigger, this.selectedText = ""
  210. }
  211. }, {
  212. key: "initSelection",
  213. value: function() {
  214. this.text ? this.selectFake() : this.target && this.selectTarget()
  215. }
  216. }, {
  217. key: "selectFake",
  218. value: function() {
  219. var t = this,
  220. e = "rtl" == document.documentElement.getAttribute("dir");
  221. this.removeFake(), this.fakeHandlerCallback = function() {
  222. return t.removeFake()
  223. }, this.fakeHandler = this.container.addEventListener("click", this.fakeHandlerCallback) || !0, this.fakeElem =
  224. document.createElement("textarea"), this.fakeElem.style.fontSize = "12pt", this.fakeElem.style.border =
  225. "0", this.fakeElem.style.padding = "0", this.fakeElem.style.margin = "0", this.fakeElem.style.position =
  226. "absolute", this.fakeElem.style[e ? "right" : "left"] = "-9999px";
  227. var n = window.pageYOffset || document.documentElement.scrollTop;
  228. this.fakeElem.style.top = n + "px", this.fakeElem.setAttribute("readonly", ""), this.fakeElem.value =
  229. this.text, this.container.appendChild(this.fakeElem), this.selectedText = (0, c.default)(this.fakeElem),
  230. this.copyText()
  231. }
  232. }, {
  233. key: "removeFake",
  234. value: function() {
  235. this.fakeHandler && (this.container.removeEventListener("click", this.fakeHandlerCallback), this.fakeHandler =
  236. null, this.fakeHandlerCallback = null), this.fakeElem && (this.container.removeChild(this.fakeElem),
  237. this.fakeElem = null)
  238. }
  239. }, {
  240. key: "selectTarget",
  241. value: function() {
  242. this.selectedText = (0, c.default)(this.target), this.copyText()
  243. }
  244. }, {
  245. key: "copyText",
  246. value: function() {
  247. var e = void 0;
  248. try {
  249. e = document.execCommand(this.action)
  250. } catch (t) {
  251. e = !1
  252. }
  253. this.handleResult(e)
  254. }
  255. }, {
  256. key: "handleResult",
  257. value: function(t) {
  258. this.emitter.emit(t ? "success" : "error", {
  259. action: this.action,
  260. text: this.selectedText,
  261. trigger: this.trigger,
  262. clearSelection: this.clearSelection.bind(this)
  263. })
  264. }
  265. }, {
  266. key: "clearSelection",
  267. value: function() {
  268. this.trigger && this.trigger.focus(), window.getSelection().removeAllRanges()
  269. }
  270. }, {
  271. key: "destroy",
  272. value: function() {
  273. this.removeFake()
  274. }
  275. }, {
  276. key: "action",
  277. set: function() {
  278. var t = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : "copy";
  279. if (this._action = t, "copy" !== this._action && "cut" !== this._action) throw new Error(
  280. 'Invalid "action" value, use either "copy" or "cut"')
  281. },
  282. get: function() {
  283. return this._action
  284. }
  285. }, {
  286. key: "target",
  287. set: function(t) {
  288. if (void 0 !== t) {
  289. if (!t || "object" !== (void 0 === t ? "undefined" : r(t)) || 1 !== t.nodeType) throw new Error(
  290. 'Invalid "target" value, use a valid Element');
  291. if ("copy" === this.action && t.hasAttribute("disabled")) throw new Error(
  292. 'Invalid "target" attribute. Please use "readonly" instead of "disabled" attribute');
  293. if ("cut" === this.action && (t.hasAttribute("readonly") || t.hasAttribute("disabled"))) throw new Error(
  294. 'Invalid "target" attribute. You can\'t cut text from elements with "readonly" or "disabled" attributes'
  295. );
  296. this._target = t
  297. }
  298. },
  299. get: function() {
  300. return this._target
  301. }
  302. }]), e
  303. }();
  304. t.exports = u
  305. }, function(t, e) {
  306. t.exports = function(t) {
  307. var e;
  308. if ("SELECT" === t.nodeName) t.focus(), e = t.value;
  309. else if ("INPUT" === t.nodeName || "TEXTAREA" === t.nodeName) {
  310. var n = t.hasAttribute("readonly");
  311. n || t.setAttribute("readonly", ""), t.select(), t.setSelectionRange(0, t.value.length), n || t.removeAttribute(
  312. "readonly"), e = t.value
  313. } else {
  314. t.hasAttribute("contenteditable") && t.focus();
  315. var o = window.getSelection(),
  316. r = document.createRange();
  317. r.selectNodeContents(t), o.removeAllRanges(), o.addRange(r), e = o.toString()
  318. }
  319. return e
  320. }
  321. }, function(t, e) {
  322. function n() {}
  323. n.prototype = {
  324. on: function(t, e, n) {
  325. var o = this.e || (this.e = {});
  326. return (o[t] || (o[t] = [])).push({
  327. fn: e,
  328. ctx: n
  329. }), this
  330. },
  331. once: function(t, e, n) {
  332. var o = this;
  333. function r() {
  334. o.off(t, r), e.apply(n, arguments)
  335. }
  336. return r._ = e, this.on(t, r, n)
  337. },
  338. emit: function(t) {
  339. for (var e = [].slice.call(arguments, 1), n = ((this.e || (this.e = {}))[t] || []).slice(), o = 0, r = n.length; o <
  340. r; o++) n[o].fn.apply(n[o].ctx, e);
  341. return this
  342. },
  343. off: function(t, e) {
  344. var n = this.e || (this.e = {}),
  345. o = n[t],
  346. r = [];
  347. if (o && e)
  348. for (var i = 0, a = o.length; i < a; i++) o[i].fn !== e && o[i].fn._ !== e && r.push(o[i]);
  349. return r.length ? n[t] = r : delete n[t], this
  350. }
  351. }, t.exports = n
  352. }, function(t, e, n) {
  353. var d = n(5),
  354. h = n(6);
  355. t.exports = function(t, e, n) {
  356. if (!t && !e && !n) throw new Error("Missing required arguments");
  357. if (!d.string(e)) throw new TypeError("Second argument must be a String");
  358. if (!d.fn(n)) throw new TypeError("Third argument must be a Function");
  359. if (d.node(t)) return s = e, f = n, (l = t).addEventListener(s, f), {
  360. destroy: function() {
  361. l.removeEventListener(s, f)
  362. }
  363. };
  364. if (d.nodeList(t)) return a = t, c = e, u = n, Array.prototype.forEach.call(a, function(t) {
  365. t.addEventListener(c, u)
  366. }), {
  367. destroy: function() {
  368. Array.prototype.forEach.call(a, function(t) {
  369. t.removeEventListener(c, u)
  370. })
  371. }
  372. };
  373. if (d.string(t)) return o = t, r = e, i = n, h(document.body, o, r, i);
  374. throw new TypeError("First argument must be a String, HTMLElement, HTMLCollection, or NodeList");
  375. var o, r, i, a, c, u, l, s, f
  376. }
  377. }, function(t, n) {
  378. n.node = function(t) {
  379. return void 0 !== t && t instanceof HTMLElement && 1 === t.nodeType
  380. }, n.nodeList = function(t) {
  381. var e = Object.prototype.toString.call(t);
  382. return void 0 !== t && ("[object NodeList]" === e || "[object HTMLCollection]" === e) && "length" in t && (0 ===
  383. t.length || n.node(t[0]))
  384. }, n.string = function(t) {
  385. return "string" == typeof t || t instanceof String
  386. }, n.fn = function(t) {
  387. return "[object Function]" === Object.prototype.toString.call(t)
  388. }
  389. }, function(t, e, n) {
  390. var a = n(7);
  391. function i(t, e, n, o, r) {
  392. var i = function(e, n, t, o) {
  393. return function(t) {
  394. t.delegateTarget = a(t.target, n), t.delegateTarget && o.call(e, t)
  395. }
  396. }.apply(this, arguments);
  397. return t.addEventListener(n, i, r), {
  398. destroy: function() {
  399. t.removeEventListener(n, i, r)
  400. }
  401. }
  402. }
  403. t.exports = function(t, e, n, o, r) {
  404. return "function" == typeof t.addEventListener ? i.apply(null, arguments) : "function" == typeof n ? i.bind(
  405. null, document).apply(null, arguments) : ("string" == typeof t && (t = document.querySelectorAll(t)), Array.prototype
  406. .map.call(t, function(t) {
  407. return i(t, e, n, o, r)
  408. }))
  409. }
  410. }, function(t, e) {
  411. if ("undefined" != typeof Element && !Element.prototype.matches) {
  412. var n = Element.prototype;
  413. n.matches = n.matchesSelector || n.mozMatchesSelector || n.msMatchesSelector || n.oMatchesSelector || n.webkitMatchesSelector
  414. }
  415. t.exports = function(t, e) {
  416. for (; t && 9 !== t.nodeType;) {
  417. if ("function" == typeof t.matches && t.matches(e)) return t;
  418. t = t.parentNode
  419. }
  420. }
  421. }])
  422. });
  423. // #endif

#结合uniapp提供的api进一步封装,clipboard-al.js

  1. /**
  2. * 复制文本 兼容H5
  3. **/
  4. // #ifdef H5
  5. import ClipboardJS from "@/components/utils/clipboard.js"
  6. // #endif
  7. const al = {
  8. getClipboardData: function(data, callback) {
  9. // #ifdef APP-PLUS || MP
  10. uni.setClipboardData({
  11. data: data,
  12. success(res) {
  13. uni.getClipboardData({
  14. success(res) {
  15. ("function" == typeof callback) && callback(true)
  16. },
  17. fail(res) {
  18. ("function" == typeof callback) && callback(false)
  19. }
  20. })
  21. },
  22. fail(res) {
  23. ("function" == typeof callback) && callback(false)
  24. }
  25. })
  26. // #endif
  27. // #ifdef H5
  28. let event = window.event || {}
  29. let clipboard = new ClipboardJS("", {
  30. text: () => data
  31. })a
  32. clipboard.on('success', (e) => {
  33. ("function" == typeof callback) && callback(true)
  34. clipboard.off('success')
  35. clipboard.off('error')
  36. clipboard.destroy()
  37. });
  38. clipboard.on('error', (e) => {
  39. ("function" == typeof callback) && callback(false)
  40. clipboard.off('success')
  41. clipboard.off('error')
  42. clipboard.destroy()
  43. });
  44. clipboard.onClick(event)
  45. // #endif
  46. }
  47. };
  48. module.exports = {
  49. getClipboardData: al.getClipboardData
  50. };

#使用步骤

  1. const al = require("@/components/utils/clipboard-al.js");
  2. methods: {
  3. clipboard: function(data) {
  4. al.getClipboardData(data, (res) => {
  5. //...
  6. })
  7. }
  8. }

#获取复制内容

  1. uni.getClipboardData({
  2. success: function (res) {
  3. uni.showToast({
  4. title: `复制的内容为${res.data}`,
  5. icon: 'none'
  6. })
  7. }
  8. });