Add Google Maps Link to Google Search Navigation

Adds a "Maps" link to Google Search navigation, allowing users to directly open their search query in Google Maps.

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

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

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==UserScript==
// @name         Add Google Maps Link to Google Search Navigation
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Adds a "Maps" link to Google Search navigation, allowing users to directly open their search query in Google Maps.
// @author       Nieme
// @include      https://www.google.*/search*
// @grant        none
// @license      GPLv2
// @run-at       document-idle
// ==/UserScript==


(function() {
    'use strict';

    const translations = {
        en: 'Maps',
        de: 'Maps',
        fr: 'Maps',
        es: 'Mapa',
        it: 'Mappa',
        nl: 'Kaarten',
        pt: 'Mapa',
        ru: 'Карты',
        ja: '地図',
        ko: '지도',
        zh: '地图'
    };

    function getLanguage() {
        const urlParams = new URLSearchParams(window.location.search);
        const langParam = urlParams.get('hl');

        if (langParam) {
            return langParam.split('-')[0];
        }

        return document.documentElement.lang || 'en';
    }

    function addMapLinkToNavigation() {
        const navigationContainer = document.querySelector("div[role='list']");
        if (!navigationContainer) {
            console.log("Userscript Google Maps Error: Navigation container not found. Contact me on Discord: Nieme");
            return;
        }

        if (navigationContainer.querySelector(".custom-map-link")) {
            console.log("Userscript Google Maps Error: Map link already added. Contact me on Discord: Nieme");
            return;
        }

        const queryElement = document.querySelector("input[name='q']");
        if (!queryElement || !queryElement.value) {
            console.log("Userscript Google Maps Error: Query element not found. Contact me on Discord: Nieme");
            return;
        }

        const query = queryElement.value;

        const currentTLD = window.location.hostname.split('.').slice(-1)[0];
        const mapsDomain = `https://www.google.${currentTLD}/maps/search/?api=1&query=${encodeURIComponent(query)}`;

        const language = getLanguage();
        const linkText = translations[language] || translations['en'];

        const newListItem = document.createElement('div');
        newListItem.setAttribute('role', 'listitem');

        const newLink = document.createElement('a');
        newLink.href = mapsDomain;
        newLink.className = 'LatpMc nPDzT T3FoJb custom-map-link';
        newLink.setAttribute('role', 'link');
        newLink.innerHTML = `<div jsname="bVqjv" class="YmvwI">${linkText}</div>`;

        newListItem.appendChild(newLink);

        // Finde das "Videos" Listenelement, damit wir den neuen Link dahinter einfügen können
        const videosLink = navigationContainer.querySelector("a[href*='tbm=vid']");
        if (videosLink && videosLink.parentElement) {
            videosLink.parentElement.insertAdjacentElement('afterend', newListItem);
        } else {
            // Fallback: Falls "Videos" nicht gefunden wird, füge den Link am Ende hinzu
            navigationContainer.appendChild(newListItem);
        }
    }

    const observer = new MutationObserver(() => {
        addMapLinkToNavigation();
    });

    observer.observe(document.body, { childList: true, subtree: true });

    window.addEventListener('load', () => {
        setTimeout(() => {
            addMapLinkToNavigation();
        }, 1000);
    });
})();