Bookmark Llama desktop logo

The Ultimate Guide to Bookmarklets

The Bookmark Llama mascot thinking  with a lightbulb switched on above him.

Bookmarklets are small software applications in the form of a bookmarked link stored in a web browser. Unlike standard bookmarks that take you to a specific webpage, bookmarklets execute a JavaScript code snippet that performs a specific function on the current page you’re viewing.

Despite their powerful capabilities and ease of accessibility, bookmarklets remain underutilized. Many people are unaware of their existence or unsure how to harness their full potential. This underutilization is a missed opportunity, as bookmarklets offer a streamlined, no-installation-needed approach to enhancing browsing efficiency and functionality supported in all major browsers (Google Chrome, Mozilla Firefox, Microsoft Edge).

Use Cases for Bookmarklets

  • Virtual Interaction with Page Elements: Bookmarklets can simulate clicks on buttons or links, streamlining actions that would otherwise require manual interaction.
  • Content Modification: They enable you to alter or customize the content of a webpage, be it text, images, or layout, fitting specific needs or preferences.
  • Data Utilization and Redirection: Bookmarklets can use the data or content from the current page to open a new tab with related information or redirect to a related site.
  • Element Removal: They offer the ability to remove unwanted elements from a page, such as ads or pop-ups, creating a cleaner browsing experience.
  • URL-Based Actions: Bookmarklets can also perform functions that don’t directly interact with the current page, like opening a specific URL under certain conditions or generating HTML content for a new tab.

How do you Add Bookmarklets?

Incorporating bookmarklets into your browser is a seamless process, especially if you already have the bookmarklet code. There are two primary methods to add them: dragging a bookmarklet link directly to your bookmarks bar or manually creating a new bookmark with the bookmarklet code as the URL. Here’s how you can do it:

  1. Find a Bookmarklet Link: Locate a website that provides a ready-to-use bookmarklet link. These are often found on websites dedicated to sharing useful bookmarklets.
  2. Drag to Bookmark Bar: Click and hold the bookmarklet link, then drag it to your browser’s bookmarks bar. Release the mouse button to drop it there.
  3. Verification: Once dropped, the bookmarklet will appear on your bookmark bar with the name set to the text of the link you dragged.

Method 2: Creating Manually

  1. Access Bookmarks Manager: Right-click on the bookmarks bar and select ‘Add Bookmark’ or ‘Add Page’, depending on your browser. In some browsers, you may need to go to the bookmarks menu and choose the option to add a new bookmark.
  2. Naming Your Bookmarklet: In the dialog box, type in a name for your bookmarklet in the ‘Name’ field. Choose a name that helps you quickly identify the bookmarklet’s function.
  3. Pasting the Bookmarklet Code: In the URL field, paste the JavaScript code of your bookmarklet. Ensure it begins with javascript: followed by the necessary code.
  4. Saving the Bookmarklet: Click Save, and the bookmarklet will now be added to your bookmarks bar or the specified folder.

How do you Create a Javascript Bookmarklet?

Creating a JavaScript bookmarklet involves writing a snippet of JavaScript code that can be executed on any webpage you visit. The process is straightforward and offers possibilities, from simple alerts to complex DOM manipulations. Let’s dive into the steps and tips for creating your own JavaScript bookmarklet.

The Basic Structure of Bookmarklets

  1. Start with the JavaScript Protocol: Every bookmarklet begins with javascript:, which tells the browser to execute the following code as JavaScript.
  2. Write an Anonymous Function: Use (() => { /\* Your code here \*/ })(); to define an anonymous function (also known as a lambda function). This structure keeps your code contained, preventing conflicts with the page’s existing JavaScript.
  3. Immediate Function Execution: The (); at the end is crucial as it immediately executes the anonymous function you’ve created.

Examples

Basic Alert

javascript: (() => {
  alert('Hello, World!');
})();

This code creates a bookmarklet that displays an alert box with “Hello, World!“.

Generating and Displaying HTML You can also create bookmarklets that generate HTML content. For instance:

javascript: (() => {
  document.body.innerHTML = '<h1></h1>';
})();

Our Tips for Creating Bookmarklets

Common Bookmarklet Functionality

  • Selecting DOM Elements: To interact with elements on the page, use document.querySelector() or document.querySelectorAll(). For example, document.querySelector(‘h1’) selects the first <h1> element.
  • Modifying the DOM: Once you’ve selected an element, you can modify its properties. For example, document.querySelector(‘h1’).style.color = ‘blue’; changes the color of the first <h1> element to blue.
  • Handling Multiple Elements: If you want to apply changes to multiple elements (like all paragraphs), use document.querySelectorAll(‘p’) and loop through the returned collection.
  • Avoid Overwriting the Page: Instead of overwriting document.body.innerHTML (which removes everything on the page), consider appending to it or modifying specific elements to preserve the page’s functionality.

Debugging

  • Test your bookmarklet code in the browser’s console first. This helps catch errors before adding it as a bookmarklet.
  • For complex bookmarklets, ensure your code is URL-encoded to avoid issues with special characters.
  • Use console.log() statements within your bookmarklet code to track variable values and the flow of execution.
  • Break down your bookmarklet into smaller functions and test each function individually to isolate issues.
  • Check for cross-domain issues. Bookmarklets may not work on web pages loaded from a different domain due to browser security restrictions.
  • Utilize browser developer tools to inspect elements and debug JavaScript errors that may arise when the bookmarklet is executed.

FAQ

How do I enable Bookmarklets?

Bookmarklets are typically enabled by default in most web browsers, as they function like regular bookmarks with the added functionality of executing JavaScript code. There's usually no special process to "enable" them. To use a bookmarklet, simply add it to your bookmarks bar or menu by creating a new bookmark and pasting the JavaScript code (beginning with `javascript:`) into the URL field. When you click on this bookmark, the code will execute on the current page you're viewing. However, it's important to be aware that some browsers or browser settings (like high security or privacy modes) might restrict the execution of JavaScript, which can affect bookmarklets.

How long can a Bookmarklet be?

The length of a bookmarklet is primarily constrained by the maximum URL length that browsers can handle, as bookmarklets are stored as javascript: URLs in bookmarks. Most modern browsers support URLs of around 2,000 to 8,000 characters, with the exact limit varying by browser. However, it's generally recommended to keep bookmarklets as short as possible, both for compatibility across different browsers and for ease of management. Longer scripts are typically hosted externally and loaded by a shorter bookmarklet to avoid these limitations.

Can Bookmarks run code?

Yes, bookmarks can run code in the form of bookmarklets. A bookmarklet is a bookmark that contains JavaScript code in its URL field, which executes when you click on it. This allows the bookmarklet to perform various actions on the current web page, like modifying content or automating tasks. However, it's important to use bookmarklets from trusted sources, as they can potentially pose security risks. While powerful, their functionality may be limited by the browser's capabilities and security settings.

How do I use Bookmarklets in Chrome mobile?

Using bookmarklets in Chrome on a mobile device can be a bit tricky, as mobile browsers don't always have the same features as their desktop counterparts. However, you can still use bookmarklets on Chrome for mobile with a workaround. Here's a step-by-step guide:

  1. Add the Bookmarklet to Your Bookmarks:
    • First, you need to create a new bookmark. You can do this by bookmarking any page; the content of the page is not important at this stage.
    • After bookmarking, go to your bookmarks by tapping the three dots in the upper right corner of Chrome and selecting "Bookmarks."
  2. Edit the Bookmark:
    • Find the bookmark you just created. Tap and hold on the bookmark, then select "Edit."
    • In the editing interface, you need to change two things: the title and the URL of the bookmark. Change the title to something you will remember that's related to what the bookmarklet does.
    • For the URL, delete the current URL and paste in the JavaScript code of your bookmarklet. This code usually starts with javascript: followed by the script.
  3. Using the Bookmarklet:
    • Navigate to the webpage where you want to use the bookmarklet.
    • Then, tap on the address bar, start typing the name of the bookmarklet you saved. Chrome should suggest your bookmarklet under the bookmarks section of the suggestions.
    • Simply tap on the bookmarklet suggestion to run it.
  4. Important Considerations:
    • Make sure you trust the source of the bookmarklet, as it is a script that will run in your browser and could potentially be malicious.
    • Some bookmarklets might not work as expected on mobile devices due to different capabilities and limitations in mobile browsers compared to desktop browsers.

By following these steps, you should be able to use most bookmarklets in Chrome on your mobile device.

How do I use Bookmarklets on my phone?

To use bookmarklets on your phone, follow these steps:

  1. Add the Bookmarklet to Your Bookmarks: First, you need to add the bookmarklet to your bookmarks. This is usually done by bookmarking the page that contains the bookmarklet or by manually creating a new bookmark and pasting the bookmarklet code (usually a JavaScript code snippet) into the URL field.
  2. Accessing the Bookmarklet:
    • For iPhone (using Safari): After adding the bookmarklet to your bookmarks, navigate to the webpage where you want to use the bookmarklet. Then, tap the share icon (the square with an arrow pointing upwards) at the bottom of your screen. In the share menu, swipe left on the bottom row of icons until you see "More". Tap on it and find the "Bookmarks" option. Now, select the bookmarklet from your bookmarks.
    • For Android (using Chrome): On Android, you can access your bookmarks by tapping the three dots in the top-right corner of the Chrome browser, then selecting "Bookmarks." Here, you can find the bookmarklet you added and tap it to execute.
  3. Using the Bookmarklet: After selecting the bookmarklet from your bookmarks, the code within the bookmarklet will run. Bookmarklets can perform a variety of functions, such as formatting a webpage, extracting information, or adding new features temporarily to the site you're viewing.
  4. Limitations and Permissions: Note that some bookmarklets may not work on mobile browsers due to limitations or security restrictions. Additionally, ensure that you trust the source of the bookmarklet, as it can execute scripts that could affect your browsing security.