Skip to content

Instantly share code, notes, and snippets.

@rauschma
Last active April 21, 2022 18:30
Show Gist options
  • Save rauschma/801e69dfc254955a7343510b72506355 to your computer and use it in GitHub Desktop.
Save rauschma/801e69dfc254955a7343510b72506355 to your computer and use it in GitHub Desktop.
/*
Functionality – when executed, these lines are copied to the clipboard:
1. The URL of the current web page.
2. The title of the current web page (as originally written)
3. The title of the current web page (converted to sentence case with a crude algorithm)
4. Optionally: The currently selected text (if any).
Installation: Create a bookmark whose URL is the code below (including `javascript:{···}`):
– Tested in Chrome and Safari.
– Both browsers can handle URLs with multiple lines.
– The braces create a new local scope so that variables such as `text` are not global.
*/
javascript:{
function toSentenceCase(str) {
return str[0] + str.slice(1).toLocaleLowerCase();
}
let text = '';
text += document.title + '\n';
text += toSentenceCase(document.title) + '\n';
text += location.href + '\n';
const selection = window.getSelection().toString().trim(); /* via @jitterted */
if (selection.length > 0) {
text += selection + '\n';
}
navigator.clipboard.writeText(text)
.catch((error) => {
alert(String(error));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment