Skip to content

Instantly share code, notes, and snippets.

@mcnamee
Last active July 25, 2022 02:09
Show Gist options
  • Save mcnamee/e4bee68ebec2e3542720dad6adfc6841 to your computer and use it in GitHub Desktop.
Save mcnamee/e4bee68ebec2e3542720dad6adfc6841 to your computer and use it in GitHub Desktop.
Copy & Paste bookmarklets for website that block copy/paste (eg. MS Teams, Defender)
javascript:(function(){
/*
* Highlight text and press the bookmarklet - the text is now in your clipboard
* Installation: Add this javascript to a new browser bookmark in the URL field
*/
let text = "";
const activeEl = document.activeElement;
const activeElTagName = activeEl ? activeEl.tagName.toLowerCase() : null;
/* Extract text from textarea/text input */
if (
(activeElTagName == "textarea") || (activeElTagName == "input" &&
/^(?:text|search|password|tel|url)$/i.test(activeEl.type)) &&
(typeof activeEl.selectionStart == "number")
) {
text = activeEl.value.slice(activeEl.selectionStart, activeEl.selectionEnd);
/* Extract text from any other element (eg. div) */
} else if (window.getSelection) {
text = window.getSelection().toString();
/* Otherwise alert us */
} else {
return alert('Please highlight text first');
}
/* Add the text to the clipboard */
navigator.clipboard.writeText(text);
})();
javascript:(async function(){
/*
* Ensure text is on your clipboard, then press into a website element (eg. a textarea)
* and press the bookmarklet to add the clipboard text into the element
* Installation: Add this javascript to a new browser bookmark in the URL field
*/
const text = await navigator.clipboard.readText();
const activeEl = document.activeElement;
const activeElTagName = activeEl ? activeEl.tagName.toLowerCase() : null;
/* Add text to an input or textarea */
if (activeElTagName === 'textarea' || activeElTagName === 'input') {
activeEl.value += text;
/* Add text to another element (eg. div) */
} else if (window.getSelection) {
activeEl.innerHTML += text;
/* Otherwise alert us */
} else {
alert('Please press into an element first');
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment