Google AI Studio | Collapse/Expand All Code Blocks Toggle

Toggle all code blocks open/closed in Google AI Studio with lazy loading support.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

作者
piknockyou
今日安裝
0
安裝總數
3
評價
0 0 0
版本
1.6
建立日期
2025-12-21
更新日期
2025-12-29
尺寸
10.2 KB
授權條款
AGPL-3.0
腳本執行於

See screenshots at the bottom

Google AI Studio | Collapse/Expand All Code Blocks Toggle

Adds a toolbar button to collapse or expand all code blocks instantly. Ideal for long AI conversations with lots of code snippets.

Why You Need This

Long AI responses with multiple code blocks make conversations hard to navigate:

  • Scrolling fatigue: Endless scrolling past code you've already reviewed.
  • Context lost: Hard to see the conversation flow buried under walls of code.
  • No native solution: Google AI Studio has no built-in way to manage code block visibility.

This script lets you control all code blocks with a single toolbar button.

Key Features

  • Smart Initial Action: First click always collapses all blocks to instantly declutter your view.
  • Persistent Mode Toggle: Each subsequent click switches between Collapse/Expand modes. Your preference is saved across page reloads and chat switches.
  • Lazy Loading Support: Automatically manages code blocks as they appear during scrolling or real-time AI generation.
  • Color-Coded Feedback:
    • Gray Icon: Inactive (click to activate Collapse Mode)
    • Blue Icon: Collapse Mode active (new blocks auto-collapse)
    • Amber Icon: Expand Mode active (new blocks auto-expand)
  • Native Integration: Adds a toolbar button that matches Google AI Studio's aesthetic.

How to Use

Action Result
First Click Activate Collapse Mode (Blue) — all current and future code blocks stay closed
Second Click Switch to Expand Mode (Amber) — all current and future code blocks stay open
Subsequent Clicks Toggle between Collapse/Expand modes

Performance Notes

  • Incremental detection: Uses MutationObserver to only process newly added code blocks (no full-page rescans).
  • Smart debouncing: Per-block 500ms debounce prevents flickering during rapid scrolling or AI generation.
  • SPA-aware: Toolbar observer ensures the button persists during navigation between chats.

Installation

  1. Install Violentmonkey (recommended), Tampermonkey, or Greasemonkey.
  2. Install this script.
  3. Visit aistudio.google.com.

Works immediately. The toggle button appears in the top-right toolbar next to the "More options" (⋮) button.

Technical Details

  • Persistence: Uses localStorage to remember your toggle state.
  • Icon: Uses Material Symbols (expand_less, expand_more) natively available in the app.
  • Styling: Minimal CSS injection to match native toolbar layout.