I'm trying to read the contents of the clipboard using JavaScript. With Internet Explorer it's possible using the function
window.clipboardData.getData("Text")
Is there a similar way of reading the clipboard in Firefox, Safari and Chrome?
Accessing the OS clipboard using browser JavaScript has been possible for several years using document. execCommand() . Unfortunately, there are some problems: clipboard access is synchronous, which has performance and security implications.
Sites should never have access to the content of the clipboard, at least not without user permission. Chrome and other Chromium-based browsers have no such restriction currently. The makers of the Brave web browser considered adding the user gesture requirement in 2021, but this has not been implemented in the browser.
The Clipboard API can be used to implement cut, copy, and paste features within a web application. Use of the asynchronous clipboard read and write methods requires that the user grant the web site or app permission to access the clipboard.
To copy text with the new Clipboard API, you will use the asynchronous writeText() method. This method accepts only one parameter - the text to copy to your clipboard.
Safari supports reading the clipboard during onpaste
events:
Information
You want to do something like:
someDomNode.onpaste = function(e) { var paste = e.clipboardData && e.clipboardData.getData ? e.clipboardData.getData('text/plain') : // Standard window.clipboardData && window.clipboardData.getData ? window.clipboardData.getData('Text') : // MS false; if(paste) { // ... } };
Online Spreadsheets hook Ctrl+C, Ctrl+V events and transfer focus to a hidden TextArea control and either set it contents to desired new clipboard contents for copy or read its contents after the event had finished for paste.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With