I want to be able to initiate real paste event when user clicks. I can understand this may be a security issue, because if any webpage had access to users clipboard, that would be bad. So I thought all browsers disallow accessing clipboard data.
But for example in google docs (in the word-like application), I can Paste from custom context menu (right mouse click on a html element pretending to be a context menu), even if the clipboard data has been copied to clipboard in different application like Microsoft Paint. This works in Google Chrome browser, which is the browser of my interest.
I thought they do it using flash, but it still works even if I completely disable flash in chrome. There was a question about this already, but the answer mentioned there is not correct. Another answer of that question suggests that google is using a chrome extension for this, but it still works even if I disable all extensions in chrome.
How to reproduce in windows:
I know how to access the clipboard data if user presses Ctrl+V on my webpage, because this triggers Paste event on the current window. But, how do I either access the clipboard data or initiate the paste of actual clipboard data (eg. a bitmap copied in mspaint) in javascript (or using jquery) while user just clicks a button or div?
This is because Google Docs, not your browser, is rendering the right-click menu. This might seem superficial, but it matters. Desktop apps have access to your clipboard, which is why they can offer a Paste button.
You can use paste special to decide which formatting gets copied when copying cells. Paste special works only within a single spreadsheet. You can't copy cells from one spreadsheet and use paste special in a spreadsheet open in another tab. On your computer, open a spreadsheet in Google Sheets.
Conclusion. If you can't use the copy-paste option in Chrome, disable your extensions, clear the cache and update the browser. On the other hand, if the webpage you're visiting disabled text selection, press CTRL + U to access the source code. We hope this guide helped you restore the copy-paste function in Chrome.
I want to be able to initiate real paste event when user clicks. I can understand this may be a security issue, because ...
The above is the bottom line..
Having this code JS Fiddle
var Copy = document.getElementById('copy'), Cut = document.getElementById('cut'), Paste = document.getElementById('paste'); // Checking Clipboard API without an action from the user console.log('Copy:' + document.queryCommandSupported('copy')); console.log('Cut:' + document.queryCommandSupported('cut')); console.log('Paste:' + document.queryCommandSupported('paste')); //Now checking the clipboard API upon a user action Copy.addEventListener('click', function(){ console.log('Copy:' + document.queryCommandSupported('copy')); }); Cut.addEventListener('click', function(){ console.log('Cut:' + document.queryCommandSupported('cut')); }); Paste.addEventListener('click', function(){ console.log('Paste:' + document.queryCommandSupported('paste')); });
<button id="copy">Copy</button> <button id="cut">Cut</button> <button id="paste">Paste</button>
If you check it with different browsers you'll see how browsers react differently dealing with the Clipboard API using queryCommandSupported()
the result is:
Chrome 47:
false
, Cut:false
, Past:false
true
, Cut:true
, Paste:false
Firefox 43:
true
, Cut:true
, Past:false
true
, Cut:true
, Paste:false
IE11: - I believe it's same in Edge
true
, Cut:true
, Past:true
true
, Cut:true
, Paste:true
Safari: - same for iOS Safari
document.execCommand()
.For detailed browsers support for Clipboard API caniuse.com/#search=clip
Also browsers support paste using right-click context menu for conteneditable="true"
elements as in this JS Fiddle 2
But for example in google docs (in the word-like application), I can Paste from custom context menu (right mouse click on a html element pretending to be a context menu), even if the clipboard data has been copied to clipboard in different application like Microsoft Paint.
I thought they do it using flash, but it still works even if I completely disable flash in chrome.
From Google Apps Script documentation for developing add-ons for Google Apps:
"The Platform: Apps Script's code editor is a web app that you launch from within Google Sheets, Docs, or Forms. The language is based on JavaScript, but executes on Google's servers rather than directly in the user's browser
(with the exception of client-side user interfaces...".
Since it is being executed on their servers, I think they can enable certain features, or even use Java, for better experience.
EDIT 1:
If you check clipboard.js, you'll see the library doesn't have an option for "paste", only "copy" and "cut", also in the "Browser Support" section at the end of the page you'll see that the library relies on execCommand API, and doesn't work in Safari.
EDIT 2:
Upon an update of the question as well as comments, for the Google docs part, I pressed Prt Sc to have a screen-shot in the clipboard, opened Google docs in Chrome, right-clicked then clicked on "Paste" from the custom context menu and for sure it works, opened Google docs in Firefox and as soon I clicked on "paste" option I got this response
Also for the record, I tried to do the same in IE11 and it is still trying to launch Google docs since 21 minutes.
So the conclusion is probably, and mostly, "Google" Chrome has an Exception: - something like a conditional statement - for "Google" docs and other Google services in their browser, also I think @julien-gregoire was right about its being an extension in Firefox.
From this Google docs support page:
For security reasons, most browsers don't allow web apps like Docs, Sheets, and Slides to use your computer's clipboard through menus.
However, if you use Chrome, you can give permission to access your clipboard by installing the Google Drive Chrome app. This allows you to use the right-click menu to copy and paste content (or select "Copy" or "Paste" from the Edit menu in the toolbar). To install the app, visit the Chrome Web Store.
And this smallbusiness page:
Without keyboard shortcuts, you have two more options for copying and pasting: either going to the "Edit" menu and selecting "Copy" or "Paste," or right-clicking in the document and selecting either "Copy" or "Paste" from the context menu. In Google Docs, both these options are only available to Chrome users who have the Google Drive Web app installed. The app is free, but is not available for other browsers.
So seems they have already implemented that app as built-in feature in newer versions of Chrome.
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