Greasemonkey – execute custom javascripts on any page

September 7th, 2006. Tagged: firefox, JavaScript

If you want to try executing custom local scripts on any page you visit, try Greasemonkey. Here's a 10 seconds tut.

The task is to create a custom script and to make sure it's executed every time you leave a page on

  1. Install Gresemonkey from here
  2. Create a file phpied.user.js (all your custom scripts must end in .user.js) with the following
     var start = new Date();
     window.addEventListener("unload", function(e) {
        var end = new Date();
        var diff = Math.floor((end - start) / 1000);
        alert("Man, I spent " + diff +
              " of my precious seconds on this guy's page!" +
              " Now that's called investment!"
    }, false);
  3. Open phpied.user.js in the browser. You'll see a message from Greasemonkey inviting you to install.
  4. Click Install... and you're done. Now this script will execute on every single page you hit. To change it so that it executes only when you visit do:
  5. Right-click the monkey icon in the bottom-right of the browser screen. Select Manage User Scripts.
  6. Click the * in the Included Pages list, then hit Edit. Type "*" This means "execute this script on every page on". Click OK.
  7. Reload this page to see the script in action.

N.B. To modify a user script, do not modify the original file where you had it initially on your file system, won't work, I tried it 😉 Instead, modify the copy that GM has stored. Right-click the monkey icon -> Manage -> select your script in the listing on the left and click Edit.

I've read before about the Greasemonkey Firefox extension, but never tried it before yesterday. Never tried probably because of a comment on the's article about Greasemokey. The comment I found so funny, yet true, was "I just don't like the idea of having to spend time on another person's website when I barely get enough time to spend on my own." Well, sometimes one might like to try custom scripts on his/her own site, for example to test some stuff on the production server without the risk of breaking something.

More resources:
- Home page
- Tutorial @ sitepoint
- Free ebook
- User scripts

Tell your friends about this post on Facebook and Twitter

Sorry, comments disabled and hidden due to excessive spam.

Meanwhile, hit me up on twitter @stoyanstefanov