Gemini Code Collapse & Copy

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

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

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 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.

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

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 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,
  });
})();