Google AI Studio | Toggle Code Blocks

Toggle all code blocks open/closed in Google AI Studio. First click always closes all code blocks.

目前為 2025-12-21 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

作者
piknockyou
評價
0 0 0
版本
1.0
建立日期
2025-12-21
更新日期
2025-12-21
尺寸
14.0 KB
授權條款
AGPL-3.0
腳本執行於

Google AI Studio | Toggle Code Blocks

Introduces the ability to manage code block visibility in Google AI Studio. Adds a toolbar button to instantly collapse or expand all code snippets for a cleaner workspace.

Features

  • Smart Initial Action: The first click always collapses all code blocks to immediately de-clutter the view.
  • Consistent Toggling: Alternates between expanding all and collapsing all on subsequent clicks.
  • Toolbar Integration: Adds a native-style button (chevron icon) to the top-right toolbar.
  • Visual Feedback: Icons update (expand_less/expand_more) to indicate the next action.
  • SPA Support: Automatically detects navigation between chats and resets state to ensure predictable behavior.

Installation

Requirements

Steps

  1. Install a userscript manager.
  2. Install this script.
  3. Open any prompt in Google AI Studio.

Usage

  1. Locate the button: Look for the chevron icon (⌃/⌄) in the toolbar next to the "More options" (⋮) button.
  2. First Click: Collapses all open code blocks.
  3. Second Click: Expands all code blocks.

How It Works

  • State Management: Tracks whether the next action should be "Collapse" or "Expand". It defaults to "Collapse" on load and reset.
  • DOM Interaction: Scans for mat-expansion-panel-header elements inside code blocks and programmatically clicks them if their current state (aria-expanded) differs from the target state.
  • Observer: Uses a MutationObserver to ensure the button persists across the dynamic single-page application lifecycle.

Technical Details

  • Icon: Uses standard Material Symbols (expand_less, expand_more) natively available in the app resources.
  • Styling: Injects minimal CSS to match the native toolbar layout.