VitorMM / Google View Image

// ==UserScript==
// @name         Google View Image
// @namespace    http://GoogleViewImage.com/
// @version      0.3
// @description  An updated version of laidbackTempo's "Bring back Google View Image!"
// @author       laidbackTempo & VitorMM
// @include      http*://www.google.*.*/*
// @include      http*://google.*.*/*
// @include      http*://www.google.*/*
// @include      http*://google.*/*
// @license      MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';


function userLanguage() {
    var url = new URL(window.location.href);

    var lang = url.searchParams.get("hl");
    if (lang !== null) return lang;

    var html = document.getElementsByTagName('html')[0];
    lang = html.getAttribute('lang');
    if (lang !== null) return lang;

    var hostComponents = url.host.split('.');
    var tempLang = hostComponents[hostComponents.length-1];
    if (tempLang === "br") return "pt-BR";

    return "en-US";
}
function localiseSearchByImage(obj, user_language) {
    var text = 'Search by image';
    if (user_language === "pt-BR") text = 'Pesquisar por imagem';
    obj.innerHTML = text;
}
function localiseViewImage(obj, user_language) {
    var text = 'View Image';
    if (user_language === "pt-BR") text = 'Visualizar imagem';
    obj.innerHTML = text;
}

function addLinks(node) {
    var user_language = userLanguage();
    var viewImageButtonWidth = '150px';
    var searchByImageButtonWidth = '150px';
    var extClass = "tampermonkey_google_view_image_script_button";
    if (node.nodeType === Node.ELEMENT_NODE) {
        if ((node.classList.contains('irc_ris')) || (node.classList.contains('irc_mi') || (node.classList.contains('irc_tas')))) {
            var object = node.closest('.irc_c');

            // Retrive image links, and image url
            var imageLinks = object.querySelector('.irc_but_r > tbody > tr');

            // Retrive the image URL
            var imageURL;

            var imageArea = object.querySelector('.irc_t');
            var thumbnail = imageArea.querySelector('img[name="' + object.dataset.itemId + '"]');
            if (thumbnail) {
                var meta = thumbnail.closest('.rg_bx').querySelector('.rg_meta');
                var metadata = JSON.parse(meta.innerHTML);
                imageURL = metadata.ou;
            } else {
                imageURL = imageArea.getElementsByClassName('irc_mi')[0].src;
            }

            // Create Search by image button
            var searchByImageTd = document.createElement('td');
            searchByImageTd.setAttribute('class', extClass);

            if (imageURL) {
                // Create Search by image link
                var searchByImageLink = document.createElement('a');
                searchByImageLink.style.width=searchByImageButtonWidth;
                searchByImageLink.setAttribute('href', '/searchbyimage?&image_url=' + imageURL);
                searchByImageTd.appendChild(searchByImageLink);

                // Insert text into Search by image button
                var searchByImageText = document.createElement('span')
                localiseSearchByImage(searchByImageText, user_language);
                searchByImageLink.appendChild(searchByImageText);
            }


            // Create View image button
            var viewImageTd = document.createElement('td');
            viewImageTd.setAttribute('class', extClass);

            if (imageURL) {
                // Create View image link
                var viewImageLink = document.createElement('a');
                viewImageLink.style.width=viewImageButtonWidth;
                viewImageLink.setAttribute('href', imageURL);
                viewImageLink.setAttribute('target', '_blank');
                viewImageTd.appendChild(viewImageLink);

                // Insert text into View image button
                var viewImageText = document.createElement('span')
                localiseViewImage(viewImageText, user_language);
                viewImageLink.appendChild(viewImageText);
            }

            imageLinks.innerHTML = viewImageTd.outerHTML + searchByImageTd.outerHTML;
        }
    }
}

    var observer = new MutationObserver(function (mutations) {
        mutations.forEach((mutation) => {
            if (mutation.addedNodes && mutation.addedNodes.length > 0) {
                for (var i = 0; i < mutation.addedNodes.length; i++) {
                    var newNode = mutation.addedNodes[i];
                    addLinks(newNode);
                }
            }
        });
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    addLinks(document.body);

})();