Create Bookmarklet (browser bookmark that executes Javsacript) to copy a given text to Clipboard.
This is the base javascript:
(function (text) {
var node = document.createElement('textarea')
var selection = document.getSelection()
node.textContent = text
document.body.appendChild(node)
selection.removeAllRanges()
node.select()
document.execCommand('copy')
selection.removeAllRanges()
document.body.removeChild(node)
})('Text To Copy')
Minify it (you can use UglifyJS or any other) to get something like this:
!function(a){var b=document.createElement("textarea"),c=document.getSelection();b.textContent=a,document.body.appendChild(b),c.removeAllRanges(),b.select(),document.execCommand("copy"),c.removeAllRanges(),document.body.removeChild(b)}("Text To Copy");
Prefix it with javascript:
:
javascript:!function(a){var b=document.createElement("textarea"),c=document.getSelection();b.textContent=a,document.body.appendChild(b),c.removeAllRanges(),b.select(),document.execCommand("copy"),c.removeAllRanges(),document.body.removeChild(b)}("Text To Copy");
Then add it as a new bookmark, in the URL field:
You can do stuff like a "Copy page title" bookmarklet by simply passing document.title
instead of the fixed string:
javascript:!function(a){var b=document.createElement("textarea"),c=document.getSelection();b.textContent=a,document.body.appendChild(b),c.removeAllRanges(),b.select(),document.execCommand("copy"),c.removeAllRanges(),document.body.removeChild(b)}(document.title);
Upon creating the bookmark, it only works once for me. Subsequent clicks show the following message in the console:
document.execCommand('cut'/'copy') wurde abgelehnt, weil es nicht von innerhalb einer kurz dauernden benutzergenerierten Ereignisbehandlung aufgerufen wurde.
There might be solutions down the rabbit hole here: https://stackoverflow.com/q/41094318.
All I want is an easy way to copy my rather long emailadress to the clipboard, preferably with a single click on a bookmark...