// ==UserScript==
// @name         Google Search Results Map Link
// @namespace    http://tampermonkey.net/
// @version      2024-03-20
// @description  Bring google maps button back
// @author       pdcmoreira
// @match        https://www.google.com/*
// @match        https://google.com/*
// @icon         https://www.google.com/
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    function addMapsButton() {
        // Find the existing results tabs container (Images, News, etc.)
        const tabsContainer = document.querySelectorAll('[role="navigation"]')[2].querySelectorAll('&>div>div>div>div>div>div')[0];

        // Give up if the container wasn't found
        if (!tabsContainer) {
            return
        }

        // Sibling link (to steal stuff from)

        const siblingLink = tabsContainer.querySelector('a');

        // Maps button

        const mapsButton = document.createElement('a');

        mapsButton.classList = siblingLink.classList;

        const searchQuery = new URLSearchParams(window.location.search).get('q');

        mapsButton.href = `http://maps.google.com/maps?q=${searchQuery}`;

        // Button content (text)

        const buttonTextContainer = document.createElement('div');

        buttonTextContainer.innerText = 'Maps';

        buttonTextContainer.classList = siblingLink.childNodes[0].classList;

        mapsButton.append(buttonTextContainer);

        // Button container

        const buttonContainer = document.createElement('div');

        buttonContainer.classList = siblingLink.parentNode.classList;

        buttonContainer.append(mapsButton);

        // Insert the Maps button at the beginning of the tabs container
        tabsContainer.insertBefore(buttonContainer, tabsContainer.childNodes[1]);
    }

    // Call the function to add the button
    addMapsButton();
})();