Google Search Maps Buttons

Adds maps buttons to Google Search and filters out -site:pinterest.* (modified from Daan Grashoff / Delivator)

您需要先安裝使用者腳本管理器擴展,如 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 Buttons
// @description  Adds maps buttons to Google Search and filters out -site:pinterest.* (modified from Daan Grashoff / Delivator)
// @version      0.1.1
// @author       mseitz
// @namespace    https://greasyfork.org/en/scripts/490916-google-search-maps-buttons
// @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/en/scripts/490916-google-search-maps-buttons
// ==/UserScript==


(function () {
    'use strict';

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

        // Get the search query from the URL
        const searchQuery = new URLSearchParams(window.location.search).get('q').replace("-site:pinterest.*", '');

        // 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();

})();