How to get copied text from JavaScript
tl;dr use Method 3 if you really think that not being honest with the user about what you're doing is justified- it's a good workaround, although many might consider it an exploit.
Looking at the W3 specification (https://www.w3.org/TR/clipboard-apis/#Cases) we can see some insight into why these events (and the still-developing API) exist in the first place. Specifically that copy
is there for you to change what was copied in the case of your target not being what the user would actually want to end up on their clipboard, while paste
exists to let you handle transfering that data into your application.
Knowing this, we can come to some conclusions:
- Method 1: The spec does not go into much detail about clipboard security, except for making the intention that implementations should work to protect users. I'm not surprised, therefore, that the copied data is hidden from you; it seems like a sensible decision by the implementers. More than that, looking at the algorithms set-out by the spec, it's quite possible that there is not data in the clipboard yet, as the aim of this event is to allow you to set what should end up their.
- Method 2: This seems much more the intention of the authors. If an application is going to access the clipboard, it should really get permission from the user. Especially because the clipboard might contain data from outside of your page.
- Method 3: It's an exploit, but it's hard to see cases where it wouldn't work. From an implementer's perspective it's hard to block- as they would have to check event delegate functions for calls; compared to just 'not making the data readily available'. It's also, arguably, secure enough as the only information you can access is information that is already on your own document.
Get clipboard data
This is a toughie. If I recall correctly, IE allows access to the clipboard, but by default Firefox does not due to security issues. I had to do this for a project I was working on, and was forced to use a small SWF file that did the copying.
http://www.jeffothy.com/weblog/clipboard-copy/
How do I read text from the clipboard?
You can use the module called win32clipboard, which is part of pywin32.
Here is an example that first sets the clipboard data then gets it:
import win32clipboard
# set clipboard data
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText('testing 123')
win32clipboard.CloseClipboard()
# get clipboard data
win32clipboard.OpenClipboard()
data = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
print data
An important reminder from the documentation:
When the window has finished examining or changing the clipboard,
close the clipboard by calling CloseClipboard. This enables other
windows to access the clipboard. Do not place an object on the
clipboard after calling CloseClipboard.
Related Topics
Vue.Js Get Selected Option on @Change
Formdata.Append("Key", "Value") Is Not Working
How to Automatically Set the Focus to a Textbox When a Web Page Loads
How to Check If a Variable Is an Integer in JavaScript
Fastest Way to Convert JavaScript Nodelist to Array
How to Check If a JavaScript Object Is a Dom Object
How to Auto-Submit an Upload Form When a File Is Selected
HTML Anchor Link - Href and Onclick Both
Remove Whitespaces Inside a String in JavaScript
How to Create an Asynchronous Function in JavaScript
Make React Useeffect Hook Not Run on Initial Render
Why How to Use a Function Before It's Defined in JavaScript
How to Get the HTML of a Div on Another Page with Jquery Ajax
Reactjs: "Uncaught Syntaxerror: Unexpected Token <"
HTML Table with Fixed Headers and a Fixed Column
How to Reset a Form Using Jquery with .Reset() Method