---
name: build-chrome-extension
description: Builds a Manifest V3 browser extension in JavaScript or TypeScript - manifest, service worker, content script, popup, message passing and storage, with the smallest permission set. Use when the user wants a Chrome extension, a browser add-on, or a popup that talks to a content script.
license: CC0-1.0
metadata:
  category: frontend
  tags: "extension,typescript"
  author: johnny-bravo
  version: "1.0"
  updated: "2026-11-11"
  status: published
  aliases: "chrome extension, manifest v3, content script, browser add-on"
  source_url: "https://github.com/PatrickJS/awesome-cursorrules/blob/b044f956f021b6e8877f16781bcfc466a6a120e9/rules/chrome-extension-dev-js-typescript-cursorrules-pro.mdc"
  source_license: CC0-1.0
  copyright: "Public domain (CC0 1.0) - rules contributed to PatrickJS/awesome-cursorrules"
  original_author: "awesome-cursorrules contributors (chrome-extension-dev rules)"
  modified: "Adapted by TheSkillVault: turned a rules file into a build order with an original message type; kept Manifest V3, least privilege, messaging and storage; dropped editor, class-style and full-file output rules; added safety notes, gotchas, portability and done-when."
  imported_on: "2026-11-11"
---

# Build a browser extension

A Manifest V3 extension is a small package. It has a manifest, a service worker, optional content scripts and a popup.

## When to use
- You want a Chrome or Chromium extension with a toolbar popup.
- A page needs a content script that messages the extension.
- You are moving an old Manifest V2 extension to V3.

## When not to use
- You want to inspect a page you did not write. Use debug-with-chrome-devtools.
- You want an automated test of a website. Use test-web-app-with-playwright.

## Steps
1. Start with `manifest.json`. Set `manifest_version` to 3 and name the extension.
2. Put background work in a service worker (`background.service_worker`). It wakes for events. It does not stay running.
3. Add a popup (`action.default_popup`) for the toolbar UI. Keep that page small.
4. Add a content script only for the sites that need it. List those hosts in `matches`. Do not use `<all_urls>` unless you must.
5. Ask for the smallest permissions. Prefer optional permissions for anything you can request later.
6. Pass messages with `chrome.runtime.sendMessage`. Give each message a TypeScript interface.
7. Store settings with `chrome.storage`. Do not put passwords or tokens in the package.
8. Schedule work with `chrome.alarms`, not `setInterval`. The worker can stop at any time.
9. Load the unpacked folder from the browser extensions page. When a message fails, read the worker error.

```ts
type SaveNote = { type: "save-note"; text: string };

chrome.runtime.onMessage.addListener((message: SaveNote, _sender, sendResponse) => {
  if (message.type !== "save-note") return;
  chrome.storage.local.set({ note: message.text }).then(() => sendResponse({ ok: true }));
  return true;
});
```

## Gotchas
- Manifest V2 background pages are gone. A timer in the worker will not keep running.
- A content script does not share the page's JavaScript world. Pass data with messages.
- A wide host pattern will scare users and the store review. Narrow `matches`.
- Set a content security policy. Load scripts from the package. Do not run remote code. Use HTTPS for network calls.
- Treat page text and user input as untrusted. Escape anything you insert into the page.
- Never ship an API key or a password inside the extension folder.

## Portability
The same package works in Chrome and other Chromium browsers. Firefox and Safari speak the WebExtensions API, with small differences. If you are using another browser, check that your calls exist on both `chrome` and `browser`. This is not a desktop editor app.

## Example prompt
"Build a Manifest V3 Chrome extension with a popup that saves a note and a content script that reads the page title."

## Done when
- The manifest is version 3 and lists only the permissions you use.
- The popup, the worker and the content script pass typed messages.
- No secret is stored in the package, and the unpacked extension loads.

---
Based on the Chrome extension JavaScript and TypeScript rules file in PatrickJS/awesome-cursorrules (CC0 1.0, public domain). See LICENSE.txt.
