{
  "category": "browser_extension",
  "title": "Chrome Extension Development",
  "description": "Best practices for modern Chrome Extension development using TypeScript and Manifest V3",
  "recommended_globs": ["**/*.{js,jsx,ts,tsx}", "**/manifest.json", "**/*.html", "**/*.css"],
  "content": [
    "# Chrome Extension Development",
    "",
    "// Description: Best practices for modern Chrome Extension development using TypeScript and Manifest V3",
    "// Recommended Globs: **/*.{js,jsx,ts,tsx}, **/manifest.json, **/*.html, **/*.css",
    "",
    "## Project Structure",
    "```",
    "extension/",
    "├── manifest.json",
    "├── src/",
    "│   ├── background/",
    "│   │   └── service_worker.ts",
    "│   ├── content/",
    "│   │   └── content_script.ts",
    "│   ├── popup/",
    "│   │   ├── Popup.tsx",
    "│   │   └── index.html",
    "│   └── options/",
    "│       ├── Options.tsx",
    "│       └── index.html",
    "├── public/",
    "│   └── assets/",
    "└── dist/",
    "```",
    "",
    "## Code Style and Structure",
    "- Write concise, technical JavaScript/TypeScript code",
    "- Use modern JavaScript features and best practices",
    "- Prefer functional programming patterns over classes",
    "- Use descriptive variable names:",
    "```typescript",
    "// Good",
    "const isExtensionEnabled = true;",
    "const hasRequiredPermissions = await checkPermissions();",
    "",
    "// Bad",
    "const enabled = true;",
    "const check = await verify();",
    "```",
    "",
    "## Naming Conventions",
    "- File names: lowercase with underscores",
    "  - `content_script.ts`",
    "  - `background_worker.ts`",
    "  - `popup_view.tsx`",
    "- Variables/Functions: camelCase",
    "- Classes/Components: PascalCase",
    "",
    "## TypeScript Usage",
    "```typescript",
    "// Message interfaces",
    "interface ExtensionMessage {",
    "  type: 'UPDATE' | 'DELETE' | 'FETCH';",
    "  payload: unknown;",
    "}",
    "",
    "// Type guards",
    "function isUpdateMessage(msg: ExtensionMessage): msg is ExtensionMessage & { type: 'UPDATE' } {",
    "  return msg.type === 'UPDATE';",
    "}",
    "```",
    "",
    "## Extension Architecture",
    "### Message Passing",
    "```typescript",
    "// In content script",
    "chrome.runtime.sendMessage<ExtensionMessage>({",
    "  type: 'UPDATE',",
    "  payload: { data: 'value' }",
    "});",
    "",
    "// In background worker",
    "chrome.runtime.onMessage.addListener((message: ExtensionMessage, sender, sendResponse) => {",
    "  if (isUpdateMessage(message)) {",
    "    // Handle update",
    "    sendResponse({ success: true });",
    "  }",
    "});",
    "```",
    "",
    "## Manifest V3 Example",
    "```json",
    "{",
    "  \"manifest_version\": 3,",
    "  \"name\": \"Extension Name\",",
    "  \"version\": \"1.0.0\",",
    "  \"description\": \"Extension description\",",
    "  \"permissions\": [",
    "    \"storage\",",
    "    \"activeTab\"",
    "  ],",
    "  \"host_permissions\": [",
    "    \"https://*.example.com/*\"",
    "  ],",
    "  \"background\": {",
    "    \"service_worker\": \"background/service_worker.js\"",
    "  },",
    "  \"content_scripts\": [{",
    "    \"matches\": [\"https://*.example.com/*\"],",
    "    \"js\": [\"content/content_script.js\"]",
    "  }],",
    "  \"action\": {",
    "    \"default_popup\": \"popup/index.html\"",
    "  }",
    "}",
    "```",
    "",
    "## Security Best Practices",
    "1. Content Security Policy",
    "```json",
    "\"content_security_policy\": {",
    "  \"extension_pages\": \"script-src 'self'; object-src 'self'\"",
    "}",
    "```",
    "",
    "2. Input Sanitization",
    "```typescript",
    "function sanitizeInput(input: string): string {",
    "  return input.replace(/[<>&\"']/g, (char) => {",
    "    const entities: Record<string, string> = {",
    "      '<': '&lt;',",
    "      '>': '&gt;',",
    "      '&': '&amp;',",
    "      '\"': '&quot;',",
    "      \"'\": '&#x27;'",
    "    };",
    "    return entities[char];",
    "  });",
    "}",
    "```",
    "",
    "## Performance Optimization",
    "- Use event pages over persistent background pages",
    "- Implement lazy loading:",
    "```typescript",
    "// Lazy load feature module",
    "const loadFeature = async () => {",
    "  const { feature } = await import('./feature.js');",
    "  return feature;",
    "};",
    "",
    "// Use when needed",
    "if (shouldLoadFeature) {",
    "  const feature = await loadFeature();",
    "  feature.initialize();",
    "}",
    "```",
    "",
    "## Testing and Debugging",
    "1. Load unpacked extension:",
    "   - Chrome Menu → Extensions → Developer mode → Load unpacked",
    "   - Select the `dist` directory",
    "",
    "2. Debug with Chrome DevTools:",
    "   - Background: Click 'inspect' in extension page",
    "   - Popup: Right-click extension icon → Inspect popup",
    "   - Content Script: Regular page DevTools → Sources → Content Scripts",
    "",
    "## Cross-browser Compatibility",
    "```typescript",
    "// Use WebExtension types for cross-browser support",
    "declare namespace browser {",
    "  const storage: typeof chrome.storage;",
    "  const runtime: typeof chrome.runtime;",
    "}",
    "",
    "// Use polyfill",
    "const browserAPI = typeof browser !== 'undefined' ? browser : chrome;",
    "```",
    "",
    "## Resources",
    "- [Chrome Extension Documentation](https://developer.chrome.com/docs/extensions/)",
    "- [Manifest V3 Migration Guide](https://developer.chrome.com/docs/extensions/mv3/intro/)",
    "- [Chrome Extension Security Guide](https://developer.chrome.com/docs/extensions/mv3/security/)"
  ]
}
