Created
September 27, 2016 21:56
-
-
Save DrDougPhD/05724d2e6c2ba8d1f567c00f6a6c7a73 to your computer and use it in GitHub Desktop.
Bookmarklet to populate your clipboard with preset text by clicking the bookmarklet.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javascript:(function()%7Bcctb_t%3Ddocument.createTextNode(%22Replace%20this%20text%20with%20the%20text%20you%20want%20copied!%22)%3Bdocument.body.appendChild(cctb_t)%3Bcctb_r%3Ddocument.createRange()%3Bcctb_r.selectNode(cctb_t)%3Bwindow.getSelection().addRange(cctb_r)%3Btry%7Bdocument.execCommand('copy')%7Dcatch(err)%7Bconsole.log('Clipboard%20Copy%20Text%20Bookmarklet%3A%20Error%20in%20copying.')%7Ddocument.body.removeChild(cctb_t)%3Bdelete%20cctb_r%3Bdelete%20cctb_t%7D)() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create the text you want copied. | |
txt = "Replace this text with the text you want copied!"; | |
// Please don't use this to store your passwords. | |
// Simulate a text selection in the browser. | |
// 1. Create a text node and append it to the active page. | |
console.log("Clipboard Copy Text Bookmarklet: bookmark pressed."); | |
var txt_node = document.createTextNode(txt); | |
document.body.appendChild(txt_node); | |
// 2. Create a range over this text. | |
var range = document.createRange(); | |
range.selectNode(txt_node); | |
// 3. Tell the window to create a selection of this range. | |
window.getSelection().addRange(range); | |
// 4. Perform copy. | |
try { | |
// Now that we've made a selection, execute the copy command. | |
var successful = document.execCommand('copy'); | |
var msg = successful ? 'successful' : 'unsuccessful'; | |
console.log('Clipboard Copy Text Bookmarklet: Copying of text was ' + msg); | |
} catch(err) { | |
console.log('Clipboard Copy Text Bookmarklet: Error in copying.'); | |
} | |
// Remove the added text node and delete variables. | |
console.log("Clipboard Copy Text Bookmarklet: Cleaning up."); | |
document.body.removeChild(txt_node); | |
delete txt_node; | |
delete txt; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment