plugin.cjs.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. 'use strict';
  2. var core = require('@capacitor/core');
  3. class ClipboardWeb extends core.WebPlugin {
  4. async write(options) {
  5. if (typeof navigator === 'undefined' || !navigator.clipboard) {
  6. throw this.unavailable('Clipboard API not available in this browser');
  7. }
  8. if (options.string !== undefined) {
  9. await this.writeText(options.string);
  10. }
  11. else if (options.url) {
  12. await this.writeText(options.url);
  13. }
  14. else if (options.image) {
  15. if (typeof ClipboardItem !== 'undefined') {
  16. try {
  17. const blob = await (await fetch(options.image)).blob();
  18. const clipboardItemInput = new ClipboardItem({ [blob.type]: blob });
  19. await navigator.clipboard.write([clipboardItemInput]);
  20. }
  21. catch (err) {
  22. throw new Error('Failed to write image');
  23. }
  24. }
  25. else {
  26. throw this.unavailable('Writing images to the clipboard is not supported in this browser');
  27. }
  28. }
  29. else {
  30. throw new Error('Nothing to write');
  31. }
  32. }
  33. async read() {
  34. if (typeof navigator === 'undefined' || !navigator.clipboard) {
  35. throw this.unavailable('Clipboard API not available in this browser');
  36. }
  37. if (typeof ClipboardItem !== 'undefined') {
  38. try {
  39. const clipboardItems = await navigator.clipboard.read();
  40. const type = clipboardItems[0].types[0];
  41. const clipboardBlob = await clipboardItems[0].getType(type);
  42. const data = await this._getBlobData(clipboardBlob, type);
  43. return { value: data, type };
  44. }
  45. catch (err) {
  46. return this.readText();
  47. }
  48. }
  49. else {
  50. return this.readText();
  51. }
  52. }
  53. async readText() {
  54. if (typeof navigator === 'undefined' ||
  55. !navigator.clipboard ||
  56. !navigator.clipboard.readText) {
  57. throw this.unavailable('Reading from clipboard not supported in this browser');
  58. }
  59. const text = await navigator.clipboard.readText();
  60. return { value: text, type: 'text/plain' };
  61. }
  62. async writeText(text) {
  63. if (typeof navigator === 'undefined' ||
  64. !navigator.clipboard ||
  65. !navigator.clipboard.writeText) {
  66. throw this.unavailable('Writting to clipboard not supported in this browser');
  67. }
  68. await navigator.clipboard.writeText(text);
  69. }
  70. _getBlobData(clipboardBlob, type) {
  71. return new Promise((resolve, reject) => {
  72. const reader = new FileReader();
  73. if (type.includes('image')) {
  74. reader.readAsDataURL(clipboardBlob);
  75. }
  76. else {
  77. reader.readAsText(clipboardBlob);
  78. }
  79. reader.onloadend = () => {
  80. const r = reader.result;
  81. resolve(r);
  82. };
  83. reader.onerror = e => {
  84. reject(e);
  85. };
  86. });
  87. }
  88. }
  89. const Clipboard = core.registerPlugin('Clipboard', {
  90. web: () => new ClipboardWeb(),
  91. });
  92. exports.Clipboard = Clipboard;
  93. //# sourceMappingURL=plugin.cjs.js.map