Greasemonkey – A customized way to browse internet

November 11, 2010, by | Start Discussion


I am writing this article by considering that my dear readers are well aware of Mozilla Firefox which  may be the best web browser after Internet Explorer. As it always happens, a new thing emerges by introducing new concepts for users. Similarly Mozilla Firefox introduced new add-on to the internet world -Greasemonkey. You might be thinking what this monkey does for us? Answer is “Greasemonkey allows you to customize the way a webpage is displayed using small bits of JavaScript”. Yes! This is a monkey who does this magic for us.

What actually Greasemonkey is?

Greasemonkey is a Firefox add-on or plug-in which extends the functionality of the browser by making changes as per the user’s instructions written in user scripts. Greasemonkey can be used for adding new functions to web pages, fixing rendering bugs, combining data from multiple web pages, and numerous other purposes. These user scripts are written in JavaScript which is a lightweight programming language to add interactivity to HTML pages. It is obvious that to write your user script, you have to learn JavaScript first. This JavaScript manipulates the contents of a web page using the Document Object Model (DOM) interface and provides results that the user wants to see. Using the Document Object Model (DOM) interface means that the user scripts interacts with the  objects within HTML, XHTML and XML documents. Objects or elements of DOM may be addressed and manipulated within the syntax of the JavaScript in user script. And this means that we have total control on the web page we see through Firefox if we have Greasemonkey installed.

Greasemonkey user scripts execute each time the page is loaded in browser and the user script repeatedly does this to make changes permanent. Some user scripts may be site specific which works only on a particular page. (for example Auto fill the username and password for Gmail as browser detects Gmail).

What can you do with Greasemonkey?

  • Automatically fill forms on specific pages
  • Alter the formatting of text, borders, graphics, etc on a page.
  • Remove specific content, such as advertising, popups, even whole sections of a page.
  • Alter content and layout beyond what the page author considered.
  • Add links, buttons, or any other type of HTML element anywhere on the page.
  • Enhance the content of the pages by retrieving correlating information from related pages on the same site, or other sites.
  • Take advantage of extended JavaScript behavior to add previously non-existent functionality to pages.
  • And lot more…

How does Greasemonkey power user scripts?

JavaScript, itself is a powerful programming extension to any HTML page. But Greasemonkey also has some good methods in its API which really enhances the power and functionality of any user script. The question is how we use it in our user scripts. Below I have listed some methods by task

Values

GM_deleteValue

This deletes a value from chrome that was previously set.

GM_getValue

A function intended to get stored values, see GM_setValue below.

GM_listValues

This API method retrieves an array of preference names that start with the branch's root.

GM_setValue

A function that accepts the name and value to store, persistently. This value can be retrieved later, even on a different invocation of the script, with GM_getValue. 

Resources

GM_getResourceText

Given a defined resource, this method returns it as a string..

GM_getResourceURL

A function that loads an external resource via a URL, such as an image, and returns the string containing the base64 encoded in the data: URL scheme.

Common Task Helpers

GM_addStyle

A function, that takes one parameter, a string of CSS to apply to the page.

GM_xmlhttpRequest

A version of the XMLHttpRequest method underlying AJAX, this API makes arbitrary HTTP request to a page from other server or from same server to load or post data into current page.

unsafeWindow

This object provides access to the raw JavaScript window scope of the content page. It is most commonly used to access JavaScript variables on the page.

Others

GM_log

A function that accepts a parameter which will be routed to the Error Console, useful for examining values when writing a script.

GM_openInTab

Similar in spirit to window.open(), this function accepts a single parameter, the URL of a page to open in a new tab.

GM_registerMenuCommand

An advanced function which allows a user script to register a menu item, and command to run when clicked, in the Firefox user interface.

How to install?

You can see this little monkey on the right bottom corner of Firefox by installing Greasemonkey add-on from this link.

https://addons.mozilla.org/en-US/firefox/addon/748

1.    Follow the above link and say “Add to Firefox”
2.    Now a window will pop-up prompting user to install add-on to Firefox. Click Install Now and restart the browser as instructed.
3.    Aright. Now that greasemonkey is installed, we check whether greasemonkey is enabled or not. To check it, in Firefox, go to   
Tools-> Greasemonkey-> Enable

Here you can manage installed user scripts and add new user scripts.

How to use it?

1.    You can add/import your own script

2.    Also you can manage, enable, disable Greasemonkey, uninstall and edit user scripts. In above image you can see on the right side ‘Included Pages’ and ‘Excluded Pages’. You can add metadata entries for these two options for selected Greasemonkey script. We will discuss description of metadata in the following points.


Now after adding it, the question is that what this monkey actually do. This monkey allows you to add your own scripts on every page load, means when your web page loads then your script does its work which is made for some specific purpose. Greasemonkey website says that 'Greasemonkey is a Firefox extension that allows you to customize the way web pages look and function.' Yes this is the function we actually need to personalize our browser, isn't it?

Resource

Now you have freedom to write your script add it to firefox and do just whatever you want. You have millions of free user scripts on the website http://www.user script.com.

Thousands of script developers have created user scripts to functionalize their browser and uploaded to user scripts.com. Some of them are useful for you or some can give you idea to create your own. Until now various users have written scripts for facebook page management, auto fill forms for particular site, to improve appearance of gmail, yahoo accounts, and also to hide advertisements from any site you visit.

Do it yourself

Now after you have used various user scripts from various usersdon’t you want to try your hand on this exciting facility? Yes you will do this I am sure. Let me tell you there are lots tutorials, books written for the use of Greasemonkey. One of them is Greasmonkey Hacks which teaches you how to write your own script, how to apply them for different purposes. About 100 scripts are described in this book along with source codes. But I recommend you to use your own mind, read some tutorials of JavaScript, take help from JavaScript to develop your own unique script. If you know HTML, JavaScript very well then go for it. You also have Google as the best internet teacher with you.

Supporting Browsers

When Greasemonkey was launched it was written only for firefox. But now browsers like Google Chrome and Mozilla based Flock are also supporting it. Flock and Firefox are directly supporting Greasemonkey by installing as an Add-On from tools menu. In case of Google Chrome Greasemonkey is not directly supported. In earlier versions right after 2.0 you have to trick the  browser setting from installations files to get support of GreaseMonkey. Latest version of Google Chrome allows GreaseMonkey to execute as an extension to Chrome browser. Like Firefox we do not need GreaseMonkey Add-On to be installed to add user scripts. You just have to create your user script and simply drag it into the Chrome browser. Your user script will do actions when target or any page loads.

How to write User script?

1.    Open any text editor you like
2.    Write your user script to execute.
3.    Save as file by adding extension “.user.js” after user script name.

e.g. If  you wish to save user script with MyScript then file will look like this MyScript.user.js 
 

Greasemonkey Metadata

As numerous user scripts exist, there is a provision to differentiate between user scripts installed on Greasemonkey Add-on. For this metadata is added above the code. This metadata tells Greasemonkey about the script itself, where it came from, and when to run it, version info, etc. As an example,

// ==User script==
// @name          Hello World
// @namespace     http://clubhack.com/
// @description   example script to alert "Hello world!" on every page
// @include       *
// @exclude       http://clubhack.com/*
// @exclude       http://www.clubhack.com /*
// ==/User script==

“//” is used to denote metadata line.
You can see metadata wrapper enclosed within

// ==User script==
//
// ==/User script== 

Greasemonkey uses above tags to denote the start and end of your user script metadata. This section may be defined anywhere in your script, but it is usually declared on top of your code.

First item of metadata is

@name

This is the name of your user script. It will be displayed in the install dialog when you first install the script and later in the “Manage User scripts” dialog. It should be as short as possible

@name is optional. If it is present in your user script, it may appear only once. If not present, it shows default name as filename of the user script, minus the .user.js extension.

Next is the namespace,

// @namespace     http://clubhack.com/

This is a URL, and Greasemonkey uses it to distinguish user scripts that have the same name but are written by different authors. If you have a domain name, you can use it as your namespace. Otherwise you can use a default tag: URI.
@namespace is optional. If present in your user script, it may appear only once. If not present, it defaults to the domain from which the user downloaded the user script.

Next comes the description,

// @description   example script to alert "Hello world!" on every page

This is a human-readable explanation of what the user script does. It is displayed in the install dialog when you first install the script, and later in the “Manage User scripts” dialog. It should be no more than two sentences.
@description is optional. If present in your user script, it may appear only once. If not present, it defaults to an empty string.
Don't forget to write the @description. Even if you are only writing user scripts for yourself, you will eventually end up with dozens of them, and administering them all in the “Manage User scripts” dialog will be much more difficult if you don't include a description. It will get easy to differentiate between many user scripts.

The next three lines are the most important items (from Greasemonkey's perspective):
the @include and @exclude URLs.

// @include       *
// @exclude       http://clubhack.com/*
// @exclude       http://www.clubhack.com/*

These lines tell Greasemonkey on which sites you want your user script to execute. Both specify a URL, with the * character as a simple notation for part of the domain name or path. In this case, we are telling Greasemonkey to execute the Hello World script on all sites except http://clubhack.com / and http://www.clubhack.com /. Excludes take precedence over includes, so even though http://clubhack.com / matches * (all sites), it will be excluded because it also matches http://clubhack.com /*.
@include and @exclude are optional. You may specify as many included and excluded URLs as you need, but you must specify each on its own line. If neither is specified, Greasemonkey will execute your user script on all sites (as if you had specified @include *).
 

User scripts Examples

1.     Malware Script Detector

This is the very useful script while you are surfing on internet heavily and not conscious about which site is flagged susceptible to malicious infection by antivirus or Google. Web pages are built using HTML and enhanced using JavaScript. But some websites injects harmful scripts into computer via your browser and you do not get any traces of it. And after some time, the injected code starts to execute and do its designated function. This user script is written to detect and alert when user script detects malicious JavaScript. It protects your computer from harmful attacks.

Click the link below to download/install this user script.

http://userscripts.org/scripts/show/30284

2.    Post Intercepter

Intercept POST requests and let user modify before submit Click the link below to download/install this user script. http://userscripts.org/scripts/show/743

3.    Web Page Finger Printer

For web2.0 security analysis. To be used with FireBug. For quick analysis, it provides the overall view of the current page contents – javascript, cookies, fuzzable links, form data. For security assessment, it provides recon scan, bruteforce scan, and fuzzing form. In what it differs from the thick-client full-fledged scanner is that this script is tied to the current url page and will not mess with the whole web site. Use it at your own risk. Feel free to send bugs. Click the link below to download/install this user script.

http://userscripts.org/scripts/show/30285

4.    Php Sec info Checker

Check phpinfo page for security and performance issues. Click the link below to download/install this user script. http://userscripts.org/scripts/show/30287
 

User script database

http://www.userscript.org is a website which has a database of a number of user scripts written by coders all over the world. All the user scripts are categorized by tags. So it gets easy to search. You can share your user script by registering to site and make your impact to Greasemonkey world by improving the web.

Be Aware…

Also I want to warn my readers… before installing any user script written by other coder, take a look at code first, check script data (Right click on script file, open with notepad) for trusted sources, check for code which may harm your computer by injecting malicious code (Malware). There may be a possibility that some scripts can keylog your browser data, and harm your privacy. You can read the material provided on Wikipedia, before you start using it. So be careful, be ethical with the tool I introduced to you.

Be Ethical…

As Greasemonkey allows us to write our own user script and allows us to take control of any web page we surf over the network, we should use this technology ethically to make use of Firefox efficiently. After reading this article some people may want to do nasty things which actually are not ethical in any way. But I expect from you all to use Greasemonkey for the purpose it was built and enjoy using it.

Sagar is IT Engineering Student. Currently working as a WEB DEVELOPER.

Leave a Reply