1

I am wondering if it is possible to store the contents of the clipboard in a string variable.

The following source states that this is not possible using pure JS, but I was thinking hoping that the chrome API would make it possible. (I am developing a chrome extension).

There is a clipboardRead permission, which would lead you to believe that it is possible, but it's description is just:

Required if the extension or app uses document.execCommand('paste'). source

To clarify: I am not trying to copy data to the clipboard or paste data from it. I would like to store the contents of the clipboard into a variable without mutating the clipboard contents in any way.

To further clarify, I can not assume that the clipboard data happens to be sitting in info.selectionText at the time of the operation.

If it is not possible, than I will just have to live with it I suppose, but it seems like something that would be possible to do with an extension.


EDIT: The reason I want to store the value is because I need to utilize the clipboard to perform an operation, but I would like to restore its contents when I am done, so the user doesn't loose whatever used to be in there.

If I just paste the data into a text area and store it from there, then, when I go to put the data back, the user will loose any formatting that they had, which is much better than nothing, but is not optimal.

Also, is there a way that I could store that data, even if it is not a string, (an image for example)? A solution that allows me to do this would undoubtedly implicitly answer the above question as well, but is not necessary. I am primarily looking save string data and would prefer a way to keep the formatting.

Community
  • 1
  • 1
Luke
  • 5,567
  • 4
  • 37
  • 66

1 Answers1

1

There was an experimental clipboard api a while ago, I guess they removed it though. You could always paste the contents into a textarea/contenteditable and get it's value:

function getClipboard() {    
    var el = document.createElement('textarea');
    document.body.appendChild(el);
    el.focus();
    document.execCommand('paste');
    var value = el.value;
    document.body.removeChild(el)
    return value;
}
console.log(getClipboard());
rgthree
  • 7,217
  • 17
  • 21
  • I am new to HTML, does contenteditable mean that the users wont loose their formatting? – Luke Jan 12 '15 at 18:34
  • @LukeP contenteditable is basically a "live" element which the browser renders. You likely do not need it unless you are building a rich text editor of some sort. Note, I also edited the code in my answer, the style.visibility won't work as I had it. – rgthree Jan 12 '15 at 18:44
  • Im going to edit my question to include slightly more context as to why I need to store the string, which will shed light on why I need to keep the formatting. This will likely render your answer weaker, so consider re-reading and updating in a few minutes. Regardless, your answer does yield useful information. – Luke Jan 12 '15 at 19:04
  • @LukeP Well, no formatting gets "lost;" you are not modifying the clipboard at all as a paste is essentially read-only. My answer allows you to get their plain-text clipboard data as-is, formatted exactly as it was copied. did you try the code and, if so, was it not doing what you expected? – rgthree Jan 12 '15 at 19:36
  • I have not had a chance to try it yet, but as you say, it allows you to get plain-text only. If their data has formatting it will be lost, not in the act of storing the data but in the effort to restore it after I erase in the other operation. – Luke Jan 12 '15 at 19:53
  • 3
    This does not work anymore. Can you double check that this works eg with latest chrome? Thanks – Jimmy Kane Mar 09 '16 at 22:32