DuckDuckGo Redirect to Google Maps

Redirects the 'Maps' button on DuckDuckGo search results to Google Maps instead of Apple Maps.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         DuckDuckGo Redirect to Google Maps
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Redirects the 'Maps' button on DuckDuckGo search results to Google Maps instead of Apple Maps.
// @author       nereids
// @license      MIT
// @match        *://duckduckgo.com/*
// @grant        none
// @icon         https://icons.duckduckgo.com/ip3/duckduckgo.com.ico
// @run-at       document-idle
// ==/UserScript==

(function() {
    'use strict';

    const GOOGLE_MAPS_BASE_URL = 'https://www.google.com/maps/search/';

    function setupMapsLinkListener() {
        // Find the 'Maps' link element using the provided structure and text content.
        // We look for an <a> tag that contains 'Maps' as its text.
        const mapsLink = Array.from(document.querySelectorAll('li a'))
                             .find(a => a.textContent.trim() === 'Maps');

        if (mapsLink) {
            // **Core Correction:** We attach an event listener to intercept the click.
            mapsLink.addEventListener('click', function(event) {
                // 1. Stop the default behavior (which is DDG's internal navigation/SPA logic).
                event.preventDefault();
                event.stopPropagation(); // Also stop propagation in case parent elements have handlers.

                // 2. Extract the search query from the current DDG URL.
                // The current URL in the browser bar is the source of the query.
                const currentUrl = new URL(window.location.href);
                const query = currentUrl.searchParams.get('q');

                if (query) {
                    // 3. Construct the Google Maps URL and perform the redirect in a new tab.
                    const googleMapsUrl = GOOGLE_MAPS_BASE_URL + encodeURIComponent(query);
                    window.open(googleMapsUrl, '_blank');

                    console.log(`Redirected 'Maps' click for query: ${query} to Google Maps.`);
                } else {
                    console.warn('Could not extract search query from current URL for redirect.');
                }
            }, true); // Use 'true' for capture phase to ensure our handler runs before any other DDG handlers.

            // For a better visual experience, you can still update the href attribute
            // to display the target URL in the status bar on hover.
            mapsLink.href = GOOGLE_MAPS_BASE_URL + (new URL(window.location.href)).searchParams.get('q') || '';
            mapsLink.target = '_blank';

            console.log('DuckDuckGo Maps link click interceptor is active.');
            return true; // Link found and listener set up
        }
        return false; // Link not found
    }

    // Use MutationObserver to reliably wait for the dynamic navigation bar to load.
    const observer = new MutationObserver((mutationsList, observer) => {
        if (setupMapsLinkListener()) {
            observer.disconnect(); // Disconnect once successful
        }
    });

    // Start observing the document body for changes in the subtree.
    observer.observe(document.body, { childList: true, subtree: true });

    // Also try to run immediately in case the element is already present on page load.
    setupMapsLinkListener();
})();