Add Copy Button to Query Bubbles For Google Gemini Chat

Adds a copy button to query bubbles for easy text copying

目前為 2025-02-21 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Add Copy Button to Query Bubbles For Google Gemini Chat
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Adds a copy button to query bubbles for easy text copying
// @author       aspen138
// @match        *://gemini.google.com/app/*
// @grant        none
// @icon         https://www.gstatic.com/lamda/images/gemini_home_icon_8d62f72e7aae54b6859f1.png
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to create a copy button
    function createCopyButton(textElement) {
        const button = document.createElement('button');
        button.textContent = 'Copy';
        button.style.marginLeft = '10px';
        button.style.padding = '2px 8px';
        button.style.fontSize = '12px';
        button.style.cursor = 'pointer';

        // Add click event to copy text
        button.addEventListener('click', function() {
            const textToCopy = textElement.textContent.trim();
            navigator.clipboard.writeText(textToCopy)
                .then(() => {
                    // Visual feedback
                    button.textContent = 'Copied!';
                    setTimeout(() => {
                        button.textContent = 'Copy';
                    }, 2000);
                })
                .catch(err => {
                    console.error('Failed to copy text: ', err);
                    button.textContent = 'Error';
                });
        });

        return button;
    }

    // Function to add copy buttons to all query bubbles
    function addCopyButtons() {
        const queryBubbles = document.querySelectorAll('.user-query-bubble-with-background');

        queryBubbles.forEach(bubble => {
            // Check if button already exists to avoid duplicates
            if (!bubble.querySelector('.copy-button')) {
                const textElement = bubble.querySelector('.query-text');
                if (textElement) {
                    const copyButton = createCopyButton(textElement);
                    copyButton.classList.add('copy-button'); // Add class for tracking
                    const container = bubble.querySelector('.horizontal-container');
                    if (container) {
                        container.appendChild(copyButton);
                    }
                }
            }
        });
    }

    // Initial run
    addCopyButtons();

    // Observe for dynamic content changes
    const observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            if (mutation.addedNodes.length) {
                addCopyButtons();
            }
        });
    });

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

})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址