Having Fun With JavaScript And GreaseMonkey

Posted by Ahmed Tarek Hasan on 3/15/2013 03:15:00 PM with 1 comment

Having Fun With JavaScript And GreaseMonkey

This time I am going to tell you about a great Firefox extension/addon called GreaseMonkey.

I think everyone of us has faced the situation when he found that some website is missing something which could have been done by just a bit of JavaScript. But, unfortunately we can do nothing regarding this except contacting the website author to try to convince him that this change is really good and he should do it.

I can now tell you that you don't have to go through all this hustle because you now have GreaseMonkey which provides you with the ability to run JavaScript across certain websites. It is like injecting the JavaScript you need into a website but for sure at your side only, not in the source website itself.

Using GreaseMonkey you can perform so many cool things. There are so many free scripts others wrote which you can search and use as you wish. You can find these scripts here or here


Me myself have wrote some scripts which made my life easier. Some alter web pages UI, others do some business depending on some rules, ...... but the most powerful ones which I really love are the links elongators.

As you know there are some services for shortening links like "1Tool", "TakeMyFile" and many otheres. These services takes a long URL and returns back to you a short one which you can share and post anywhere you wish. The only annoying thing regarding this is that when you use the short link you will be directed to a page with a time counter or ads or some annoying thing till you are finally re-directed to the main URL. This really made me mad.

This could be somehow acceptable when you just need to browse a certain URL, but what about a bunch of them. We all know about forums and how we can find in one thread an attachment which is so big in size that it is divided into a huge number of part files uploaded to some online hosting service like RapidShare or whatever. In this case, lets say that number of links is 30, will you click on each one of these 30 links to be redirected to some annoying page -each time of the 30 times- and wait for some counter and finally get your original link??!!!

This encouraged me to write my own GreaseMonkey scripts to undo what the shortening services already done by getting the original links and replacing the shortened ones in the page with their corresponding original ones.

This is just a sample of what you can do with GreaseMonkey and that's why I really encourage you give it a try, you will love it.

To know what you can do with GreaseMonkey, you can have a look on the code sample below
// ==UserScript==

// @name          1Tool Short Links Elongator

// @namespace     DevelopmentSimplyPut(developmentsimplyput.blogspot.com)

// @description   Elongates all 1Tool short links to their direct links

// @include       *

// ==/UserScript==

String.prototype.ReplaceAll = function(stringToFind,stringToReplace){
    var temp = this;
    var index = temp.indexOf(stringToFind);

        while(index != -1){

            temp = temp.replace(stringToFind,stringToReplace);
            index = temp.indexOf(stringToFind);
        }
        return temp;
    }

Array.prototype.unique = function () {
 var r = new Array();
 o:for(var i = 0, n = this.length; i < n; i++)
 {
  for(var x = 0, y = r.length; x < y; x++)
  {
   if(r[x]==this[i])
   {
    continue o;
   }
  }
  r[r.length] = this[i];
 }
 return r;
}

function Run(Urls)
{
 if(Urls.length>0)
 {
  for(i=0;i<Urls.length;i++)
   GetDirectLink(Urls[i]);
 }
}

function GetDirectLink(str)
{
 GM_xmlhttpRequest(
       {
        method: "GET",
        url: 'http://www.yahoo.com/.php?id=' + str.replace('http://1tool.biz/',""),
        headers:{'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey', 'Content-type':'application/x-www-form-urlencoded'},
        onload:function(result)
        {
         var parts1=new Array();
         var parts2=new Array();
         parts1=result.responseText.split('onclick="NewWindow(');
         parts2=parts1[1].split("'");
         result=parts2[1];
         (document.getElementsByTagName("body"))[0].innerHTML=(document.getElementsByTagName("body"))[0].innerHTML.ReplaceAll(str,result);
        }
       }
      );
}

var DirectUrls=new Array();
var Urls=new Array();
var UrlsPattern=/http:\/\/1tool\.biz\/(?:\w*)/g;
Urls=(document.getElementsByTagName("body"))[0].innerHTML.match(UrlsPattern);

Urls=Urls.unique();
Run(Urls);

That's it. This is just a scratch on the surface but not everything. You can check it with yourself and find what you can do with GreaseMonkey.

At last, you can have a look on my scripts here or here and for sure your feedback is so welcomed.


Resources & Links
  1. Greasespot
  2. Greasemonkey :: Add-ons for Firefox
  3. Userscripts.org: Power-ups for your browser
  4. AhmedTarekHasan's Scripts on GreasyFork.org
  5. AhmedTarekHasan's Scripts on Openuserjs.org

Categories: