Advertisement
menixator

Tampermonkey - GM_setClipboard.js

Feb 18th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var copy = function(opts) {
  2.   if (typeof opts === 'string') {
  3.     opts = {
  4.       content: opts,
  5.       mimetype: 'text/plain'
  6.     };
  7.   }
  8.   var frame = document.createElement("iframe");
  9.   document.body.appendChild(frame);
  10.   try {
  11.     frame.contentDocument.designMode = "on";
  12.     /*
  13.         # if html is opts.type
  14.         #   frame.setAttribute "sandbox", "allow-same-origin"
  15.         #   frame.contentDocument.documentElement.innerHTML = opts.content
  16.         #   frame.contentDocument.execCommand "selectAll", not 1, null
  17.         # else
  18.         */
  19.     frame.contentDocument.oncopy = function(b) {
  20.       b.clipboardData.setData(opts.mimetype || "text/plain", opts.content);
  21.       return b.preventDefault();
  22.     };
  23.     frame.contentDocument.execCommand("copy", !1, null);
  24.     frame.contentDocument.designMode = "off";
  25.   } catch (err) {
  26.     console.error("Couldn\'t copy the text!");
  27.   }
  28.   frame.parentNode.removeChild(frame);
  29. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement