|
// ==UserScript== |
|
// @name Google maps addon |
|
// @namespace http://tampermonkey.net/ |
|
// @version 2024-03-21 |
|
// @description Bring google maps button back |
|
// @author Daan Grashoff |
|
// @homepage https://gist.github.com/Daan-Grashoff/57c40bc355bcb4d1ebc1290094f57ddf |
|
// @match *://www.google.com/search* |
|
// @match *://google.com/search* |
|
// @match *://www.google.co.uk/search* |
|
// @match *://www.google.nl/search* |
|
// @match *://www.google.de/search* |
|
// @match *://www.google.fr/search* |
|
// @match *://www.google.tld/search* |
|
// @run-at document-start |
|
// @icon https://www.google.com/images/branding/googleg/1x/googleg_standard_color_128dp.png |
|
// @grant none |
|
// ==/UserScript== |
|
|
|
(function() { |
|
'use strict'; |
|
|
|
const DEBUG = true; |
|
function log(...args) { |
|
if (DEBUG) console.log('[Maps Button]', ...args); |
|
} |
|
|
|
function addMapsButton() { |
|
log('Attempting to add Maps button...'); |
|
|
|
// Find the navigation container using multiple possible selectors |
|
const possibleContainers = [ |
|
document.querySelector('div[role="navigation"] div[role="list"]'), |
|
document.querySelector('.MUFPAc'), |
|
document.querySelector('.nfdoRb'), |
|
document.querySelector('.T47uwc'), |
|
document.querySelector('.NZmxZe'), |
|
document.querySelector('.crJ18e') |
|
]; |
|
|
|
const tabsContainer = possibleContainers.find(el => el !== null); |
|
if (!tabsContainer) { |
|
log('No navigation container found'); |
|
return; |
|
} |
|
|
|
// Check if Maps button already exists |
|
if (tabsContainer.querySelector('.maps-button-custom')) { |
|
log('Maps button already exists'); |
|
return; |
|
} |
|
|
|
// Get the search query |
|
const searchQuery = new URLSearchParams(window.location.search).get('q'); |
|
if (!searchQuery) { |
|
log('No search query found'); |
|
return; |
|
} |
|
|
|
// Create Maps button |
|
const mapsListItem = document.createElement('div'); |
|
mapsListItem.setAttribute('role', 'listitem'); |
|
mapsListItem.classList.add('maps-button-custom'); |
|
|
|
const mapsButton = document.createElement('a'); |
|
mapsButton.classList.add('nPDzT'); |
|
mapsButton.classList.add('T3FoJb'); |
|
mapsButton.href = `https://www.google.com/maps/search/${encodeURIComponent(searchQuery)}`; |
|
|
|
const mapsButtonText = document.createElement('div'); |
|
mapsButtonText.classList.add('YmvwI'); |
|
mapsButtonText.textContent = 'Maps'; |
|
mapsButton.appendChild(mapsButtonText); |
|
mapsListItem.appendChild(mapsButton); |
|
|
|
// Find the best insertion point |
|
const allTab = tabsContainer.querySelector('[aria-current="page"]')?.closest('[role="listitem"]') || |
|
tabsContainer.querySelector('[role="listitem"]'); |
|
|
|
if (allTab?.nextSibling) { |
|
tabsContainer.insertBefore(mapsListItem, allTab.nextSibling); |
|
} else { |
|
tabsContainer.appendChild(mapsListItem); |
|
} |
|
|
|
log('Maps button added successfully'); |
|
} |
|
|
|
// Initial load |
|
if (document.readyState === 'loading') { |
|
document.addEventListener('DOMContentLoaded', addMapsButton); |
|
} else { |
|
addMapsButton(); |
|
} |
|
|
|
// Watch for URL changes (Google's dynamic navigation) |
|
let lastUrl = location.href; |
|
const observer = new MutationObserver(() => { |
|
const currentUrl = location.href; |
|
if (currentUrl !== lastUrl) { |
|
lastUrl = currentUrl; |
|
setTimeout(addMapsButton, 100); |
|
} |
|
}); |
|
|
|
observer.observe(document, { subtree: true, childList: true }); |
|
|
|
// Periodic check to ensure button stays present |
|
setInterval(() => { |
|
const hasButton = document.querySelector('.maps-button-custom'); |
|
if (!hasButton) { |
|
log('Periodic check - attempting to add button'); |
|
addMapsButton(); |
|
} |
|
}, 2000); |
|
})(); |
@Daan-Grashoff For the readme with "click here to install" doesn't work. But if you open the js file as raw, then copy that url, that will work to show it as raw for copy paste. If you find out how to make it actually one click auto install to tampermonkey, do let me know, as i borrowed your code to not add, but remove the short videos tab as it annoyed me that i had GPT cook up.