tvkanters / Google Images link fixer

// ==UserScript==
// @name        Google Images link fixer
// @namespace   http://tvkdevelopment.com
// @author      tvkanters
// @description Keeps Google's Images link at the second spot and the Videos link at the third spot.
// @include     /^[a-z]+:\/\/([^.]+.)?google.[a-z]+/
// @version     7
// @grant       none
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.js
// ==/UserScript==

var onloadCount = 0;
var keyUpFixed = false;
$(function() {
    
    // onload is called 3 times for some reason
    if (++onloadCount > 1) {
        return;
    }
    
    function fixClasses(link) {
        if (!link.hasClass("hdtb-mitem")) {
            link.removeClass();
            link.addClass("hdtb-mitem hdtb-imb");
        }
    }
    
    function fixImagesLink() {
        var fixed = 0;
        var interval = setInterval(function() {
            // Makes sure the links are fixed after changing query
            if (!keyUpFixed) {
                var inputFields = $("#lst-ib");
                if (inputFields.length > 0) {
                    keyUpFixed = true;
                    inputFields.keyup(function() {
                        fixImagesLink();
                    });
                }
            }
            
            var links = $("#hdtb-msb");
            
            // Ignore if the links couldn't be found
            if (!links.length) {
                safeGuard();
                return;
            }
            
            var imagesLink = links.find("a[href*='tbm=isch']").parent();
            var videosLink = links.find("a[href*='tbm=vid']").parent();
            
            if (!imagesLink.length) {
                imagesLink = links.find(".hdtb-mitem:contains(Images)");
            }
            if (!videosLink.length) {
                videosLink = links.find(".hdtb-mitem:contains(Videos)");
            }
            
            // Ignore when the links are still on the right spot
            if (links.children().index(imagesLink) == 1 && links.children().index(videosLink) == 2) {
                safeGuard();
                return;
            }
            
            fixClasses(imagesLink);
            fixClasses(videosLink);
            
            links = imagesLink.parent();
            
            // Move the links
            imagesLink.insertAfter(links.children(":first"));
            videosLink.insertAfter(imagesLink);
            
            // After fixing the links twice, we're done
            if (++fixed == 2) {
                clearInterval(interval);
                return;
            }
            
            // Make sure we stop checking in case Google changes something
            safeGuard();
        }, 50);
        
        var safeGuardCount = 0;
        var safeGuardLimit = 50;
        function safeGuard() {
            if (++safeGuardCount > safeGuardLimit) {
                clearInterval(interval);
            }
        }
    }
    fixImagesLink();
        
});