Add Copy Button to Query Bubbles For Google Gemini Chat

Adds a copy button to query bubbles for easy text copying

目前为 2025-02-21 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Add Copy Button to Query Bubbles For Google Gemini Chat
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Adds a copy button to query bubbles for easy text copying
  6. // @author aspen138
  7. // @match *://gemini.google.com/app/*
  8. // @grant none
  9. // @icon https://www.gstatic.com/lamda/images/gemini_home_icon_8d62f72e7aae54b6859f1.png
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // Function to create a copy button
  17. function createCopyButton(textElement) {
  18. const button = document.createElement('button');
  19. button.textContent = 'Copy';
  20. button.style.marginLeft = '10px';
  21. button.style.padding = '2px 8px';
  22. button.style.fontSize = '12px';
  23. button.style.cursor = 'pointer';
  24.  
  25. // Add click event to copy text
  26. button.addEventListener('click', function() {
  27. const textToCopy = textElement.textContent.trim();
  28. navigator.clipboard.writeText(textToCopy)
  29. .then(() => {
  30. // Visual feedback
  31. button.textContent = 'Copied!';
  32. setTimeout(() => {
  33. button.textContent = 'Copy';
  34. }, 2000);
  35. })
  36. .catch(err => {
  37. console.error('Failed to copy text: ', err);
  38. button.textContent = 'Error';
  39. });
  40. });
  41.  
  42. return button;
  43. }
  44.  
  45. // Function to add copy buttons to all query bubbles
  46. function addCopyButtons() {
  47. const queryBubbles = document.querySelectorAll('.user-query-bubble-with-background');
  48.  
  49. queryBubbles.forEach(bubble => {
  50. // Check if button already exists to avoid duplicates
  51. if (!bubble.querySelector('.copy-button')) {
  52. const textElement = bubble.querySelector('.query-text');
  53. if (textElement) {
  54. const copyButton = createCopyButton(textElement);
  55. copyButton.classList.add('copy-button'); // Add class for tracking
  56. const container = bubble.querySelector('.horizontal-container');
  57. if (container) {
  58. container.appendChild(copyButton);
  59. }
  60. }
  61. }
  62. });
  63. }
  64.  
  65. // Initial run
  66. addCopyButtons();
  67.  
  68. // Observe for dynamic content changes
  69. const observer = new MutationObserver(function(mutations) {
  70. mutations.forEach(function(mutation) {
  71. if (mutation.addedNodes.length) {
  72. addCopyButtons();
  73. }
  74. });
  75. });
  76.  
  77. // Start observing the document body for changes
  78. observer.observe(document.body, {
  79. childList: true,
  80. subtree: true
  81. });
  82.  
  83. })();

QingJ © 2025

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