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);
Here is a shorter stringified version with the text to copy in front to make it easier to spot in the bookmark manager. Just replace
TXT
with whatever you want to copy.javascript:{t='TXT';d=document;d.body.appendChild(Object.assign(d.createElement('textarea'),{value:t})).select();d.execCommand('copy');}
Here's a variation with a pop-up alert displaying what was copied. I prefer this one, but if you had something sensitive in there like a password then you might want to use the one above or change the alert at the end to not display the input (remove the
+t
at the end).javascript:{t='TXT';d=document;d.body.appendChild(Object.assign(d.createElement('textarea'),{value:t})).select();d.execCommand('copy');alert('Text Copied\n'+t);}
I've found it very useful and have a bunch of bookmarklets in a folder in my bookmark bar for different usernames and such. You can use either single or double quotes for strings in JS so if your text contained one you could enclose it in the other (eg.
t="Joe's final words"+' were "good bye".'
) You could even experiment with setting t to a JS function to copy the current date or something queried from the current page you are on. Lots of possibilities!