Reputation: 5708
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.
Upvotes: 1
Views: 4346
Reputation: 7273
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());
Upvotes: 1