JMM’s notes on

Bookmarklets

Bookmarklets are bookmarks that run JavaScript. They’re like browser extensions, but without actually needing an extension. In fact, I think they may predate extensions. Speaking of history, bookmarklets have sometimes been called “favelets”. Regardless of what you want to call them, you should learn how to use them. They’re like a secret tool for the sophisticated cybersurfer.

Basics

Bookmarklets are javascript: URIs, generally looking something like the following:

javascript:(function () {
    /* Your code here */
})()

Here’s a super basic bookmarklet:

javascript:window.alert("Hello!");

That’ll just make an alert saying “Hello!”. To actually add this to your bookmarks toolbar, you can do one of three things:

  1. Add a new bookmark manually
  2. Drag a link of the bookmarklet to your toolbar
  3. Edit the URL of an existing bookmark

Only the third way seems to work on mobile devices. As of 2024-02-11, I’ve confirmed it works on Firefox for Android and Safari on an iPad (but it seems like Apple might censor this functionality in third-party browsers).

Adding bookmarklets manually

In Firefox, to add a bookmarklet manually, you do the following:

  1. Open up your “Library” (your list of bookmarks) by pressing Control+Shift+O (or by going to the hamburger menu and going to Bookmarks > Manage bookmarks
  2. Select your “Bookmarks Toolbar” from the sidebar
  3. In the menu, select Organize > Add Bookmark
  4. Give your bookmark a name, and then copy your bookmarklet URL to the URL.
  5. (Optional) You can give your bookmarklet a keyword and it’ll run if you type that keyword in the address bar.

Creating bookmarklets from existing bookmarks

On mobile browsers (like Firefox for Android or Safari on iOS), it doesn’t seem like you can straight-up add a bookmark by entering a name and a URL. A workaround is to bookmark a random page, and then edit the name and URL to contain the bookmarklet’s URL.

To use a bookmarklet, click the bookmark in your toolbar. Or, if you’ve given it a keyword, type the keyword.

Examples

Here are some example bookmarklets:

Issues

Firefox Android bookmarklet support

I haven’t found a way to get bookmarklets to work in Firefox for Android. Okay, I found out that bookmarklets do work in Firefox for Android. They’re a little bit wonky though.

Bookmarklet timing issue

A simple bookmarklet that just triggers an alert doesn’t seem to work:

javascript:window.alert("Hello!");

Well actually, I noticed that it would sometimes work, and sometimes wouldn’t. I think the issue comes down to timing. That is, there’s not a problem with alerting but with the fact that the script runs before the last tab comes back into focus.

A workaround is to use a setTimeout so the bookmarklet runs on the next event cycle, like so:

javascript:(()=>{setTimeout(()=>{window.alert("Hello!");});})()

However, the following doesn’t work, and I’m not sure why:

javascript:setTimeout(()=>{window.alert("Hello");},0);

See also Bugzilla 1811582 - Bookmarklets doesn't work from bookmarks.

Content-Security-Policy (CSP) issues

Another issue is that bookmarklets don’t seem to work with pages that have a Content-Security-Policy (CSP) that prevent external scripts. See this GitHub blog post on CSP and bugzilla bug 866522 (Bookmarklets affected by CSP).

Support in general

I get the vague sense that bookmarklets are becoming less supported over time due to security concerns. I think I may have needed to switch some about:config flag to get javascript: URIs to work.