Christian Heilmann

“Tweet this page” bookmarklet

Update: Moved the code to GitHub, make sure to drag it from there to your toolbars
Update: Fixed now for Chrome – there were some issues with whitespace :(

I like tweeting about pages I read in the following format:

"Headline"
https://example.com

Most “share this” buttons add a lot of cruft and if there is none, it is annoying having to copy the text first and then the URL into Twitter.

With this bookmarklet, you can highlight some text, click the bookmarklet and get a tweet window in a new tab. This is the source in a more readable manner – this will not work as a bookmarklet, see the link below instead!


(function(){
n=getSelection().anchorNode;
if(!n) {
t = document.title;
} else {
t = n.nodeType === 3 ? n.data : n.innerText;
}

t = ‘“’ + t.trim() + ‘”nn’;
window.open(
`https://twitter.com/intent/tweet?text=
${encodeURIComponent(t)}
${document.location.href}`
)

})();

Explanation:

That’s it. Drag the bookmarklet to the toolbar from the GitHub Demo page or check the code.

Voilà, happy tweeting.