Google Gemini - Direct Links

Removes the google.com/search?q= redirection wrapper from links generated by Google Gemini, allowing you to open external websites directly.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Google Gemini - Direct Links
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Removes the google.com/search?q= redirection wrapper from links generated by Google Gemini, allowing you to open external websites directly.
// @author       YourUsername
// @license      MIT
// @match        https://gemini.google.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=gemini.google.com
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to clean the redirect links
    function cleanRedirectLinks() {
        // Select all links starting with the Google Search redirect prefix
        const links = document.querySelectorAll('a[href^="https://www.google.com/search?q="]');

        links.forEach(link => {
            // Prevent processing the same link multiple times
            if (link.dataset.cleaned === "true") return;

            try {
                // Parse the current URL
                const urlObj = new URL(link.href);
                // Extract the 'q' parameter which contains the real URL
                const realUrl = urlObj.searchParams.get('q');

                // Check if realUrl exists and starts with http/https (to avoid breaking legitimate text searches)
                if (realUrl && (realUrl.startsWith('http://') || realUrl.startsWith('https://'))) {

                    // Replace the redirect link with the direct URL
                    link.href = realUrl;

                    // Optional: Force open in new tab to keep Gemini chat active
                    link.target = "_blank";
                    link.rel = "noopener noreferrer";

                    // Mark as cleaned to avoid re-processing
                    link.dataset.cleaned = "true";
                }
            } catch (e) {
                console.error("Gemini Direct Links - Error cleaning link:", e);
            }
        });
    }

    // Create a MutationObserver to handle dynamic content loading (SPA)
    const observer = new MutationObserver((mutations) => {
        cleanRedirectLinks();
    });

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

    // Initial run
    cleanRedirectLinks();

})();