What actually Greasemonkey is?
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.
- And lot more…
How does Greasemonkey power user scripts?
This deletes a value from chrome that was previously set.
A function intended to get stored values, see GM_setValue below.
This API method retrieves an array of preference names that start with the branch's root.
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.
Given a defined resource, this method returns it as a string..
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
A function, that takes one parameter, a string of CSS to apply to the page.
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.
A function that accepts a parameter which will be routed to the Error Console, useful for examining values when writing a script.
Similar in spirit to window.open(), this function accepts a single parameter, the URL of a page to open in a new tab.
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.
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?
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
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
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
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.
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
Click the link below to download/install this user script.
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
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.
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.
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.