clipboard-behavior.html 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <script>
  2. 'use strict';
  3. (function(document) {
  4. var copyTextarea = document.createElement('textarea');
  5. copyTextarea.setAttribute('id', 'clipboard-textarea');
  6. var style = copyTextarea.style;
  7. style.position = 'absolute';
  8. style.top = '-10000px';
  9. document.body.appendChild(copyTextarea);
  10. window.Chat.ClipboardBehavior = {
  11. copyToClipboard: function(content) {
  12. copyTextarea.value = content;
  13. var range = document.createRange();
  14. range.selectNode(copyTextarea);
  15. window.getSelection().addRange(range);
  16. try {
  17. // Now that we've selected the anchor text, execute the copy command
  18. var successful = document.execCommand('copy');
  19. if (successful) {
  20. app.displayToast('Copied text to clipboard. Paste it where you want!');
  21. } else {
  22. console.log('failed to copy to clipboard', successful);
  23. }
  24. } catch (err) {
  25. console.log('Oops, unable to copy', err);
  26. }
  27. // Remove the selections - NOTE: Should use
  28. // removeRange(range) when it is supported
  29. window.getSelection().removeAllRanges();
  30. }
  31. };
  32. }(document));
  33. </script>