mckinley.cc Home Blog Notes Twtxt

An Acceptable Use for JavaScript

June 2nd, 2022

JavaScript is a heavily overused programming language. People seem to have it in their heads that it is a good way to construct a document as well as a good general-purpose programming language. I believe it is neither. I have kept my Web site JavaScript-less so far and I intend to do so far into the future. One thing JavaScript is good for, however, is for bookmarklets.

This post is the first in my series about bookmarklets. The other posts are linked below.

  1. An Acceptable Use for JavaScript
  2. Defining a Favicon for a Bookmarklet
  3. Bringing Back a Useful Browser Feature With a Bookmarklet

Before we talk about bookmarklets, I want to talk about browser extensions. Browser extensions, which are also written in JavaScript most of the time, are heavy. There's usually a lot of code running, and it can be difficult to read it, even if the extension is free software, because of that. Some browser extensions are good and perform tasks that vastly improve the Web browsing experience, like uBlock Origin. Most browser extensions, however, perform very simple tasks and include tons of spyware on top of it.

In addition, browser extensions are always running in the background and, depending on the permissions you've given it, have access to extremely sensitive information about your browsing habits. As a general rule of thumb, I try to only use browser extensions that satisfy the requirements below.

  1. The extension is free software.
  2. The extension provides meaningful features that positively affect how the core functions of the Web browser work.

But, sometimes I want to automate something simple to save me some time. That's when I reach for a bookmarklet. Bookmarklets are simply bookmarks that run some code on the current page. They are better for simple tasks than proper browser extensions for the following reasons.

  1. They only run when you click the button.
  2. Even when minified, it is usually feasible to read every line of code yourself.
  3. They work across many Web browsers.
  4. They are distributed on normal, independent websites like this one instead of centralized platforms.
  5. The act of writing a bookmarklet is more likely than a browser extension to fit in the XKCD time chart.

Here's an example of a simple bookmarklet.

window.location.href = 'https://web.archive.org/web/*/' + window.location.href

You're not missing anything, that's it. Even if you've never written a line of code in your life, it isn't too difficult to see what this does. Wrap it in an anonymous function, put javascript: at the front and you get the finished product.

javascript:(function(){window.location.href = 'https://web.archive.org/web/*/' + window.location.href})()

You can put that into the URL field of a new bookmark, or you can click and drag the following link to your bookmarks bar: Search WM. Now, go to any Web page you want and click the button. It will search the Internet Archive's Wayback Machine for archived copies of the page. Neat, huh?

Here are a couple of similar bookmarklets that I use regularly.

Search archive.today
window.location.href = 'https://archive.today/' + window.location.href
W3C Validator
window.location.href = 'https://validator.w3.org/check?uri=' + encodeURIComponent(window.location.href)

If you're interested in adding an icon to your bookmarklets, see the next post in the series: Defining a Favicon for a Bookmarklet.