<!-- skill: Chrome Extension | version: 1.0.0 | description: Build Manifest V3 Chrome extensions — popups, content scripts, background workers | category: Web -->

## Instructions
- Target Manifest V3 only (`"manifest_version": 3`). V2 is dead.
- Keep it minimal: `manifest.json` + popup (`popup.html`/`popup.js`) + optional content script + service worker (`background.js`).
- Request the narrowest permissions possible (`activeTab` beats `<all_urls>`); declare host permissions separately.
- Content scripts can't use most chrome APIs — message the service worker with `chrome.runtime.sendMessage`.
- Service workers are ephemeral: no globals, persist state in `chrome.storage.local`.
- Test via `chrome://extensions` → Developer mode → Load unpacked. Pack (`.zip` of the folder) for the Web Store.

## Examples
- "Copy page title" button: popup reads `chrome.tabs.query({active:true})` and copies `tab.title`.
- Reader mode: content script strips everything except `<article>` and restyles it.

## Workflows
1. Scaffold `manifest.json`, icons (16/48/128 PNG), popup files.
2. Implement popup UI first (plain HTML/CSS/JS, no build step needed for small extensions).
3. Add content script + message passing if page interaction is needed.
4. Test unpacked, check `chrome://extensions/errors`, tighten permissions.
5. Zip the folder → publish ($5 developer signup) or distribute unpacked.

## Templates
```json
// manifest.json — minimal MV3
{
  "manifest_version": 3,
  "name": "My Tool",
  "version": "1.0.0",
  "description": "Does one thing well.",
  "action": { "default_popup": "popup.html" },
  "permissions": ["activeTab", "storage"],
  "background": { "service_worker": "background.js" }
}
```
```js
// popup.js — talk to the active tab
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
chrome.scripting.executeScript({ target: { tabId: tab.id }, func: () => document.title }, (res) => {
  document.body.textContent = res[0].result;
});
```

## Tools
- Needs `scripting` permission for the `executeScript` pattern above.
- Use SARAH `file_write` for one file per concern; validate JSON with `python -m json.tool`.

## Advanced
- Side panel (`sidePanel` permission + `chrome.sidePanel.open`) for persistent UIs that outlive popups.
- Network control: `declarativeNetRequest` for adblock-style rules (no host permission needed for listed rulesets).
- Storage: `chrome.storage.sync` (100KB, all devices) vs `.local` (10MB, this machine); `onChanged` listeners keep popup/options in sync.
- Service worker lifetime: no `setInterval` reliance — use `chrome.alarms` for periodic work; long connections via `chrome.runtime.connect` ports.
- Publishing checklist: 128px icon, screenshots 1280x800, privacy policy URL if handling data, staged rollout %, monitor Web Store reviews + crash reports.
- Troubleshooting: worker goes inactive → expected, use alarms/events; CORS in content script → route fetches through the worker; `Could not establish connection` → tab has no content script (chrome:// pages are off-limits).

## Documentation
- MV3 docs: https://developer.chrome.com/docs/extensions
- API reference: https://developer.chrome.com/docs/extensions/reference
