Google Search Maps Fix

Bring Google maps button back

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Google Search Maps Fix
// @namespace    http://tampermonkey.net/
// @version      2.0.1
// @description  Bring Google maps button back
// @author       Ovinomaster
// @include      https://www.google.tld/search*
// @icon         https://www.google.com/images/branding/googleg/1x/googleg_standard_color_128dp.png
// @grant        none
// @license      CC-BY-NC-SA-4.0; https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
// @homepageURL  https://greasyfork.org/it/users/1271103-ovinomaster
// ==/UserScript==
(function() {
  'use strict';

  function addMapsTab() {
    // Target the new navigation container
    const navList = document.querySelector('div.beZ0tf.O1uzAe[role="list"]');
    if (!navList) return;

    // Skip if "Maps" already exists
    if (navList.querySelector('span.R1QWuf')?.textContent === 'Maps' ||
        navList.querySelector('a[data-added-by-userscript="maps"]')) return;

    // Clone an existing tab for structure consistency
    const refItem = navList.querySelector('div[role="listitem"]');
    if (!refItem) return;

    const newItem = refItem.cloneNode(true);
    const newLink = newItem.querySelector('a.C6AK7c');
    const q = new URLSearchParams(window.location.search).get('q') || '';

    if (!newLink) return;

    // Update link
    newLink.href = `https://www.google.com/maps?q=${encodeURIComponent(q)}`;
    // newLink.target = '_blank';
    newLink.setAttribute('data-added-by-userscript', 'maps');

    // Clean up styles & attributes
    newLink.removeAttribute('aria-disabled');
    newLink.removeAttribute('aria-current');
    newLink.removeAttribute('selected');
    newLink.style.color = '';
    newLink.style.textDecoration = '';

    // Replace text
    const label = newLink.querySelector('span.R1QWuf');
    if (label) label.textContent = 'Maps';

    // Insert the new tab after the first one (usually "All")
    navList.insertBefore(newItem, navList.children[1]);
  }

  // function makeMiniMapClickable() {
    // const q = new URLSearchParams(window.location.search).get('q') || '';
    // const mapsLink = `https://www.google.com/maps?q=${encodeURIComponent(q)}`;
    // const mini = document.querySelector('.pyitY, [data-attrid*="map"]');
    // if (mini && !mini.closest('a')) {
      // const a = document.createElement('a');
      // a.href = mapsLink;
      // a.target = '_blank';
      // a.style.display = 'block';
      // a.style.textDecoration = 'none';
      // while (mini.firstChild) a.appendChild(mini.firstChild);
      // mini.appendChild(a);
    // }
  // }

  // Keep trying since Google loads this asynchronously
  const observer = new MutationObserver(() => addMapsTab());
  observer.observe(document.body, { childList: true, subtree: true });

  // Run now and periodically
  addMapsTab();
  makeMiniMapClickable();
  setInterval(addMapsTab, 3000);
})();