Gemini Copy Text Button

特定の要素からテキストをコピーするボタンを追加します。

As of 2024-06-23. See the latest version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        Gemini Copy Text Button
// @namespace   https://qestir.hatenablog.com/entry/2024/06/23/153328
// @match       https://gemini.google.com/*
// @grant       none
// @version     1.2
// @description 特定の要素からテキストをコピーするボタンを追加します。
// @author      Qestir
// @license GPL-3.0-or-later
//
// ==/UserScript==
// スクリプトを実行する関数
(function() {
  // テキストをコピーする関数
  function copyTextToClipboard(text) {
    // 一時的なテキストエリアを作成
    var textArea = document.createElement("textarea");
    textArea.value = text;

    // テキストエリアをドキュメントに追加
    document.body.appendChild(textArea);

    // テキストを選択
    textArea.select();

    try {
      // クリップボードにコピー
      var successful = document.execCommand('copy');
      var msg = successful ? '成功' : '失敗';
      console.log('テキストのコピーに' + msg + 'しました');
    } catch (err) {
      console.error('テキストのコピーに失敗しました', err);
    }

    // テキストエリアをドキュメントから削除
    document.body.removeChild(textArea);
  }

  // テキストをコピーするボタンを作成する関数
  function createCopyButton(targetElement) {
    // コピー用ボタンを作成
    var button = document.createElement("button");
    button.textContent = "コピー";
    button.style.marginLeft = "10px";
    button.style.padding = "5px 10px";
    button.style.backgroundColor = "#4CAF50";
    button.style.color = "white";
    button.style.border = "none";
    button.style.borderRadius = "5px";
    button.style.cursor = "pointer";

    // ボタンにホバースタイルを追加
    button.addEventListener("mouseover", function() {
      button.style.backgroundColor = "#45a049";
    });
    button.addEventListener("mouseout", function() {
      button.style.backgroundColor = "#4CAF50";
    });

    // ボタンがクリックされた時の処理
    button.addEventListener("click", function() {
      // query-textクラスのテキストのみを取得
      var textElement = targetElement.querySelector(".query-text");
      if (textElement) {
        var text = textElement.textContent;
        copyTextToClipboard(text);
      }
    });

    // 対象の要素にボタンを追加
    targetElement.appendChild(button);
  }

  // 特定のクラス名の要素にボタンを追加する関数
  function addCopyButtons() {
    var userQueryElements = document.querySelectorAll("user-query");
    userQueryElements.forEach(function(element) {
      if (!element.querySelector("button")) {
        createCopyButton(element);
      }
    });
  }

  // MutationObserverの設定
  var observer = new MutationObserver(function(mutations) {
    mutations.forEach(function(mutation) {
      mutation.addedNodes.forEach(function(node) {
        if (node.nodeType === 1) { // 要素ノードの場合
          var userQueryElements = node.querySelectorAll("user-query");
          userQueryElements.forEach(function(element) {
            createCopyButton(element);
          });
        }
      });
    });
  });

  // 監視を開始
  observer.observe(document.body, { childList: true, subtree: true });

  // 初回実行
  addCopyButtons();
})();