clipboard-shims

Shims for GM_setClipboard and GM.setClipboard.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greatest.deepsurf.us/scripts/483593/1304472/clipboard-shims.js

  1. // ==UserScript==
  2. // @name clipboard-shims
  3. // @description Shims for GM_setClipboard and GM.setClipboard.
  4. // @author Jason Kwok
  5. // @namespace https://jasonhk.dev/
  6. // @version 1.0.0
  7. // @license MIT
  8. // ==/UserScript==
  9.  
  10. let GM_setClipboard = function GM_setClipboard(data, type = "text/plain")
  11. {
  12. if (navigator.clipboard?.write)
  13. {
  14. navigator.clipboard.write([new ClipboardItem({ [type]: data })]);
  15. }
  16. else
  17. {
  18. document.addEventListener("copy", (event) =>
  19. {
  20. event.preventDefault();
  21. event.stopImmediatePropagation();
  22.  
  23. event.clipboardData.setData(type, data);
  24. }, { capture: true, once: true });
  25.  
  26. document.execCommand("copy");
  27. }
  28. }
  29.  
  30. GM.setClipboard = GM_setClipboard;