Copy image to clipboard

This worked across all browsers (as of 2016). I have uploaded on GitHub as well: https://github.com/owaisafaq/copier-js //Cross-browser function to select content function SelectText(element) { var doc = document; if (doc.body.createTextRange) { var range = document.body.createTextRange(); range.moveToElementText(element); range.select(); } else if (window.getSelection) { var selection = window.getSelection(); var range = document.createRange(); range.selectNodeContents(element); selection.removeAllRanges(); selection.addRange(range); } } … Read more

Using execCommand (Javascript) to copy hidden text to clipboard

Here’s my solution that doesn’t use jQuery: function setClipboard(value) { var tempInput = document.createElement(“input”); tempInput.style = “position: absolute; left: -1000px; top: -1000px”; tempInput.value = value; document.body.appendChild(tempInput); tempInput.select(); document.execCommand(“copy”); document.body.removeChild(tempInput); } <!DOCTYPE html> <html> <head> <title>Set Clipboard</title> </head> <body> <button onclick=”setClipboard(‘foo loves bar’)”>Set Clipboard</button> </body> </html>

Copy output of a JavaScript variable to the clipboard

function copyToClipboard(text) { var dummy = document.createElement(“textarea”); // to avoid breaking orgain page when copying more words // cant copy when adding below this code // dummy.style.display = ‘none’ document.body.appendChild(dummy); //Be careful if you use texarea. setAttribute(‘value’, value), which works with “input” does not work with “textarea”. – Eduard dummy.value = text; dummy.select(); document.execCommand(“copy”); document.body.removeChild(dummy); … Read more

How to copy data to clipboard in C#

There are two classes that lives in different assemblies and different namespaces. WinForms: use following namespace declaration, make sure Main is marked with [STAThread] attribute: using System.Windows.Forms; WPF: use following namespace declaration using System.Windows; console: add reference to System.Windows.Forms, use following namespace declaration, make sure Main is marked with [STAThread] attribute. Step-by-step guide in another … Read more

Copy / Put text on the clipboard with FireFox, Safari and Chrome

For security reasons, Firefox doesn’t allow you to place text on the clipboard. However, there is a workaround available using Flash. function copyIntoClipboard(text) { var flashId = ‘flashId-HKxmj5’; /* Replace this with your clipboard.swf location */ var clipboardSWF = ‘http://appengine.bravo9.com/copy-into-clipboard/clipboard.swf’; if(!document.getElementById(flashId)) { var div = document.createElement(‘div’); div.id = flashId; document.body.appendChild(div); } document.getElementById(flashId).innerHTML = ”; var … Read more

How to add extra info to copied web text

2022 Update More complex solution that handles rich text formatting. The 2020 solution is still relevant if you only deal with plain text. const copyListener = (e) => { const range = window.getSelection().getRangeAt(0), rangeContents = range.cloneContents(), pageLink = `Read more at: ${document.location.href}`, helper = document.createElement(“div”); helper.appendChild(rangeContents); event.clipboardData.setData(“text/plain”, `${helper.innerText}\n${pageLink}`); event.clipboardData.setData(“text/html”, `${helper.innerHTML}<br>${pageLink}`); event.preventDefault(); }; document.addEventListener(“copy”, copyListener); #richText … Read more

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 … Read more