Reputation: 768
In our application, we have a custom paste function that calls window.clipboardData.getData("Text") to get the current clipboard data. It then performs some functions on this data. In Edge, window.clipboardData is undefined. It does seem that getData works when used within the "paste" event though such as the following.
document.addEventListener("paste", function(e) {
var test = e.clipboardData.getData("text/plain");
});
I potentially can design a workaround that will involve this overriding of the paste event, but it would be non-ideal. A solution that can be called outside of an event would be preferable.
As an aside, I read that Edge did not support the clipboard API at one point, but my understanding is that this is fixed, so please find something specifically sustantiating the current functionality (e.clipboardData working but no equivalent to window.clipboardData existing if that is your answer.
Upvotes: 4
Views: 7733
Reputation: 768
As Kaiido noted, it is not possible to get at the pasted content outside of the paste event in Edge (and Chrome for that matter).
Users previously used a custom right-click menu to access "Paste From Excel" functionality to replace content in an editable grid with tab-delimited content from the clipboard. If window.clipboardData is undefined the user received a message saying that you must use the standard CTRL+V paste in this browser.
I then added the listener below which essentially determined if the content was tab-delimited and treated it as a "Paste from Excel" whereas it treated other data layouts as a standard "Paste". This was sufficient for my deployment, but for others, it may be worthwhile to launch a confirm window to verify intention.
document.getElementById(myGridID).addEventListener("paste", function(e) {
var clipboardContent = window.clipboardData ? window.clipboardData.getData("Text") : (e.clipboardData ? e.clipboardData.getData("text/plain") : "");
if(clipboardContent != null && clipboardContent.indexOf('\t') >= 0)
{
MyExcelPasteFunction(myGridID, clipboardContent);
e.preventDefault();
}
});
Upvotes: 1
Reputation: 136668
Edge, like all modern browsers uses the official ClipboardEvent::clipboardData:
inp.onpaste = evt =>
console.log(evt.clipboardData.getData('text'));
<input id="inp">
Go with it. The deprecated and non-standard window::clipboardData should only be used as a mean of legacy support, for older versions of IE.
As to what you wish to do, (paste without user interaction), that goes against the specs recommendations for privacy. You won't be able to do from web-content. You'll need to run your script from an high-privilege script, like an extension.
• Implementations must not let scripts create synthetic clipboard events to get access to real clipboard data (unless the user has configured it to do so).
Upvotes: 5