Copy Button For Gemini Generated Text in Box

Google Gemini

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Copy Button For Gemini Generated Text in Box
// @namespace   https://www.ufukyayla.com.tr/gemini_copy
// @match       https://gemini.google.com/*
// @grant       none
// @version     1.2
// @description Google Gemini
// @author      Ufuk Yayla
// @license GPL-3.0-or-later
// ==/UserScript==

(function() {
	function show_toast(txt) {
		var n = document.createElement("div");

		n.textContent = txt;
		n.style.position = "fixed";
		n.style.bottom = "20px";
		n.style.right = "20px";
		n.style.padding = "10px";
		n.style.color = "#ffffff";
		n.style.backgroundColor = "#55bb55";
		n.style.borderRadius = "6px";
		n.style.zIndex = "1000";

		document.body.appendChild(n);

		setTimeout(function() {
			document.body.removeChild(n);
		}, 3000);
	}

	function copy_text(txt) {
		var n = document.createElement("textarea");
		n.visibility = "hidden";
		n.value = txt;
		document.body.appendChild(n);
		
		n.select();

		try {
			var ok = document.execCommand("copy");
			var msg = ok ? "Copied" : "Copy to clipboard is not supported";
			show_toast(msg);
		} catch (err) {
			show_toast("Copy attempt failed");
		}

		document.body.removeChild(n);
	}

	function add_copy_button(elem) {
		var n = document.createElement("button");

		n.textContent = "Copy";
		n.style.padding = "8px";
		n.style.color = "#ffffff";	
		n.style.backgroundColor = "#55bb55";
		n.style.border = "none";
		n.style.borderRadius = "6px";
		n.style.cursor = "pointer";

		n.onmouseover = function() {
			n.style.backgroundColor = "#448844";
		}
		
		n.onmouseout = function() {
			n.style.backgroundColor = "#55bb55";
		}

		n.onclick = function() {
			var text_elem = elem.querySelector(".code-container");
			if (text_elem) {
				var txt = text_elem.textContent;
				copy_text(txt);
			}
		}

		elem.appendChild(n);
	}
	
	//----------observer
	var observer = new MutationObserver(function(mutations) {
		mutations.forEach(function(mutation) {
			mutation.addedNodes.forEach(function(node) {
				if (node.nodeType === 1) {
					var code_blocks = node.querySelectorAll("code-block");
					code_blocks.forEach(function(block) {
						add_copy_button(block);
					});
				}
			});
		});
	});
	observer.observe(document.body, { childList: true, subtree: true });
	
	//----------init
	function add_buttons_init() {
		var code_blocks = document.querySelectorAll("code-block");
		code_blocks.forEach(function(block) {
			if (!block.querySelector("button")) {
				add_copy_button(block);
			}
		});
	}
	add_buttons_init();
})();