Skip to content

Instantly share code, notes, and snippets.

@Delivator
Forked from Daan-Grashoff/google_search_maps_addon.js
Last active November 18, 2024 15:24
Show Gist options
  • Save Delivator/cbb71fc770b11b02b4269f26d65ce145 to your computer and use it in GitHub Desktop.
Save Delivator/cbb71fc770b11b02b4269f26d65ce145 to your computer and use it in GitHub Desktop.
Bring back the google maps button and make map image clickable when searching on google
// ==UserScript==
// @name Google Search Maps Fix
// @namespace http://tampermonkey.net/
// @version 2024-06-05
// @description Bring Google maps button back (originally based off of Daan Grashoff's script)
// @author Delivator
// @match https://www.google.com/search*
// @include https://www.google.tld/search*
// @icon https://www.google.com/images/branding/googleg/1x/googleg_standard_color_128dp.png
// @grant none
// ==/UserScript==
(function() {
'use strict';
function addMapsLink() {
// Get the search query from the URL
const searchQuery = new URLSearchParams(window.location.search).get('q');
// Construct the Maps link with the query
const mapsLink = `${window.location.origin}/maps?q=${searchQuery}`;
// Select the menu container and map
const menuContainer = document.querySelector('.crJ18e > div[role="list"]');
const mapImage = document.querySelector('#lu_map, #dimg_95');
if (menuContainer) {
// Create a new list item div
const listItem = document.createElement('div');
listItem.setAttribute('role', 'listitem');
// Create the link element
const link = document.createElement('a');
link.href = mapsLink;
link.className = 'nPDzT T3FoJb';
link.setAttribute('jsname', 'VIftV');
link.setAttribute('role', 'link');
// Create the inner text div
const innerTextDiv = document.createElement('div');
innerTextDiv.setAttribute('jsname', 'bVqjv');
innerTextDiv.className = 'YmvwI';
innerTextDiv.textContent = 'Maps';
// Append the inner text div to the link
link.appendChild(innerTextDiv);
// Append the link to the list item
listItem.appendChild(link);
// Insert the new list item as the second-to-last element
if (menuContainer.children.length > 1) {
menuContainer.insertBefore(listItem, menuContainer.children[menuContainer.children.length - 1]);
} else {
menuContainer.appendChild(listItem);
}
}
if (mapImage) {
// Create a new anchor element for map element
const anchorElement = document.createElement('a');
anchorElement.href = mapsLink;
mapImage.parentNode.insertBefore(anchorElement, mapImage);
anchorElement.appendChild(mapImage);
}
}
// Run the function to add the Maps link when the page loads
window.addEventListener('load', addMapsLink);
})();
@Delivator
Copy link
Author

The inconsistency is so frustrating. Sometimes google uses static maps, sometimes dynamic maps and sometimes no maps. And sometimes the tab looks completely different and I would need an exception for every single case.

@MoridinBG
Copy link

Yeah, Google are not making it easy. I ended up with

        // Find the existing results tabs (Images, News, etc.)
        const linksContainer = document.querySelector('.crJ18e');
        const tabsContainer = document.querySelector('.IUOThf');
        
        // The map sometimes is a thumbnail on the right and sometimes a larger image on top of the results
        var mapImage = document.querySelector('.Lx2b0d');
        if (mapImage == null) {
            mapImage = document.querySelector('.dirs');
        }

Which seems to handle all cases I have encountered so far.

@zipel
Copy link

zipel commented Apr 12, 2024

Thank you so much for this script! It is working for addresses (both the tab and the map image) but for some reason, it does not work for cities. For example when I search for "Copenhagen", the tab does not appear

Hmm can't reproduce it on my side. Can you send me a link?

It's fine, I found a workaround for that. Basically when I was running one-word plain searches like "Denmark" or "Copenhagen", there wasn't any map button appearing. I have made it a habit now to end all of my searches regarding places with the word map, so I am typing "USA map" instead of just "USA" and it works like a charm, I have both the button "Maps" on top and the clickable map! Thanks once again for the great script!

@BlackThornCraft
Copy link

does this script work for you all anymore?

@Xornop
Copy link

Xornop commented Jun 19, 2024

been using a mash of the codes of different authors (@Daan-Grashoff, @Delivator, @MoridinBG) sprinkled with some chatgpt cause i dont know js at all, been broken for a couple weeks now. Hope they can tell me how to fix it :p

Code:
(function () {

    'use strict';

    function addMapsLink() {
        // Find the existing results tabs (Images, News, etc.)
        const linksContainer = document.querySelector('.crJ18e');
        const tabsContainer = document.querySelector('.IUOThf');

        // The map sometimes is a thumbnail on the right and sometimes a larger image on top of the results
        var mapImage = document.querySelector('.Lx2b0d');
        if (mapImage == null) {
            mapImage = document.querySelector('.dirs');
        }

        // Get the search query from the URL
        const searchQuery = new URLSearchParams(window.location.search).get('q');

        // Construct the Maps link with the query
        const mapsLink = `${window.location.origin}/maps?q=${searchQuery}`;

        // If map image exists
        if (mapImage) {
            const anchorElement = document.createElement('a');
            anchorElement.href = mapsLink;
            mapImage.parentNode.insertBefore(anchorElement, mapImage);
            anchorElement.appendChild(mapImage);
        }

        // If links exist, proceed
        if (linksContainer) {
            // Create the Maps button
            const mapsButtonL = document.createElement('a');
            mapsButtonL.classList.add('nPDzT', 'T3FoJb');  // Style to match other tabs

            // Create the inner elements for the Maps button
            const mapDivL = document.createElement('div');
            mapDivL.jsname = 'bVqjv';
            mapDivL.classList.add('YmvwI');    //GKS7s

            const mapSpanL = document.createElement('span');
            mapSpanL.classList.add('FMKtTb', 'UqcIvb');
            mapSpanL.jsname = 'pIvPIe';
            mapSpanL.textContent = 'Maps';

            // Assemble the elements
            mapDivL.appendChild(mapSpanL);
            mapsButtonL.appendChild(mapDivL);
            mapsButtonL.href = mapsLink;

            // Insert the Maps button at the beginning of the tabs container
            linksContainer.prepend(mapsButtonL);
        }

        // If tabs exist, proceed
        if (tabsContainer) {
            // Create the Maps button
            const mapsButtonT = document.createElement('a');
            mapsButtonT.classList.add('nPDzT', 'T3FoJb');  // Style to match other tabs

            // Create the inner elements for the Maps button
            const mapDivT = document.createElement('div');
            mapDivT.jsname = 'bVqjv';
            mapDivT.classList.add('GKS7s');

            const mapSpanT = document.createElement('span');
            mapSpanT.classList.add('FMKtTb', 'UqcIvb');
            mapSpanT.jsname = 'pIvPIe';
            mapSpanT.textContent = 'Maps';

            // Assemble the elements
            mapDivT.appendChild(mapSpanT);
            mapsButtonT.appendChild(mapDivT);
            mapsButtonT.href = mapsLink;

            // Insert the Maps button at the beginning of the tabs container
            tabsContainer.prepend(mapsButtonT);
        }

    }

    // Call the function to add the button
    addMapsLink();

})();

@Delivator
Copy link
Author

hi @Xornop , can you tell me what exactly is broken? For me it still works that the "Maps" tab is added but clicking on the map sometimes doesnt work.

@Xornop
Copy link

Xornop commented Jun 21, 2024

@Delivator yeah same for me, thats my issue. I dont use the maps tab, just in cases where it doesnt give me a minimap when i think it should have

@Xornop
Copy link

Xornop commented Sep 22, 2024

Actually i realized my code works still, probably a PEBKAC error on my end

@Delivator
Copy link
Author

Anyone finding this, check out this script: https://github.com/mimoklef/Search-Google-Maps-Back

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment