Gemini Code Collapse & Copy

Collapse code blocks by default with show/hide and copy buttons on Gemini

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Gemini Code Collapse & Copy
// @namespace    https://google.com/
// @version      1.0
// @description  Collapse code blocks by default with show/hide and copy buttons on Gemini
// @author       3xploiton3
// @match        https://gemini.google.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
  "use strict";

  const CSS = `
    .gm-btn {
      background: #f1f3f4;
      border: 1px solid #dadce0;
      border-radius: 4px;
      padding: 2px 6px;
      margin: 2px 4px 2px 0;
      font-size: 12px;
      cursor: pointer;
    }
    .gm-btn:hover {
      background: #e8eaed;
    }
    .gm-code-container {
      position: relative;
      margin-bottom: 8px;
    }
  `;
  const style = document.createElement("style");
  style.textContent = CSS;
  document.head.append(style);

  function enhance(pre) {
    if (pre.dataset.gmEnhanced) return;
    pre.dataset.gmEnhanced = "1";

    const code = pre.querySelector("code");
    if (!code) return;

    code.style.display = "none";
    pre.classList.add("gm-code-container");

    const wrapper = document.createElement("div");
    wrapper.style.display = "flex";
    wrapper.style.alignItems = "center";

    const btnShow = document.createElement("button");
    btnShow.textContent = "Show code";
    btnShow.className = "gm-btn";
    btnShow.addEventListener("click", () => {
      const visible = code.style.display === "";
      code.style.display = visible ? "none" : "";
      btnShow.textContent = visible ? "Show code" : "Hide code";
    });

    const btnCopy = document.createElement("button");
    btnCopy.textContent = "Copy";
    btnCopy.className = "gm-btn";
    btnCopy.addEventListener("click", () => {
      navigator.clipboard.writeText(code.innerText);
      btnCopy.textContent = "Copied!";
      setTimeout(() => (btnCopy.textContent = "Copy"), 1000);
    });

    wrapper.append(btnShow, btnCopy);
    pre.parentElement.insertBefore(wrapper, pre);
  }

  function scan() {
    document.querySelectorAll("pre").forEach(enhance);
  }

  scan();
  new MutationObserver(scan).observe(document.body, {
    childList: true,
    subtree: true,
  });
})();