|
| 1 | +# Douban Library Extension Architecture |
| 2 | + |
| 3 | +## Overview |
| 4 | +This Edge extension enhances Douban book detail pages by adding library borrowing functionality. It checks if books are available in the Beijing Library (bjyth.jiatu.cloud) and adds a "Borrow" button when available. |
| 5 | + |
| 6 | +## Extension Workflow |
| 7 | + |
| 8 | +```mermaid |
| 9 | +flowchart TD |
| 10 | + A[User visits Douban book page] --> B[Content script activates] |
| 11 | + B --> C[Extract ISBN from page] |
| 12 | + C --> D{ISBN found?} |
| 13 | + D -->|No| E[Exit - No action] |
| 14 | + D -->|Yes| F[Send ISBN to background script] |
| 15 | + F --> G[Background script queries library] |
| 16 | + G --> H[Parse HTML response] |
| 17 | + H --> I{Book available?} |
| 18 | + I -->|No| J[No button added] |
| 19 | + I -->|Yes| K[Inject Borrow button] |
| 20 | + K --> L[User clicks button] |
| 21 | + L --> M[Open library page in new tab] |
| 22 | +``` |
| 23 | + |
| 24 | +## Component Structure |
| 25 | + |
| 26 | +``` |
| 27 | +ext/ |
| 28 | +├── manifest.json # Extension configuration |
| 29 | +├── content.js # Runs on Douban pages |
| 30 | +├── background.js # Handles cross-origin requests |
| 31 | +├── styles.css # Button styling |
| 32 | +├── icons/ # Extension icons |
| 33 | +│ ├── icon-16.png |
| 34 | +│ ├── icon-48.png |
| 35 | +│ └── icon-128.png |
| 36 | +├── package.json # Development dependencies |
| 37 | +└── README.md # User documentation |
| 38 | +``` |
| 39 | + |
| 40 | +## Key Components |
| 41 | + |
| 42 | +### 1. Content Script (content.js) |
| 43 | +- **Runs on**: `https://book.douban.com/subject/*` |
| 44 | +- **Responsibilities**: |
| 45 | + - Extract ISBN from book detail page |
| 46 | + - Send ISBN to background script |
| 47 | + - Inject "Borrow" button when book is available |
| 48 | + - Handle button click events |
| 49 | + |
| 50 | +### 2. Background Script (background.js) |
| 51 | +- **Purpose**: Handle cross-origin requests (CORS bypass) |
| 52 | +- **Responsibilities**: |
| 53 | + - Receive ISBN from content script |
| 54 | + - Query library search API |
| 55 | + - Parse HTML response |
| 56 | + - Return availability status |
| 57 | + |
| 58 | +### 3. Manifest Configuration |
| 59 | +- **Permissions needed**: |
| 60 | + - `https://book.douban.com/*` (content script injection) |
| 61 | + - `https://bjyth.jiatu.cloud/*` (library API access) |
| 62 | + - `activeTab` (interact with current tab) |
| 63 | + - `storage` (optional, for caching) |
| 64 | + |
| 65 | +## Data Flow |
| 66 | + |
| 67 | +1. **ISBN Extraction**: |
| 68 | + - Look for ISBN in book info section |
| 69 | + - Format: ISBN-10 or ISBN-13 |
| 70 | + - Location: Usually under "书籍信息" section |
| 71 | + |
| 72 | +2. **Library Query**: |
| 73 | + - URL: `https://bjyth.jiatu.cloud/yuntu-pc/home/search/index?word={ISBN}` |
| 74 | + - Method: GET request via background script |
| 75 | + - Response: HTML page with search results |
| 76 | + |
| 77 | +3. **Availability Check**: |
| 78 | + - Parse HTML for book status |
| 79 | + - Look for availability indicators |
| 80 | + - Common patterns: "可借", "在架", "available" |
| 81 | + |
| 82 | +4. **Button Injection**: |
| 83 | + - Location: Next to ISBN number |
| 84 | + - Style: Blue button (#2E7FBE) |
| 85 | + - Text: "图书馆借阅" or "Borrow" |
| 86 | + - Action: Opens library page in new tab |
| 87 | + |
| 88 | +## Technical Considerations |
| 89 | + |
| 90 | +1. **CORS Handling**: |
| 91 | + - Library site may block cross-origin requests |
| 92 | + - Solution: Use background script as proxy |
| 93 | + |
| 94 | +2. **Page Structure Changes**: |
| 95 | + - Douban may update their HTML structure |
| 96 | + - Use robust selectors and fallbacks |
| 97 | + |
| 98 | +3. **Performance**: |
| 99 | + - Cache library queries (optional enhancement) |
| 100 | + - Debounce multiple requests |
| 101 | + |
| 102 | +4. **Error Handling**: |
| 103 | + - Network failures |
| 104 | + - ISBN not found |
| 105 | + - Library site changes |
| 106 | + |
| 107 | +## Security Considerations |
| 108 | + |
| 109 | +- Only inject scripts on trusted domains |
| 110 | +- Validate ISBN format before querying |
| 111 | +- Sanitize any HTML content from library response |
| 112 | +- Use Content Security Policy in manifest |
| 113 | + |
| 114 | +## Testing Strategy |
| 115 | + |
| 116 | +1. Test with various ISBN formats |
| 117 | +2. Test with books available/unavailable |
| 118 | +3. Test network error scenarios |
| 119 | +4. Test on different Douban page layouts |
| 120 | +5. Verify button styling consistency |
| 121 | + |
| 122 | +## Future Enhancements (Out of scope for MVP) |
| 123 | + |
| 124 | +- Support multiple library systems |
| 125 | +- Show real-time availability count |
| 126 | +- Add reservation functionality |
| 127 | +- Cache queries for performance |
| 128 | +- User preferences/settings page |
0 commit comments