Joeviocoe / Google Direct Links

// ==UserScript==
// @name         Google Direct Links
// @namespace
// @version      2.0.3
// @description  Skip redirection URL on Google search results.
// @include      *www.google.com/*
// @include      *mail.google.com/*
// @include      *encrypted.google.com/*
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js
// @updateURL    https://openuserjs.org/install/Joeviocoe/Google_Direct_Links.user.js
// @downloadURL  https://openuserjs.org/install/Joeviocoe/Google_Direct_Links.user.js
// @icon         https://www.google.com/s2/favicons?domain=google.com
// @copyright    2018+, Joeviocoe
// @license      MIT
// @noframes
// ==/UserScript==
// Copyright (c) 2018, Joeviocoe
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
//   this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
//   this list of conditions and the following disclaimer in the documentation
//   and/or other materials provided with the distribution.
// * Neither the name of Joeviocoe nor the names of its contributors
//   may be used to endorse or promote products derived from this software
//   without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
/*jslint browser: true*/
/*global $, jQuery, alert*/

if (window.location.href.indexOf("encrypted.google.com") > -1) {
    window.location.href = window.location.href.replace('encrypted','www');
}

var counter = 0;

function links() {
    console.log("Google Direct Links:  Removing redirects on " + document.getElementsByTagName("a").length + " URLs");
    var links = document.getElementsByTagName("a");
    for ( var i = 0; i < links.length; i++ ) {
        var link = links[i];
        if (link.hasAttribute("data-saferedirecturl")) {
            link.removeAttribute("data-saferedirecturl");
        }
        if (link.hasAttribute("onmousedown")) {
            link.removeAttribute("onmousedown");
            if (link.removeEventListener) {
                link.removeEventListener("mousedown", link.onmousedown, false);
            } else if(link.detachEvent) {
                link.detachEvent("onmousedown", link.onmousedown);
            }
        }
        if (link.href.indexOf("&sa=") > -1) {
            link.setAttribute("href", decodeURIComponent(decodeURIComponent(decodeURIComponent(link.href)).split("&sa=")[0]));
        }
        if (link.href.indexOf("aclk?sa=") > -1) {
            link.setAttribute("href", "http" + decodeURIComponent(decodeURIComponent(decodeURIComponent(link.href)).split("http").slice(-1)[0].split("&ctype=")[0]));
            link.setAttribute("target", "_blank");
        }
        if (link.href.indexOf("s_dest_url=") > -1) {
            link.setAttribute("href", "http" + decodeURIComponent(decodeURIComponent(decodeURIComponent(link.href)).split("http").slice(-1)[0]));
            link.setAttribute("target", "_blank");
        }
        if (link.href.indexOf("url=q?") > -1) {
            link.setAttribute("href", "http" + decodeURIComponent(decodeURIComponent(decodeURIComponent(link.href)).split("http").slice(-1)[0].split("&sa=")[0]));
            link.setAttribute("target", "_blank");
        }
    }
}

function images() {
    $('span:contains("Images may be")').remove();
    $('html').contents().find('a[href][data-ved]').children('img[onload][src]').each(function(){
        var url = $(this).attr('src');
        $(this).parent('a[href][data-ved]').attr('href',url).removeAttr('ping').removeAttr('data-ved');
        if ( url.indexOf("base64") < 0 ) {
            console.log("Google Direct Links:  Removing redirects on Image - " + url);
        }
    });
}

$('*').on('click', function (e) {
    if ( counter === 0 ) {
        setTimeout (function() { links(); counter = 0; }, 1000);
    }
    counter++;
});

$('input').keydown( function(e) {
    var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
    if(key == 13) {
        setTimeout (function() { links(); counter = 0; }, 1000);
    }
});

if (window.location.href.indexOf("tbm=isch") > -1) {
    images();
    $('*').on('mousemove', function (e) {
        if ( counter === 0 ) {
            setTimeout (function() { images(); counter = 0; }, 1000);
        }
        counter++;
    });
}

links();
setTimeout (function() { links(); }, 2000);