|
| 1 | +# BiViNote - Claude Code Context |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +BiViNote is a Chrome/Edge browser extension (Manifest V3) that captures Bilibili video subtitles and screenshots, generating Markdown notes with embedded images. |
| 6 | + |
| 7 | +## Architecture |
| 8 | + |
| 9 | +### Modular Structure |
| 10 | + |
| 11 | +The project uses a modular architecture with two layers: |
| 12 | + |
| 13 | +1. **Core modules** (`src/core/`): Essential functionality shared by all versions |
| 14 | +2. **Feature modules** (`src/modules/deepseek/`): Optional DeepSeek AI functionality |
| 15 | + |
| 16 | +### Message Bus |
| 17 | + |
| 18 | +Modules communicate through a centralized message bus (`src/core/message-bus.js`): |
| 19 | +- `registerHandler(type, handler)`: Register message handlers |
| 20 | +- `handleMessage(message, sender, sendResponse)`: Dispatch messages to handlers |
| 21 | + |
| 22 | +### Core Modules |
| 23 | + |
| 24 | +- **src/core/message-bus.js**: Centralized message bus for inter-module communication |
| 25 | +- **src/core/background.js** (Service Worker): Proxies Bilibili API requests (CORS), manages extension icon state |
| 26 | +- **src/core/panel.js**: Floating panel UI (400x600px), 5 tabs (字幕/章节/视频信息/文档整理/设置), collapse to draggable icon with quick menu, prompt full-page editor |
| 27 | +- **src/core/subtitle.js**: Subtitle fetch, render, highlight sync, click-to-seek, preview popup |
| 28 | +- **src/core/chapter.js**: Chapter fetch, render, click-to-seek, preview popup |
| 29 | +- **src/core/video-info.js**: Video metadata display with checkboxes (including chapter/subtitle timestamp toggles) |
| 30 | +- **src/core/capture.js**: OffscreenCanvas screenshot, save file, clipboard |
| 31 | +- **src/core/crop-viewer.js**: Screenshot viewer with Cropper.js (crop, zoom, rotate, flip), sidebar navigation, frame stepping |
| 32 | +- **src/core/export.js**: SRT/Markdown/ZIP export (assets/ directory, timestamp toggles) |
| 33 | +- **src/core/settings.js**: chrome.storage.local persistence (settings, videoInfoChecked, prompts, downloadDir) |
| 34 | +- **src/core/state.js**: Centralized runtime state with fetchRunId for stale request cancellation |
| 35 | + |
| 36 | +### DeepSeek Module |
| 37 | + |
| 38 | +- **src/modules/deepseek/background.js**: DeepSeek backend logic - SSE stream processing, message routing, stop_stream abort |
| 39 | +- **src/modules/deepseek/panel.js**: DeepSeek panel extension - document organization tab |
| 40 | +- **src/modules/deepseek/client.js**: DeepSeek client - state machine, think/response chunk parsing, request lifecycle, abort with activeRequestId filtering |
| 41 | +- **src/modules/deepseek/api.js**: Injected into DeepSeek MAIN world - auth token extraction, PoW solving, completion API, stop_stream API |
| 42 | +- **src/modules/deepseek/bridge.js**: Injected into DeepSeek ISOLATED world - message bridge between MAIN world and background |
| 43 | +- **src/modules/deepseek/wasm-solver.js**: DeepSeek PoW WASM solver for DeepSeekHashV1 algorithm |
| 44 | + |
| 45 | +### Build System |
| 46 | + |
| 47 | +The build script (`scripts/build.js`) generates two versions: |
| 48 | +- **Main version** (`dist/main/`): Core + DeepSeek modules merged |
| 49 | +- **Lite version** (`dist/lite/`): Core only, no DeepSeek dependencies |
| 50 | + |
| 51 | +Build process: |
| 52 | +1. Core files copied to `js/` directory |
| 53 | +2. For Main version: DeepSeek background.js and panel.js merged with core versions |
| 54 | +3. DeepSeek module files copied to `modules/deepseek/` |
| 55 | +4. Manifest adjusted based on version |
| 56 | + |
| 57 | +## Key Technical Decisions |
| 58 | + |
| 59 | +1. **Modular architecture**: Core modules in `src/core/`, feature modules in `src/modules/` |
| 60 | +2. **Message bus pattern**: Centralized `message-bus.js` for inter-module communication |
| 61 | +3. **Dual-source API**: Primary `player/wbi/v2?aid=xxx` (more stable), fallback `player/v2?bvid=xxx` |
| 62 | +4. **Subtitle sorting**: By language priority (zh > en > other) with URL pathname tiebreaker (ignoring auth_key) |
| 63 | +5. **CORS**: CDN requests (hdslb.com) use `credentials: 'omit'` |
| 64 | +6. **Icon state**: `chrome.tabs.onUpdated/onActivated` + dimmed PNG variants (black-on-white) |
| 65 | +7. **Auto-scroll**: Subtitle page - 3s pause on user scroll; Doc organize page - pause on scroll up, resume on scroll to bottom |
| 66 | +8. **Event delegation**: Click handlers attached once per container to prevent listener leaks |
| 67 | +9. **Preview dark mode**: Overlay inherits `data-bn-theme` attribute for CSS variable resolution |
| 68 | +10. **Crop viewer**: Cropper.js library for image manipulation, sidebar always visible, icon+text toolbar |
| 69 | +11. **DeepSeek multi-world injection**: MAIN world (deepseek-api.js) for API calls, ISOLATED world (deepseek-bridge.js) for message bridging |
| 70 | +12. **SSE stream parsing**: 7 event formats (fragments, APPEND array/string, path reasoning, thinking type, OpenAI delta, text fallback) |
| 71 | +13. **Request lifecycle**: activeRequestId filtering to discard stale chunks after abort; stop_stream API to terminate server-side generation |
| 72 | +14. **Build system**: Two versions (Main with DeepSeek, Lite without) generated from modular source |
| 73 | + |
| 74 | +## Features |
| 75 | + |
| 76 | +### Core Features (Both Versions) |
| 77 | + |
| 78 | +- 5 tabs: 字幕, 章节, 视频信息, 文档整理, 设置 |
| 79 | +- Chapter/subtitle timestamp toggles in export |
| 80 | +- Export ZIP uses `assets/` directory (not `screenshots/`) |
| 81 | +- Screenshot naming: `bivinote-{bvid}-{MMSS}.png` for download, `{MMSS}.png` for assets |
| 82 | +- Video info checkboxes persisted (including chapterTimestamp, subtitleTimestamp), first 6 default checked |
| 83 | +- Auto-refresh subtitles on BVID change or page param (p=) change |
| 84 | +- Description preserved with line breaks in panel display |
| 85 | +- Collapse to draggable circular icon with quick screenshot menu |
| 86 | +- Screenshot viewer: Cropper.js crop, zoom, rotate, flip, sidebar navigation |
| 87 | +- Frame stepping with flash-free image replacement |
| 88 | +- Settings: restore defaults preserves prompts, default doc mode is auto |
| 89 | + |
| 90 | +### Main Version Features (with DeepSeek) |
| 91 | + |
| 92 | +- DeepSeek AI doc organize: auto mode with streaming think/response display, stop button, continue in DeepSeek |
| 93 | +- Prompt management: full-page editor for 3 prompt types (manual no-image, manual with-image, auto DeepSeek) |
| 94 | + |
| 95 | +## Reference Projects |
| 96 | + |
| 97 | +- [Bilibili-Obsidian-Clipper](https://github.com/haixiong1997/Bilibili-Obsidian-Clipper): Subtitle fetching, API strategy, SRT/Markdown export |
| 98 | +- [BiViShot](https://github.com/hyglgithub/BiViShot): Screenshot capture, frame navigation, clipboard |
| 99 | +- [Cropper.js](https://github.com/fengyuanchen/cropperjs): Image cropping library |
| 100 | +- [DeepSeek-Web-Plugin](https://github.com/user/DeepSeek-Web-Plugin): DeepSeek API integration, SSE parsing, PoW solving, cookie fallback |
| 101 | + |
| 102 | +## Build & Test |
| 103 | + |
| 104 | +### Build |
| 105 | + |
| 106 | +```bash |
| 107 | +# Build both versions |
| 108 | +node scripts/build.js |
| 109 | + |
| 110 | +# Build Main version only (core + DeepSeek) |
| 111 | +node scripts/build.js main |
| 112 | + |
| 113 | +# Build Lite version only (core only) |
| 114 | +node scripts/build.js lite |
| 115 | +``` |
| 116 | + |
| 117 | +Output: |
| 118 | +- `dist/main/` - Main version with DeepSeek AI |
| 119 | +- `dist/lite/` - Lite version without DeepSeek |
| 120 | + |
| 121 | +### Load Extension |
| 122 | + |
| 123 | +1. `chrome://extensions/` → Developer mode → Load unpacked |
| 124 | +2. Select `dist/main/` or `dist/lite/` directory |
| 125 | + |
| 126 | +### Run Tests |
| 127 | + |
| 128 | +```bash |
| 129 | +# Run all tests |
| 130 | +npm test |
| 131 | + |
| 132 | +# Run specific test file |
| 133 | +npm test -- tests/message-bus.test.js |
| 134 | +``` |
| 135 | + |
| 136 | +## Commit Convention |
| 137 | + |
| 138 | +- `feat:` new feature |
| 139 | +- `fix:` bug fix |
| 140 | +- `chore:` version bump, release prep |
| 141 | +- `docs:` documentation |
| 142 | +- Use `Co-Authored-By: Claude <noreply@anthropic.com>` in commit messages |
| 143 | + |
| 144 | +## Release & Deployment |
| 145 | + |
| 146 | +### GitHub Repository Setup (已完成) |
| 147 | + |
| 148 | +首次开源配置步骤: |
| 149 | +1. 清理敏感信息(检查 `.gitignore`) |
| 150 | +2. 提交 `LICENSE` 文件 |
| 151 | +3. 编写高质量 `README.md`,配置仓库 About 信息和 Topics 标签 |
| 152 | +4. 打 Tag 发布第一个 Release(填写 Changelog) |
| 153 | +5. 配置 GitHub Actions 自动部署 Pages |
| 154 | + |
| 155 | +### Version Iteration Workflow |
| 156 | + |
| 157 | +后续版本迭代流程: |
| 158 | + |
| 159 | +#### Step 1: 编写更新日志 |
| 160 | + |
| 161 | +编辑 `CHANGELOG.md`,在顶部添加新版本内容: |
| 162 | + |
| 163 | +```markdown |
| 164 | +## [x.y.z] - YYYY-MM-DD |
| 165 | +### Added |
| 166 | +- 新增功能描述 |
| 167 | + |
| 168 | +### Fixed |
| 169 | +- 修复问题描述 |
| 170 | + |
| 171 | +### Changed |
| 172 | +- 变更描述 |
| 173 | +``` |
| 174 | + |
| 175 | +#### Step 2: 更新版本号 |
| 176 | + |
| 177 | +修改 `manifest.json` 中的 `version` 字段: |
| 178 | + |
| 179 | +```json |
| 180 | +{ |
| 181 | + "version": "x.y.z" |
| 182 | +} |
| 183 | +``` |
| 184 | + |
| 185 | +#### Step 3: 提交并推送 |
| 186 | + |
| 187 | +```bash |
| 188 | +git add CHANGELOG.md manifest.json |
| 189 | +git commit -m "chore: bump version to x.y.z" |
| 190 | +git push origin main |
| 191 | +``` |
| 192 | + |
| 193 | +#### Step 4: 自动发布 |
| 194 | + |
| 195 | +GitHub Actions 自动完成: |
| 196 | +1. 检测 `manifest.json` 变更触发工作流 |
| 197 | +2. 打包扩展为 `bivinote-vx.y.z-chrome.zip` |
| 198 | +3. 从 `CHANGELOG.md` 提取当前版本更新日志 |
| 199 | +4. 创建 Release `BiViNote vx.y.z`,附带格式化的更新说明 |
| 200 | + |
| 201 | +### Release Naming Convention |
| 202 | + |
| 203 | +- Tag: `vx.y.z`(如 `v2.2.0`) |
| 204 | +- Release Name: `BiViNote vx.y.z` |
| 205 | +- Asset: `bivinote-vx.y.z-chrome.zip` |
| 206 | + |
| 207 | +### GitHub Pages |
| 208 | + |
| 209 | +- 部署源:`main` 分支 `/docs` 目录 |
| 210 | +- 访问地址:https://hyglgithub.github.io/BiViNote/ |
| 211 | +- 更新方式:推送到 main 分支自动触发部署 |
| 212 | + |
| 213 | +### Workflow Files |
| 214 | + |
| 215 | +- `.github/workflows/release.yml` - 自动发布工作流 |
| 216 | +- `CHANGELOG.md` - 版本更新日志 |
| 217 | +- `docs/` - GitHub Pages 网站文件 |
0 commit comments