显示 HN:用户脚本的 Cursor
Show HN: Cursor for Userscripts

原始链接: https://github.com/chebykinn/browser-code

## 浏览器代码:网页编码代理 浏览器代码是一个浏览器扩展,它使 Claude(或类似代理)能够像操作文件系统一样与网页进行交互和修改。它将每个网站呈现为一个虚拟文件系统——`page.html` 代表 DOM,`console.log` 用于输出,目录用于脚本和样式——允许代理生成、编辑和管理用户脚本。 这些脚本使用浏览器的用户脚本 API(Chrome 120+ 和 Firefox)保存,并在匹配的 URL 上自动运行,甚至包括动态路由(例如 `/products/[id]`)。一个关键特性是版本控制,防止基于过时的 DOM 状态进行编辑。 该扩展以两种模式运行:**计划模式**(只读探索和规划)和 **执行模式**(脚本执行和 DOM 修改,在用户批准后)。它提供双向用户脚本同步到本地文件系统(Chrome)或导出功能(Firefox)。 代理使用 `Read`、`Edit`、`Write`、`Glob` 和 `Bash` 等工具来操作虚拟文件系统。它通过用户脚本 API 绕过内容安全策略 (CSP) 限制,从而能够在 LinkedIn 等网站上执行脚本。

## 用户脚本的游标:一个基于浏览器的编码代理 一位 Hacker News 的开发者分享了“游标”,这是一个实验性的浏览器扩展,它将一个编码代理(如 Claude Code/Cursor)直接嵌入到浏览器中。游标不再手动使用 JavaScript 在控制台中编辑 DOM,而是将网页视为构建在浏览器本地存储之上的虚拟文件系统中的文件。 该代理可以生成和维护用户脚本和 CSS,利用工具来读取/编辑文件、grep 内容,并通过模拟的 bash 环境执行 JS 代码——镜像了这些模型经常训练的 RL 沙箱。目前与 Opus 4.5 测试,已被证明对诸如数据提取到 CSV 等任务有用。 虽然 Chrome Web Store 的提交正在等待处理,但开发者出于信任原因优先考虑基于源代码的构建,因为该扩展具有广泛的权限。讨论强调了与其他模型(如 Gemini Flash)的潜在集成,以及与类似项目(如 QuillMonkey 和 RobotMonkey)的比较,强调了人们对这种网络脚本编写方法的日益增长的兴趣。
相关文章

原文

A coding agent for userscripts with its own loader.

Browser Code is a browser extension that gives Claude a virtual filesystem view of web pages. It generates, edits, and manages userscripts that persist to chrome.userScripts (the same API that Tampermonkey uses) and auto-run on matching URLs.

Think Claude Code, but for the DOM.

demo.mp4
  1. Agent sees the page as a filesystem - The DOM becomes page.html, console output is console.log, and you can create scripts in ./scripts/ and styles in ./styles/
  2. Scripts persist via userScripts API - Saved scripts register with Chrome's userScripts API (Chrome 120+) or Firefox's equivalent, bypassing CSP restrictions
  3. Auto-runs on matching URLs - Scripts execute on page load for their saved URL patterns, including dynamic routes like /products/[id]
┌─────────────────────────────────────────────────────────────────┐
│                     Browser Extension                           │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  ┌──────────────────────────────────────────────────────────┐   │
│  │              Background Service Worker                    │  │
│  │  • Claude API client (agentic loop)                       │  │
│  │  • userScripts registration & CSP bypass                  │  │
│  │  • Conversation history per tab                           │  │
│  │  • VFS storage coordination                               │  │
│  └──────────────────────────────────────────────────────────┘   │
│                              ↕                                  │
│  ┌──────────────────────────────────────────────────────────┐   │
│  │                   Content Script                          │  │
│  │  • Virtual Filesystem (VFS) implementation                │  │
│  │  • DOM ↔ HTML serialization with version tracking         │  │
│  │  • Script execution in MAIN world                         │  │
│  │  • Console interception                                   │  │
│  └──────────────────────────────────────────────────────────┘   │
│                              ↕                                  │
│  ┌──────────────────────────────────────────────────────────┐   │
│  │                   Sidebar Panel (React)                   │  │
│  │  • Chat UI with tool call visualization                   │  │
│  │  • Plan/Execute mode toggle                               │  │
│  │  • File browser & local sync                              │  │
│  └──────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

Every website is presented as a virtual filesystem:

/{domain}/{url-path}/
├── page.html           # Live DOM (read/edit triggers mutations)
├── console.log         # Captured console output (read-only)
├── screenshot.png      # On-demand page capture
├── plan.md             # Agent's plan (plan mode)
├── scripts/
│   ├── my-script.js    # Your scripts (persisted, auto-run)
│   └── _auto_edits.js  # Generated from DOM edits
└── styles/
    └── custom.css      # Your styles (persisted, auto-injected)

Files have version numbers for optimistic concurrency control. The agent must read a file before editing, and provide the version from the read. If the DOM changed (user scrolled, JS mutated it), the edit fails with a version mismatch - forcing a re-read.

This prevents the agent from making changes based on stale data when editing a live page.

Scripts saved to paths like /products/[id] or /docs/[...slug] match dynamically:

Pattern Matches window.__routeParams
/products/[id] /products/123 { id: "123" }
/users/[userId]/posts/[postId] /users/5/posts/42 { userId: "5", postId: "42" }
/docs/[...path] /docs/api/auth/oauth { path: ["api", "auth", "oauth"] }

Route params are injected into window.__routeParams before your script runs.

Browser Code uses a two-phase workflow for safety:

  1. Plan Mode (default) - Agent explores the page, reads files, proposes changes to plan.md. Cannot mutate DOM or write scripts.
  2. Execute Mode - After user approval, agent executes the plan. Can write files, edit DOM, run scripts.

This prevents the agent from making unintended changes while exploring.

Sync your userscripts bidirectionally with your local filesystem:

Browser Read Write API Used
Chrome File System Access API
Firefox - ✓ (export) Downloads API

Chrome: Select a directory once, then edits sync both ways. Edit in VS Code, see changes in browser.

Firefox: Export-only via Downloads API. Scripts download to your configured directory.

Conflict resolution: newest wins, or choose per-conflict.

The agent has filesystem-like tools:

Tool Description
Read Read file content. Returns content + version for conflict detection.
Edit Find-and-replace in a file. Requires version from last Read.
Write Write entire file. Use version 0 for new files.
Glob Find files matching a pattern (./scripts/*.js).
Grep Search for regex in files. Returns matches with context.
Bash Execute a script file (./scripts/foo.js) or inline JS.
Ls List directory contents.
Screenshot Capture current viewport.
Todo Manage task list for multi-step operations.
  1. Build the extension (see below) or download a release
  2. Go to chrome://extensions
  3. Enable Developer mode
  4. Click Load unpacked → select .output/chrome-mv3/
  5. Click extension Details → enable User scripts permission (required for CSP bypass)
  1. Build the extension (see below)
  2. Go to about:debugging#/runtime/this-firefox
  3. Click Load Temporary Add-on → select any file in .output/firefox-mv2/
bun install

# Development
bun run dev           # Chrome
bun run dev:firefox   # Firefox

# Production
bun run build         # Both browsers
bun run zip           # Create distribution zips
  • CSP bypass requires permission: Sites like LinkedIn have strict CSP. Chrome's userScripts API bypasses this, but you must enable the "User scripts" permission in extension settings.
  • Trusted Types: Some sites sanitize innerHTML. Scripts may need to use DOM APIs (createElement) instead.
  • Firefox sync is export-only: No File System Access API in Firefox, so sync is one-way via Downloads.

Storage: VFS data stored in browser.storage.local keyed by vfs:{domain}. Each domain has paths → scripts/styles.

Script Registration: On storage change, background script re-registers all scripts via chrome.userScripts.register() with match patterns derived from VFS paths.

CSP Bypass: userScripts API configures a custom world with permissive CSP: script-src 'self' 'unsafe-inline' 'unsafe-eval'.

DOM Serialization: page.html reads serialize the full document. Writes diff against current DOM and apply minimal mutations.

联系我们 contact @ memedata.com