Bringing bookmarklets back with Clojure and Boot

{tl;dr}

  1. Bookmarklets are freaking awesome.
  2. Bookmarklets > browser extensions.
  3. Here's a nifty new open-source utility for building bookmarklets quickly.

But wait, what are bookmarklets again?


Well, according to bookmarklets.com:

Bookmarklets are simple tools that extend the surf and search capabilities of Firefox and Explorer web browsers.

So, they're, uh... well, I guess that's not very specific. When was this website made, 1998? Oh...

Wikipedia is more helpful, though:

A bookmarklet is a bookmark stored in a web browser that contains JavaScript commands that add new features to the browser. They are unobtrusive JavaScripts stored as the URL of a bookmark in a web browser or as a hyperlink on a web page.

In essence, a bookmarklet is a link that executes JavaScript code when you clicked. For example, this link is a bookmarklet that pops up an alert message.

bookmarklet alert

If you were to drag that link to your browser's Bookmarks bar or folder, it becomes a convenient link that pops up the same alert no matter what page you're viewing.

Bookmarklets are simple, but powerful. Armed with JavaScript, any developer can build programs that make browsers do some really cool stuff, such as:

  • Modify the appearance of the current page, e.g. changing the background color or switching the font to something professional, like Comic Sans.
  • Open a Google search in another tab for highlighted text.
  • Replace all Japanese text on a page with an English translation.
  • Submit the page to a bookmarking ("read this later") service.

bookmarklt comic sans joke

I actually love Comic Sans! Why haven't I heard of bookmarklets before?

If you were a techie in the 90's and early 00's, chances are you know them. But in recent years they've fallen out of favor, and it's not 100% clear why.

One opinion is that the increased implementation of Content Security Policy (CSP) has hindered bookmarklet adoption. In short, when the page you're browsing is served over SSL (i.e. the URL begins with https://), it requires the bookmarklet's JavaScript to also be SSL secure. Meaning, all your code has to be hosted in an SSL-accessible spot.

Now, in the early 2000s, we didn't have services like Dropbox to do this easily...so it could have pushed away developers at the time.

A more likely explanation for the demise of bookmarklets is the rise of browser extensions.

bookmarklet browser addons

Firefox add-ons and Chrome extensions are examples of browser-based programs that overlap with the functionality of bookmarklets. Indeed, extensions provide even deeper functionality, as they can tap into browser features like password and tab management.

But there are also some downsides to extensions:

User Downside:
- Slower: they run in the background and eat up CPU and RAM.
- Invasive: they have the potential to monitor your browsing history and steal your passwords.

Developer Downside:
- Complex: It's a lot harder to build than a bookmarklet. Each browser has its own documentation, process, and requirements.
- Unnecessary work: Amongst other things, you have to design an icon, decide what the drop-down menu contains, and build a tiny UI inside of a pop-up window.

For these reasons, it's my opinion that bookmarklets are preferable to extensions for many use-cases. Not only are they simpler to design, but they are better for the end-user (without whom, developers would, you know, cease to exist).

I'm sold. How do I build a bookmarklet?

The process boils down to writing JavaScript code, URL-encoding it, and stuffing it into a link that starts with javascript:.

Now, on one hand, JavaScript is pretty cool. If I wanted to write a bookmarklet that makes your browser sing Too Many Cooks, I can.

On the other hand, the downside of JavaScript is that it involves...well...JavaScript, an objectively terrible language that was literally designed in 10 days.

bookmarklet javascript comparison

Like many developers fed up with JavaScript's many idiosyncrasies, I feel that writing any non-trivial code in JavaScript is a fool's errand. Fortunately, there are a number of well-designed higher-level languages that compile (or transpile) to JavaScript. At Adzerk, we prefer ClojureScript, a dialect of Clojure.

Writing a bookmarklet in ClojureScript

Don't worry, this isn't a long, complex how-to guide. Rather, our team has already built tasks that automate the process!

One such task is boot-cljs. Boot is a build tool for Clojure and ClojureScript created by Adzerk's own Alan Dipert and Micha Niskin.

Boot's belief is that any good build tool can automate the tedious parts of developing software. The Boot API lets Clojure developers write tasks that perform a specific operation on any project file.

To use our task, write code in a ClojureScript namespace and run boot cljs target to spit out a JavaScript file into the target directory.

The next step to a working bookmarklet is to take the file, URL-encode it, and stick it in a javascript: link on an HTML page.

But why do it manually? Can't we automate that too?

bookmarklet automation

Enter boot-bookmarklet

boot-bookmarklet is a Boot task that fully automates the process described above.

Simply run boot bookmarklet target and the task will compile your ClojureScript namespaces into Javascript, URL-encode each namespace into a javascript: link, and generate an HTML page for you in the target directory.

Then, you can open target/bookmarklets.html in your browser and drag the bookmarklet links into your bookmarks bar (or share with friends).

VoilĂ ! You just made a bookmarklet, ClojureScript-style. It's scotch-time.

In case I've been wordy

To summarize, boot-bookmarklet gives you:

  • A simple way to build arbitrarily complex bookmarklets.
  • An ability to write in a well-designed programming language.
  • A single command for generating the end result.
  • A way to make your computer sing.

bookmarklet singing joke

The bookmarklet Renaissance

I hope I've convinced you why it was a shame that bookmarklets went away. Will you help lead the comeback?

Like the article?

Get notified of future blog posts. Don't worry - we won't make it hard to get to inbox zero: no more than 2 e-mails a month. We promise.