A simple bookmarklet to share the current page

Posted on

Cover Image for A simple bookmarklet to share the current page

In 2019, Christian Heilmann introduced a brilliant little bookmarklet on DEV Community — a tool designed to make sharing the webpage you’re viewing on Twitter as simple as a single click.

But as technology evolves, so do platforms. Twitter’s changes to its tweet creation process rendered the original bookmarklet obsolete.

I've updated Christian's code to fix this issue, so now the bookmarklet works again.

You can check out the updated code below or on this bookmarklet-tweet-current-page.js Gist.

Tweet current page bookmarklet

javascript:(function(){n=getSelection().anchorNode;if(!n){t=document.title;}else{t=n.nodeType===3?n.data:n.innerText;}t=%27“%27+t.trim()+%27”\n\n%27;window.open(`https://twitter.com/compose/tweet?text=${encodeURIComponent(t)}${document.location.href}`)})();

Share on Bluesky bookmarklet

I’ve also created an equivalent bookmarklet for Bluesky, giving you the same one-click convenience to share pages on this rising platform.

The code for this is below or on this bookmarklet-share-bluesky.js Gist.

javascript:(function(){n=getSelection().anchorNode;if(!n){t=document.title;}else{t=n.nodeType===3?n.data:n.innerText;}t=%27“%27+t.trim()+%27”\n\n%27;window.open(`https://bsky.app/intent/compose?text=${encodeURIComponent(t)}${document.location.href}`)})();

How to create a bookmarklet

Creating the bookmarklet is surprisingly easy. Here’s how to do it:

1. Open Your Browser’s Bookmark Manager

In most browsers, you can do this by right-clicking the bookmarks bar and selecting “Add page” or “New bookmark.”

2. Add a New Bookmark

Give it a name like “Share on Twitter” or “Share on Bluesky”

3. Paste the JavaScript Code

Paste the JavaScript code from one of the examples above.

4. Save the Bookmarklet

Click “Save” or “Done,” and you’re all set!

How to use

Now that you’ve created the bookmarklet, simply click the bookmarklet in your bookmarks bar, and the current page you are on will be shared in a new tab.