Google AI Studio & Gemini - Clean & Direct Links 🔗

Removes the google.com/url and google.com/search redirection wrappers from links generated by AI Studio and Gemini. Allows direct access to external websites.

Vous devrez installer une extension telle que Tampermonkey, Greasemonkey ou Violentmonkey pour installer ce script.

Vous devrez installer une extension telle que Tampermonkey pour installer ce script.

Vous devrez installer une extension telle que Tampermonkey ou Violentmonkey pour installer ce script.

Vous devrez installer une extension telle que Tampermonkey ou Userscripts pour installer ce script.

Vous devrez installer une extension telle que Tampermonkey pour installer ce script.

Vous devrez installer une extension de gestionnaire de script utilisateur pour installer ce script.

(J'ai déjà un gestionnaire de scripts utilisateur, laissez-moi l'installer !)

Vous devrez installer une extension telle que Stylus pour installer ce style.

Vous devrez installer une extension telle que Stylus pour installer ce style.

Vous devrez installer une extension telle que Stylus pour installer ce style.

Vous devrez installer une extension du gestionnaire de style pour utilisateur pour installer ce style.

Vous devrez installer une extension du gestionnaire de style pour utilisateur pour installer ce style.

Vous devrez installer une extension du gestionnaire de style pour utilisateur pour installer ce style.

(J'ai déjà un gestionnaire de style utilisateur, laissez-moi l'installer!)

// ==UserScript==
// @name         Google AI Studio & Gemini - Clean & Direct Links 🔗
// @version      2.0
// @description  Removes the google.com/url and google.com/search redirection wrappers from links generated by AI Studio and Gemini. Allows direct access to external websites.
// @author       Milor123
// @match        https://aistudio.google.com/*
// @match        https://gemini.google.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=google.com
// @grant        none
// @license      MIT
// @run-at       document-end
// @namespace http://tampermonkey.net/
// ==/UserScript==

(function() {
    'use strict';

    /**
     * Main function to clean dirty redirection links.
     * It handles both:
     * 1. AI Studio style: google.com/url?q=...
     * 2. Gemini style: google.com/search?q=... (only if q is a URL)
     */
    function cleanLinks() {
        // Select all anchor tags that look like a Google redirect or search wrapper
        const selector = 'a[href*="google.com/url"], a[href*="google.com/search?q="]';
        const links = document.querySelectorAll(selector);

        links.forEach(link => {
            // Optimization: Skip if already processed
            if (link.dataset.cleaned === "true") return;

            try {
                const urlObj = new URL(link.href);

                // 1. Extract the destination. Google usually puts it in 'q', sometimes 'url'.
                let realUrl = urlObj.searchParams.get('q') || urlObj.searchParams.get('url');

                // 2. Validation Logic
                if (realUrl) {
                    // Decode the URL (converts %3A to :, etc.)
                    realUrl = decodeURIComponent(realUrl);

                    // CRITICAL CHECK:
                    // Gemini uses "google.com/search?q=apple" for searching text.
                    // We only want to replace the link if 'q' is actually a WEBSITE (starts with http).
                    if (realUrl.startsWith('http://') || realUrl.startsWith('https://')) {

                        // Replace the href with the clean URL
                        link.href = realUrl;

                        // Ensure it opens in a new tab
                        link.target = "_blank";
                        link.rel = "noopener noreferrer";

                        // Mark as cleaned
                        link.dataset.cleaned = "true";
                    }
                }
            } catch (e) {
                // Silent catch to prevent console spam if a link is malformed
                // console.error("Link Cleaner Error:", e);
            }
        });
    }

    /**
     * MutationObserver setup.
     * Both AI Studio and Gemini are SPAs (Single Page Applications).
     * We need to watch for new nodes being added to the DOM as the AI types.
     */
    const observer = new MutationObserver((mutations) => {
        // We don't need to check every mutation type, just run the cleaner.
        // It's fast enough to run on every batch of updates.
        cleanLinks();
    });

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

    // Initial run to handle any static links loaded on refresh
    cleanLinks();

})();