Gemini - Middle-click to open chat in new tab

Allows middle-clicking on conversation history, "New Chat" button, and "Explore Gems" button in Gemini to open them in a new background tab.

目前為 2025-07-30 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Gemini - Middle-click to open chat in new tab
// @name:zh-CN   Gemini - 中键点击在新标签页打开对话/页面
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  Allows middle-clicking on conversation history, "New Chat" button, and "Explore Gems" button in Gemini to open them in a new background tab.
// @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;
        }

        // 查找点击的元素或其最近的祖先元素
        const newChatButton = event.target.closest('button[aria-label="发起新对话"]');
        const exploreGemsButton = event.target.closest('button[aria-label="Explore Gems"]');
        const conversationElement = event.target.closest('[data-test-id="conversation"]');

        let url = null;
        let logMessage = '';

        // 【新增】处理“发起新对话”按钮
        if (newChatButton) {
            event.preventDefault();
            event.stopPropagation();
            url = 'https://gemini.google.com/app';
            logMessage = 'Opening New Chat in new background tab:';
        }
        // 【新增】处理“探索 Gem”按钮
        else if (exploreGemsButton) {
            event.preventDefault();
            event.stopPropagation();
            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]) {
                    event.preventDefault();
                    event.stopPropagation();
                    const conversationId = match[1];
                    url = `https://gemini.google.com/app/${conversationId}`;
                    logMessage = 'Opening Gemini conversation in new background tab:';
                }
            }
        }

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

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

})();