Gemini - 中键点击在新标签页打开 (多语言支持)

允许在 Gemini 的对话历史、"发起新对话"和"探索 Gem"上使用鼠标中键点击,从而在新的后台标签页中打开它们。此版本支持不同的界面语言。

当前为 2025-07-30 提交的版本,查看 最新版本

// ==UserScript==
// @name         Gemini - Middle-click to open in new tab (Multi-language)
// @name:zh-CN   Gemini - 中键点击在新标签页打开 (多语言支持)
// @namespace    http://tampermonkey.net/
// @version      1.5
// @description  Allows middle-clicking on conversation history, "New Chat", and "Explore Gems" in Gemini to open them in a new background tab. Works across different UI languages.
// @description:zh-CN  允许在 Gemini 的对话历史、"发起新对话"和"探索 Gem"上使用鼠标中键点击,从而在新的后台标签页中打开它们。此版本支持不同的界面语言。
// @author       Gemini & contributors
// @match        https://gemini.google.com/app*
// @match        https://gemini.google.com/gems*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=gemini.google.com
// @grant        GM_openInTab
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    document.addEventListener('mousedown', function(event) {
        // event.button === 1 代表鼠标中键点击
        if (event.button !== 1) {
            return;
        }

        // 【重大更新】使用不依赖于语言的图标选择器
        // :has() 选择器可以找到包含特定子元素的父元素,非常适合此场景
        const newChatButton = event.target.closest('button:has(mat-icon[fonticon="edit_square"])');
        const exploreGemsButton = event.target.closest('button:has(mat-icon[fonticon="gem_spark"])');
        const conversationElement = event.target.closest('[data-test-id="conversation"]');

        let url = null;
        let logMessage = '';

        // 处理“发起新对话”按钮
        if (newChatButton) {
            url = 'https://gemini.google.com/app';
            logMessage = 'Opening New Chat in new background tab:';
        }
        // 处理“探索 Gem”按钮
        else if (exploreGemsButton) {
            url = 'https://gemini.google.com/gems/view';
            logMessage = 'Opening Explore Gems in new background tab:';
        }
        // 处理对话历史记录
        else if (conversationElement) {
            const jslog = conversationElement.getAttribute('jslog');
            if (jslog) {
                // 正则表达式匹配16位以上的十六进制字符作为ID
                const match = jslog.match(/([a-f0-9]{16,})/);
                if (match && match[1]) {
                    const conversationId = match[1];
                    url = `https://gemini.google.com/app/${conversationId}`;
                    logMessage = 'Opening Gemini conversation in new background tab:';
                }
            }
        }

        // 如果成功获取了URL,则阻止默认行为并在后台新标签页中打开
        if (url) {
            event.preventDefault();
            event.stopPropagation();
            console.log(logMessage, url);
            // 将 active 设置为 false,实现在后台打开新标签页
            GM_openInTab(url, { active: false });
        }

    }, true); // 使用捕获阶段以确保高优先级

})();