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);
})();
@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