Joeviocoe / Google Direct Links Mobile

// ==UserScript==
// @name         Google Direct Links Mobile
// @namespace
// @version      1.0.2
// @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
// @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() {
    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.hasAttribute("oncontextmenu")) {
            link.removeAttribute("oncontextmenu");
        }
        if (link.hasAttribute("ping")) {
            link.removeAttribute("ping");
        }
        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");
        }
        if (link.href.indexOf("url?q=") > -1) {
            link.setAttribute("href", "http" + decodeURIComponent(decodeURIComponent(decodeURIComponent(link.href)).split("http").slice(-1)[0]));
            link.setAttribute("target", "_blank");
        }
    }
}

function run() {
    links();
}

setTimeout( function() {
    window.addEventListener('click', function() {
        if ( counter === 0 ) {
            setTimeout (function() { run(); counter = 0; }, 500);
        }
        counter++;
    });
    window.addEventListener('scroll', function() {
        if ( counter === 0 ) {
            setTimeout (function() { run(); counter = 0; }, 500);
        }
        counter++;
    });

    var counter = 0;
    run();
},2000);