firefoxmobilegreasemonkey

How to mobilize all links, in a browsed page, using Google Mobilizer?


I'm trying to make the links on some of my pages "mobilized" with this great service by Google Mobilizer.

EG: change URLs like:

http://imgur.com/ 

to:

http://www.google.com/gwt/x?u=imgur.com

Google Mobilizer reduces the size of whatever URL is passed to it greatly. Images get reduced in size. Only the first frame of animated gifs are rendered, and basically any resource is heavily reduced in size. This decreases the download time greatly on low-bandwidth and high latency connections.

Could this be done in Firefox by an add-on or a userscript? So that all the links on a webpage are prefixed by www.google.com/gwt/x?u=?

There's a Chrome extension which actually gives you a context-menu item to do it.


Solution

  • This works for me. It replaces all links on a page with Google Mobilizer URL appended to them.

    if (!(
    // Except when on ...
                (/google.com\/search/.test(document.URL))
            ||  (/google.*\/gwt/.test(document.URL))
        )){     
        var link = document.body.getElementsByTagName("a");
        for (var i = 0; i < link.length; i++) {
            if (!(
    //Only for external links
                        (link[i].hostname == window.location.hostname)
    //And except for links that contain... 
                    ||  (/google\./.test(link[i].href))
                    ||  (/stack/.test(link[i].href))
                    ||  (/reddit\./.test(link[i].href))
    //And except for Null & Javascript-purpose links...
                    ||  (/\0/.test(link[i].href))
                )){
    //Mobilize all links (append mobilizer URL)
                link[i].href = link[i].href.replace("http","http://google.com/gwt/x?u=http")
                }
            }
        }
    else if (/google.*\/gwt/.test(document.URL)){
    // But when on Google Mobilizer site itself, ...
        var link = document.body.getElementsByTagName("a")
        for (var i = 0; i < link.length; i++) {
            if ((/gwt\/x\/e\?/.test(link[i].href))) {
    //De-mobilize (just) "View page directly" link (because it unnecessarily prompts for redirection)
                var inner = link[i].href.indexOf('http',1);
                inner = inner>-1 ? link[i].href.substr(inner,link[i].href.indexOf('&',inner)-inner) : false;
                if (inner)
                    link[i].href = inner;
                }
            }
        }
    

    It also checks to see if the links are of the parent site domain, and doesn't change them.