Skip to content

Instantly share code, notes, and snippets.

@peterdalle
Created October 7, 2020 09:34
Show Gist options
  • Save peterdalle/eae34551f279b9a4f029ced7aaf5f2d1 to your computer and use it in GitHub Desktop.
Save peterdalle/eae34551f279b9a4f029ced7aaf5f2d1 to your computer and use it in GitHub Desktop.
Bookmarklet: copy document title and URL to clipboard + highlighted text

Copy title and URL to clipboard bookmarklet

A bookmarklet to copy the document title and page URL to the clipboard. If you select/highlight text on the web page, the text is appended to the beginning of the text.

Full version:

javascript: (
function () {
	let selection = "";
	if (window.getSelection() != '') {
		selection = window.getSelection().toString();
	}
	text = selection + " " + document.title + " " + location.href;
	var b = document.createElement("textarea"), c = document.getSelection();
	b.textContent = text, document.body.appendChild(b), c.removeAllRanges(), b.select(), document.execCommand("copy"), c.removeAllRanges(), document.body.removeChild(b)
}
)()

Bookmarklet version:

javascript:( function () { let selection = ""; if (window.getSelection() != '') { selection = window.getSelection().toString(); } text = selection + " " + document.title + " " + location.href; var b = document.createElement("textarea"), c = document.getSelection(); b.textContent = text, document.body.appendChild(b), c.removeAllRanges(), b.select(), document.execCommand("copy"), c.removeAllRanges(), document.body.removeChild(b) } )()

Modified from https://michalparkola.com/roam-bookmarklet.

Convert JavaScript to bookmarklets with http://js.do/blog/bookmarklets/.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment