Skip to content
Advertisement

How to convert a bookmarklet into a Greasemonkey userscript?

Is there a easy way to do this. And is there anything that needs to be changed due to differences in how it is ran?

Advertisement

Answer

The easiest way to do this:

  1. Run the bookmarklet code through a URL decoder. so that javascript:alert%20('Hi%20Boss!')%3B, for example, becomes:
    javascript:alert ('Hi Boss!');

  2. Strip the leading javascript: off.   Result: alert ('Hi Boss!');

  3. Add this code to the end of your Greasemonkey file. For example, create a file named,
    Hello World.user.js, with this code:

    // ==UserScript==
    // @name            Hello World!
    // @description     My first GM script from a bookmarklet
    // @include         https://stackoverflow.com/questions/*
    // @grant           none
    // ==/UserScript==
    
    alert ('Hi Boss!');
    
  4. Open Hello World.user.js with Firefox (CtrlO ).   Greasemonkey will prompt to install the script.

  5. Now the bookmarklet code will run automatically on whatever pages you specified with the @include and @exclude directives.

  6. Update: To ensure maximum compatibility, use the @grant none directive that was added in later versions of Greasemonkey and Tampermonkey.


IMPORTANT:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement