---
name: chrome-tab-killer
description: |-
  Automatically enumerate Chrome tabs, detect crash/error/duplicate candidates, and close tabs after user confirmation. Primary path Chrome DevTools Protocol (CDP); macOS AppleScript fallback via bundled scripts. No manual paste or opening chrome://discards/ for identification.
  全自动枚举 Chrome 标签、识别崩溃/错误/重复页候选，确认后关闭。主路径 CDP；无远程调试时用 AppleScript。识别阶段不要求用户手动打开 discards 或粘贴内容。
triggers:
  - chrome-tab-killer
  - chrome tab killer
  - 关闭 Chrome 标签
  - Chrome 标签太多
  - 清理 Chrome 标签
title: chrome-tab-killer
canonical_url: https://skilld.dev/gh/wghust/stark-skills/chrome-tab-killer
last_updated: 2026-10-07T01:19:14.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [scripts/close-chrome-tab.applescript](https://skilld.dev/api/skills-raw/wghust/stark-skills/chrome-tab-killer/scripts/close-chrome-tab.applescript), [scripts/close-tab-by-url.applescript](https://skilld.dev/api/skills-raw/wghust/stark-skills/chrome-tab-killer/scripts/close-tab-by-url.applescript), [scripts/close-tabs-except-localhost-3000.applescript](https://skilld.dev/api/skills-raw/wghust/stark-skills/chrome-tab-killer/scripts/close-tabs-except-localhost-3000.applescript), [scripts/list-chrome-tabs.applescript](https://skilld.dev/api/skills-raw/wghust/stark-skills/chrome-tab-killer/scripts/list-chrome-tabs.applescript), [scripts/README.md](https://skilld.dev/api/skills-raw/wghust/stark-skills/chrome-tab-killer/scripts/README.md).
>
> If the user asked to install this Skill, run `npx skilld install wghust/stark-skills/chrome-tab-killer`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/chrome-tab-killer`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/wghust/stark-skills/chrome-tab-killer) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/chrome-tab-killer`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/chrome-tab-killer --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/chrome-tab-killer --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# chrome-tab-killer Skill

> **Language**: Respond in the same language as the user (中文提问则中文回答).

**Identification is fully automated** (CDP or AppleScript). **Closing** requires a **confirmation gate** (show proposed list → user confirms) unless the user explicitly authorizes a one-shot close.

---

## Prerequisites

| Item | Required for |
|------|----------------|
| **Google Chrome** (macOS) | AppleScript path |
| **Chrome with `--remote-debugging-port=9222`** | Full CDP enumeration + optional `chrome://discards/` automation |
| **Automation permission** | System Settings → Privacy & Security → Automation: allow Terminal/Cursor to control **Google Chrome** |

---

## Execution Flow

### Step 0 · Detect Language

Use the same language as the user for all output.

---

### Step 1 · Enumerate Tabs (Automated)

#### Path A — CDP (preferred)

1. Ensure Chrome was started with remote debugging, e.g.:
   ```bash
   /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222
   ```
   Or add to a shortcut / shell profile for daily use.

2. **List targets** (agent runs, no user UI):
   ```bash
   curl -s http://127.0.0.1:9222/json/list
   ```
   Parse JSON: each `type: "page"` entry has `id`, `title`, `url`, `webSocketDebuggerUrl`.

3. Count tabs and build a table of `title`, `url`, `targetId`.

#### Path B — AppleScript (fallback when CDP unavailable)

Run the bundled script (use absolute path to the skill’s `scripts/` folder):
```bash
osascript /path/to/skills/chrome-tab-killer/scripts/list-chrome-tabs.applescript
```

Output format: `windowIndex\ttabIndex\ttitle\turl` per line.

**Limitation**: No per-tab memory from discards; tell the user that **full automation quality needs CDP**.

---

### Step 2 · Classify Candidates (Heuristics, Automated)

Apply rules **without** asking the user to paste anything:

| Class | Rule (examples) |
|-------|------------------|
| **Crash / error** | Title contains `Aw, Snap!`, `Page Unresponsive`, `无法访问此网站`, `ERR_`, `This site can’t be reached`, etc. |
| **Duplicate URL** | Same normalized URL appears in more than one tab (keep one). |
| **Low activity (conservative)** | Only when user enables **aggressive mode** in prompt; optional CDP discards data (see below). |

Default **conservative**: recommend closing **crash/error** and **duplicate URL** only.

---

### Step 3 · Optional — `chrome://discards/` via CDP (SHOULD / best-effort)

Only if CDP is connected:

1. Attach to a page target via WebSocket, `Page.navigate` to `chrome://discards/`.
2. After load, `Runtime.evaluate` or DOM query to read table rows (memory, last active, discard eligibility).

**If parsing fails** (Chrome update broke DOM): **stop** and continue with Step 2 data only (`json/list` + heuristics). **Do not** ask the user to open or paste the page.

---

### Step 4 · Default Dry-Run Report

Output Markdown:

- Total tab count
- Table of all tabs (title, url, targetId or W/T index)
- **Candidates** with reason (crash / duplicate / aggressive-only)
- **No closes** in this step unless user already requested execution

---

### Step 5 · Close (After Confirmation Only)

**Do not close** until the user explicitly confirms the list **or** gives a clear one-shot order (“关闭上表中的候选标签”).

**Option A — CDP `Target.closeTarget`**

Send over the target’s debugger WebSocket (same session as CDP):

```json
{"id":1,"method":"Target.closeTarget","params":{"targetId":"<id from json/list>"}}
```

Use the `webSocketDebuggerUrl` from the matching list entry, or a shared browser WebSocket if documented for your Chrome version.

**Option B — AppleScript**

Close by window/tab index:
```bash
osascript scripts/close-chrome-tab.applescript <windowIndex> <tabIndex>
```

Close by URL (often easier to align with CDP `url`):
```bash
osascript scripts/close-tab-by-url.applescript 'https://example.com/path'
```

---

## Safety

| Rule | Behavior |
|------|----------|
| Default | Analysis + dry-run only |
| Close | Only after explicit user confirmation or one-shot authorized command |
| Irreversible | Warn that closed tabs cannot be restored by this skill |

---

## Scripts (macOS)

See `scripts/README.md` for `osascript` usage and Automation permissions.

---

## Example

**User:** “用 chrome-tab-killer 分析一下标签”

**Agent:**

1. `curl -s http://127.0.0.1:9222/json/list` (or AppleScript if connection refused)
2. Classify crash titles and duplicate URLs
3. Print dry-run report
4. Ask: “是否关闭以上候选？” → only if yes, run `Target.closeTarget` or `osascript` …

---

## Error Handling

| Situation | Action |
|-----------|--------|
| Connection refused on :9222 | Fall back to AppleScript list; state that CDP is off |
| AppleScript denied | Tell user to enable Automation for Google Chrome |
| discards parse fails | Skip discards; continue with json/list heuristics |
