Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to read the clipboard in Firefox, Safari and Chrome using JavaScript?

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?

like image 201
Gil Faria Avatar asked Oct 24 '08 14:10

Gil Faria


People also ask

Can JavaScript access the clipboard?

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.

Can a browser read your clipboard?

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.

Can I use clipboard API?

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.

How do I copy to the clipboard in JavaScript?

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.


2 Answers

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) {         // ...     } }; 
like image 167
eyelidlessness Avatar answered Sep 19 '22 23:09

eyelidlessness


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.

like image 33
agsamek Avatar answered Sep 20 '22 23:09

agsamek