-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.cursorrules
More file actions
725 lines (608 loc) · 17 KB
/
.cursorrules
File metadata and controls
725 lines (608 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
# Chrome Extension - Cursor AI Rules
## Technology Stack
- **Platform**: Chrome Extensions Manifest V3
- **Languages**: JavaScript (ES6+), HTML5, CSS3
- **UI Library**: Vanilla JS (no framework overhead)
- **Charting**: Chart.js
- **Backend Communication**: REST API calls
## JavaScript Code Style
### Modern ES6+ Patterns
```javascript
// Use const/let, never var
const API_URL = 'https://api.tubefocus.com';
let userScore = 0;
// Arrow functions for callbacks
videos.forEach(video => processVideo(video));
// Destructuring
const { videoId, title, channel } = videoData;
// Template literals
const message = `Score: ${score}/100`;
// Async/await over promises
async function fetchScore(videoId) {
try {
const response = await fetch(`${API_URL}/score/${videoId}`);
return await response.json();
} catch (error) {
console.error('Failed to fetch score:', error);
return null;
}
}
```
### JSDoc Documentation
```javascript
/**
* Calculate productivity score for a YouTube video
* @param {string} videoId - YouTube video identifier
* @param {Object} options - Scoring options
* @param {boolean} options.useCache - Whether to use cached results
* @returns {Promise<number>} Productivity score (0-100)
* @throws {Error} If video ID is invalid
*/
async function calculateScore(videoId, options = {}) {
// Implementation
}
```
## Chrome Extension Architecture
### File Structure & Responsibilities
#### manifest.json
- Keep permissions minimal (security)
- Use Manifest V3 (required)
- Specify content security policy
- Document why each permission is needed
#### background.js (Service Worker)
```javascript
// Background script for event handling
// - Manages extension lifecycle
// - Handles cross-tab communication
// - Caches API responses
// - Manages alarms/timers
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === 'install') {
// First-time setup
}
});
```
#### content.js (Content Script)
```javascript
// Injected into YouTube pages
// - DOM manipulation
// - Video detection
// - Score display
// - User interaction capture
// Isolate from page scripts
(function() {
'use strict';
// Your content script code
})();
```
#### popup.js (Extension Popup)
```javascript
// Popup UI logic
// - Display user statistics
// - Settings management
// - Quick actions
// - Chart visualization
document.addEventListener('DOMContentLoaded', async () => {
await loadUserData();
initializeCharts();
});
```
### Message Passing
```javascript
// Content script -> Background
chrome.runtime.sendMessage({
action: 'getScore',
videoId: currentVideoId
}, (response) => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
return;
}
displayScore(response.score);
});
// Background listener
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'getScore') {
fetchScoreFromAPI(request.videoId)
.then(sendResponse)
.catch(error => sendResponse({ error: error.message }));
return true; // Async response
}
});
```
## API Integration
### Backend Communication
```javascript
class TubeFocusAPI {
constructor(baseURL) {
this.baseURL = baseURL;
this.cache = new Map();
}
async request(endpoint, options = {}) {
const url = `${this.baseURL}${endpoint}`;
const config = {
headers: {
'Content-Type': 'application/json',
...options.headers
},
...options
};
try {
const response = await fetch(url, config);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
return await response.json();
} catch (error) {
console.error(`API request failed: ${error.message}`);
throw error;
}
}
async getVideoScore(videoId) {
// Check cache first
if (this.cache.has(videoId)) {
const cached = this.cache.get(videoId);
if (Date.now() - cached.timestamp < 3600000) { // 1 hour
return cached.data;
}
}
const data = await this.request(`/api/v1/score/${videoId}`);
this.cache.set(videoId, { data, timestamp: Date.now() });
return data;
}
}
```
### Error Handling
```javascript
// Always handle network failures gracefully
async function fetchWithRetry(url, options = {}, maxRetries = 3) {
for (let i = 0; i < maxRetries; i++) {
try {
return await fetch(url, options);
} catch (error) {
if (i === maxRetries - 1) throw error;
await sleep(Math.pow(2, i) * 1000); // Exponential backoff
}
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
```
## DOM Manipulation Best Practices
### YouTube Page Integration
```javascript
// Wait for YouTube elements to load
function waitForElement(selector, timeout = 5000) {
return new Promise((resolve, reject) => {
const element = document.querySelector(selector);
if (element) return resolve(element);
const observer = new MutationObserver(() => {
const element = document.querySelector(selector);
if (element) {
observer.disconnect();
resolve(element);
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
setTimeout(() => {
observer.disconnect();
reject(new Error('Element not found'));
}, timeout);
});
}
// Extract video ID from URL
function getVideoId() {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get('v');
}
// Inject score UI
function injectScoreDisplay(score) {
const container = document.querySelector('#above-the-fold');
if (!container) return;
const scoreElement = document.createElement('div');
scoreElement.id = 'tubefocus-score';
scoreElement.className = 'tubefocus-score-container';
scoreElement.innerHTML = `
<div class="score-badge">
<span class="score-value">${score}</span>
<span class="score-label">Productivity Score</span>
</div>
`;
container.appendChild(scoreElement);
}
```
### Clean Up Resources
```javascript
// Remove listeners when done
const controller = new AbortController();
document.addEventListener('click', handleClick, {
signal: controller.signal
});
// Later: controller.abort();
// Clean up on navigation
window.addEventListener('beforeunload', () => {
// Clean up resources
controller.abort();
clearCaches();
});
```
## Storage Management
### Chrome Storage API
```javascript
// Save settings
async function saveSettings(settings) {
await chrome.storage.sync.set({ settings });
}
// Load settings with defaults
async function loadSettings() {
const defaults = {
enabled: true,
scoreThreshold: 70,
showNotifications: true
};
const { settings } = await chrome.storage.sync.get({ settings: defaults });
return settings;
}
// Listen for storage changes
chrome.storage.onChanged.addListener((changes, namespace) => {
if (namespace === 'sync' && changes.settings) {
updateUIWithNewSettings(changes.settings.newValue);
}
});
```
## Performance Optimization
### Lazy Loading
```javascript
// Load heavy libraries only when needed
let chartJS = null;
async function loadChartJS() {
if (!chartJS) {
chartJS = await import('./libs/chart.min.js');
}
return chartJS;
}
```
### Debouncing & Throttling
```javascript
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
clearTimeout(timeout);
timeout = setTimeout(() => func(...args), wait);
};
}
function throttle(func, limit) {
let inThrottle;
return function(...args) {
if (!inThrottle) {
func(...args);
inThrottle = true;
setTimeout(() => inThrottle = false, limit);
}
};
}
// Usage
const handleScroll = debounce(() => {
// Handle scroll event
}, 300);
window.addEventListener('scroll', handleScroll);
```
### Efficient DOM Updates
```javascript
// Batch DOM updates
function updateVideoList(videos) {
const fragment = document.createDocumentFragment();
videos.forEach(video => {
const element = createVideoElement(video);
fragment.appendChild(element);
});
document.querySelector('#video-list').appendChild(fragment);
}
```
## UI/UX Best Practices
### Loading States
```javascript
function showLoading(element) {
element.classList.add('loading');
element.innerHTML = '<div class="spinner"></div>';
}
function hideLoading(element, content) {
element.classList.remove('loading');
element.innerHTML = content;
}
```
### Error Messages
```javascript
function showError(message, duration = 5000) {
const errorDiv = document.createElement('div');
errorDiv.className = 'tubefocus-error';
errorDiv.textContent = message;
document.body.appendChild(errorDiv);
setTimeout(() => {
errorDiv.classList.add('fade-out');
setTimeout(() => errorDiv.remove(), 300);
}, duration);
}
```
### Accessibility
```html
<!-- Use semantic HTML -->
<button
aria-label="Show productivity score"
role="button"
tabindex="0">
Score
</button>
<!-- Keyboard navigation -->
<script>
element.addEventListener('keydown', (e) => {
if (e.key === 'Enter' || e.key === ' ') {
handleClick();
}
});
</script>
```
## Security Best Practices
### Content Security Policy
```json
// manifest.json
{
"content_security_policy": {
"extension_pages": "script-src 'self'; object-src 'self'"
}
}
```
### Input Sanitization
```javascript
function sanitizeHTML(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// Use textContent instead of innerHTML when possible
element.textContent = userInput; // Safe
// element.innerHTML = userInput; // Dangerous
```
### API Key Management
```javascript
// Never hardcode API keys in extension code
// Use environment-specific config files
// Backend should handle authentication
// config.js (gitignored local dev file)
const config = {
apiUrl: process.env.NODE_ENV === 'production'
? 'https://api.tubefocus.com'
: 'http://localhost:5000'
};
```
## Testing Guidelines
### Unit Tests
```javascript
// test_local_dev.js
describe('Score Calculation', () => {
it('should return score between 0-100', () => {
const score = calculateScore(mockData);
expect(score).toBeGreaterThanOrEqual(0);
expect(score).toBeLessThanOrEqual(100);
});
});
```
### Manual Testing Checklist
- [ ] Test on YouTube homepage
- [ ] Test on video watch page
- [ ] Test on channel pages
- [ ] Test with slow network (throttling)
- [ ] Test with backend offline
- [ ] Test extension enable/disable
- [ ] Test storage quota limits
- [ ] Test on different screen sizes
## Chart.js Integration
### Initialization
```javascript
async function initializeChart(data) {
const ctx = document.getElementById('scoreChart').getContext('2d');
new Chart(ctx, {
type: 'line',
data: {
labels: data.dates,
datasets: [{
label: 'Productivity Score',
data: data.scores,
borderColor: '#4CAF50',
backgroundColor: 'rgba(76, 175, 80, 0.1)',
tension: 0.4
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: { display: true },
tooltip: { enabled: true }
}
}
});
}
```
## Debugging Tips
### Console Logging
```javascript
// Use different log levels
console.debug('Detailed debug info');
console.log('General information');
console.warn('Warning message');
console.error('Error occurred', error);
// Group related logs
console.group('Video Processing');
console.log('Video ID:', videoId);
console.log('Score:', score);
console.groupEnd();
```
### Chrome DevTools
```javascript
// Debug background script: chrome://extensions > Inspect background page
// Debug content script: Regular DevTools on YouTube page
// Debug popup: Right-click popup > Inspect
// Use debugger statement for breakpoints
if (someCondition) {
debugger; // Execution will pause here
}
```
## Git Workflow for Extension
### MANDATORY: Feature Branch Workflow
**NEVER work directly on main branch**
#### Branch Creation
```bash
# Always create feature branch from main
git checkout main
git pull origin main
git checkout -b feature/your-feature-name
```
#### Development Cycle with Version Management
1. Create feature branch
2. Make changes and test locally
3. Update `manifest.json` version if user-facing change
4. Create changelog if significant change
5. Commit with conventional message format
6. Push branch to remote
7. Create Pull Request for review
8. Merge to main after approval
9. Delete feature branch
#### Manifest Version Management
**When to Update Version:**
- ✅ New features → Minor version (1.0.0 → 1.1.0)
- ✅ Bug fixes → Patch version (1.0.0 → 1.0.1)
- ✅ Major changes → Major version (1.0.0 → 2.0.0)
- ❌ Code refactoring (no user impact) → No change
- ❌ Documentation only → No change
**Version Format:** `major.minor.patch`
#### Before Committing
- [ ] Test extension functionality
- [ ] Update `manifest.json` version (if needed)
- [ ] Remove debug console.log statements
- [ ] Verify no hardcoded secrets
- [ ] Create changelog (if significant change)
- [ ] Format commit message conventionally
#### Commit Message Format
```bash
# Format: type(scope): subject
git commit -m "feat(ui): add dark mode support
- Added dark mode toggle in popup
- Updated styles for dark theme
- Updated manifest version to 1.1.0
- See changelogs/2026-01-22-dark-mode.md
Closes #45"
```
**Commit Types:**
- `feat:` new features
- `fix:` bug fixes
- `ui:` UI/UX improvements
- `refactor:` code refactoring
- `perf:` performance improvements
- `security:` security fixes
- `docs:` documentation
- `chore:` maintenance
## Changelog Requirements
### Create Changelog For
- ✅ New features
- ✅ UI/UX changes
- ✅ Manifest updates
- ✅ Permission changes
- ✅ API integration changes
- ✅ Performance improvements (>10%)
- ✅ Security fixes
- ✅ Chrome Web Store releases
### Changelog Location
`extension/changelogs/YYYY-MM-DD-brief-description.md`
### Changelog Must Include
```markdown
# Feature Name
**Type:** Feature | Fix | UI/UX | Performance | Security
**Date:** YYYY-MM-DD
**Author:** Your Name
**Branch:** feature/branch-name
**Manifest Version:** X.X.X
## Summary
Brief description
## Changes Made
- Change 1
- Change 2
## User Impact
- What users will notice
- New features
## Testing
- How tested
- Browser versions
## Screenshots
[If UI changes]
```
### Integration with Git
```bash
# After creating changelog and updating manifest
git add manifest.json
git add changelogs/2026-01-22-feature.md
git add [other files]
git commit -m "feat: implement feature
- Updated manifest version to 1.1.0
- See changelogs/2026-01-22-feature.md"
```
## Deployment Checklist
Before submitting to Chrome Web Store:
- [ ] Update version in manifest.json
- [ ] Create/update changelog
- [ ] Test on clean Chrome profile
- [ ] Remove console.log statements (except errors)
- [ ] Minify CSS/JS (optional)
- [ ] Optimize images
- [ ] Update README.md
- [ ] Test with production backend
- [ ] Review permissions (minimal required)
- [ ] Prepare store assets (screenshots, description)
- [ ] Test on multiple YouTube layouts
- [ ] Create git tag: `git tag v1.1.0`
- [ ] Push tag: `git push origin v1.1.0`
## MCP Usage for Extension Development
### Figma MCP
- Reference UI designs and mockups
- Ensure visual consistency
- Extract design tokens (colors, spacing)
### Context7 MCP
- Look up Chrome Extension API documentation
- Check best practices
- Research YouTube DOM structure changes
## Browser Compatibility
### Chrome Version Support
```json
// manifest.json
{
"minimum_chrome_version": "88"
}
```
### Feature Detection
```javascript
// Check for API availability
if (chrome.storage && chrome.storage.sync) {
// Use sync storage
} else {
// Fallback to local storage
}
```
## Code Review Checklist (Before PR)
1. [ ] Message passing correctness
2. [ ] No memory leaks (listeners, timers cleaned up)
3. [ ] Error handling completeness
4. [ ] Performance impact on YouTube minimal
5. [ ] Security vulnerabilities checked
6. [ ] User experience polished
7. [ ] Code documented (JSDoc)
8. [ ] Manifest permissions justified (minimal)
9. [ ] Manifest version updated (if needed)
10. [ ] Changelog created (if significant)
11. [ ] Conventional commit message
12. [ ] Branch up to date with main
13. [ ] No merge conflicts
14. [ ] Tested on clean Chrome profile