Table of Contents

JavaScript Bookmarklets: A Tutorial

Harbor photo used on a blog post about JavaScript Bookmarklets.

Bookmarklets

I sometimes use bookmarklets to customize my web-browsing experience, boost productivity, or make web pages more accessible. While browser extensions and plugins provide simple alternatives to most use cases of bookmarklets, I find that bookmarklets give me more control in terms of privacy and performance.

In this tutorial, you’ll add a working bookmarklet to your browser in under a minute, then learn the one-line template for writing your own, and see what people use them for.

What are bookmarklets? 

A bookmarklet is JavaScript code that can be saved as a bookmark in a web browser. When they are clicked, the JavaScript code is executed on the current web page, allowing the user to perform a given action or add functionality to the page.

Bookmarklets enhance the browsing experience by adding custom features or shortcuts. They can modify the appearance of a webpage, extract information, perform calculations, or interact with other web services. Bookmarklets typically call an API or access the webpage’s Document Object Model (DOM) to manipulate its content. An example bookmarklet is shown below. 

🌸👋🏻 Enjoying this post? Get the next issue of [ret]2read. Once a month, I’ll email you whatever’s on my mind, usually Apple security research, offensive security, open source software freedom, and operating system internals.

Dictionary bookmarklet

Here is an example bookmarklet use case. To use it, highlight a word on a webpage and click the bookmarklet. A new tab will open with Google’s definition of the word. 

javascript:(()=>{let w=String(window.getSelection()).trim();if(!w)w=prompt('Word?');if(w)window.open('https://www.google.com/search?q=define:'+encodeURIComponent(w));})();

Updated version: uses HTTPS, opens the definition in a new tab, and replaces the deprecated escape() with encodeURIComponent(). The original is still on Gist.

Bookmarklet use

To use a bookmarklet, most browsers require you save it as a bookmark in your browser’s bookmarks toolbar or bookmarks menu. When you want to use the bookmarklet, you click on the bookmark, and the JavaScript code is executed on the open web page. More detailed steps are described below.

How to use bookmarklets 

A YouTube video I made on adding a bookmarklet to Safari.

To use the bookmarklet in most browsers, follow these steps:

  1. Create a new bookmark in your web browser by right-clicking on the bookmarks bar or the bookmarks menu and selecting “Add Page” or “Add Bookmark.” (In Safari, bookmark any page first, then edit that bookmark.)
  2. Give the bookmark a name. For example, you can name it “Definitions.”
  3. In the URL or Address field of the bookmark, paste the entire JavaScript code for the bookmarklet you want to use, starting with javascript:. 
  4. Save the bookmark. It should now appear in your bookmarks bar or bookmarks menu.
  5. When you want to use the bookmarklet, open the webpage you wish to perform the action on.
  6. Click on the bookmark you just created. This will execute the JavaScript code and open a new tab or window with the desired functionality.

🌸 Want an ad like this in [ret]2read? Book an issue →

Sponsor

Example Co.: Every Mac patched within a day of Apple’s release

Example Co. enforces macOS updates across your fleet with Declarative Device Management and flags any Mac still exposed to a CVE Apple has already fixed.

Sponsors write this section. They have no input on anything else in [ret]2read.

How to write your own bookmarklet

Every bookmarklet follows the same template: the javascript: prefix, then your code wrapped in a function that runs immediately. The wrapper keeps your variables from leaking into the page and stops the browser from replacing the page with the code’s return value.

javascript:(()=>{ /* your code here */ })();

For example, this one toggles edit mode on any page, so you can click into the text and change it (handy for mocking up copy or cleaning a page before a screenshot). Click it again to turn edit mode off.

javascript:(()=>{document.designMode=document.designMode==='on'?'off':'on';})();

Two cautions: a bookmarklet runs with full access to the page you click it on, so only save code you have read and understand. And some sites’ Content Security Policy can block bookmarklets in some browsers; if nothing happens, try another site to confirm it works.

Why bookmarklets? 

People use bookmarklets for various reasons, including customization, productivity, accessibility, information gathering, and integration with external services. 

1. Customization

Enhance the browsing experience by adding JavaScript to web pages to modify the site’s appearance, behavior, or content. 

2. Productivity

Next, you can automate repetitive tasks or simplify complex actions. Bookmarklets can perform actions such as filling out forms, extracting information, bookmarking pages, translating text, or generating custom reports. 

3. Accessibility

Improve accessibility and usability of websites by adjusting font sizes, colors, or contrast. Additionally, bookmarklets can provide text-to-speech functionality and standardized navigation techniques.

🌸👋🏻 Enjoying this post? Get the next issue of [ret]2read. Once a month, I’ll email you whatever’s on my mind, usually Apple security research, offensive security, open source software freedom, and operating system internals.

4. Information gathering

Extract information from web pages or perform web scraping tasks. Bookmarklets can retrieve data such as article metadata, prices, stock information, or social media metrics. 

5. Integration with external services

Bookmarklets can integrate with external web services or APIs. They can interact with social media platforms, bookmarking services, project management tools, or communication tools, enabling users to share content, save bookmarks, or perform actions on other platforms directly from the current web page.

Conclusion on bookmarklets

Overall, bookmarklets provide a versatile and lightweight approach to augmenting the browsing experience. Whether for accessibility enhancements, seamless integrations, or increased productivity, these snippets of JavaScript code empower us with capabilities right from our bookmarks toolbar!

If you enjoyed this post, consider reading 8 Best VS Code Extensions or Connecting Angular Apps to MySQL. 

Portrait of Olivia Gallucci in garden, used in LNP article.

Written by Olivia Gallucci

Olivia is a senior security engineer, certified personal trainer, and freedom software advocate. She writes about offensive security, open source software, and professional development.

Discover more from [ret]2read

A Monthly OS Internals Publication by Olivia Gallucci. Subscribe now to keep reading and get access to the full archive.

Continue reading