Google AI Studio | Collapse/Expand All Code Blocks Toggle

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

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

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

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

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

您需要先安装一款用户脚本管理器扩展,例如 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.