# Add Dark Mode to a Chrome Extension Popup

Yes. An agent adds light, dark and system themes to your extension's popup and options pages, saves the choice in chrome.storage.sync, tests it and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-dark-mode/chrome-extension/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads how the pages are built before it styles them. In an extension shaped like chibat/chrome-extension-typescript-starter, `public/manifest.json` is Manifest V3 with an `action` popup at `popup.html`, an `options_ui` page at `options.html`, a background service worker at `js/background.js`, a content script on `<all_urls>` and the `storage` permission. `webpack/webpack.common.js` bundles `src/popup.tsx`, `src/options.tsx`, `src/background.ts` and `src/content_script.tsx` into `dist/js`, splits shared code into `vendor.js`, and copies everything in `public/` to `dist/`. `package.json` pins `react ^18.2.0`, `typescript ^5.0.4`, `webpack ^5.94.0`, `jest ^29.5.0` with `ts-jest`, and `@types/chrome 0.0.158`; `npm run build` is the production webpack build and `npm test` runs Jest over `src/` with `tsconfig.test.json`. Neither `popup.html` nor `options.html` links a stylesheet: the popup's only styling is an inline `minWidth: 700px` in `src/popup.tsx`.

The theme is three files and two edits. `src/theme.ts` exports a `Theme` type of `"system"`, `"light"` and `"dark"`, a `resolveTheme(preference, prefersDark)` function that turns the preference and the `prefers-color-scheme` result into the effective theme, an `applyTheme(root, theme)` that sets `data-theme` on the document element, and a `loadTheme` and `saveTheme` pair over `chrome.storage.sync` under a `theme` key, next to the `favoriteColor` and `likesColor` keys `src/options.tsx` already stores. `public/theme.css` defines the colours as custom properties on `:root`, overrides them under `[data-theme="dark"]`, and makes `system` follow the OS with a `prefers-color-scheme: dark` media query. `popup.html` and `options.html` get a `<link>` to `theme.css`, which the existing `CopyPlugin` pattern ships to `dist/` with no webpack change.

`src/options.tsx` gains a `Theme` select beside the colour select, saved by the same `saveOptions` call so the `Options saved.` status covers it, and both pages read the preference on mount and apply it before the first paint settles. The popup also subscribes to `chrome.storage.onChanged` so a change on the options page is reflected the next time the popup opens without a reload. The `background.ts` polling loop and the content script that recolours a tab's body on message are left alone; the content script's colour belongs to the page, not the extension's own UI, and the pull request says so.

Tests follow `src/__tests__/sum.ts`, which the Jest config finds under `src` and runs with `ts-jest`. `src/__tests__/theme.ts` covers `resolveTheme` for all six combinations of preference and OS setting, and `applyTheme` against a minimal object with a `setAttribute`, so no DOM environment or `chrome` global is needed. The agent runs `npm test` and `npm run build` in the sandbox, lists `dist/` to show `theme.css`, `popup.html`, `options.html` and the four bundles are present, and pastes the output. The repository's `.github/workflows/build.yml` runs the same two commands on Node 20 and 22 for the pull request.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "codex",
  "input": "Add dark mode to this Chrome extension and open a pull request. Read public/manifest.json, public/popup.html, public/options.html, src/popup.tsx, src/options.tsx, src/background.ts, src/content_script.tsx, webpack/webpack.common.js, jest.config.js, tsconfig.json and package.json first. Write src/theme.ts with a Theme type of system, light and dark, a pure resolveTheme(preference, prefersDark), an applyTheme that sets data-theme on the document element, and loadTheme and saveTheme over chrome.storage.sync under a theme key. Write public/theme.css with colour custom properties, a dark override on [data-theme=dark] and a prefers-color-scheme media query for system, and link it from popup.html and options.html; the existing copy plugin ships it. Add a Theme select to the options page saved with the other options, apply the theme on mount in both pages, and have the popup follow chrome.storage.onChanged. Leave background.ts and the content script unchanged and say why. Add Jest tests for resolveTheme and applyTheme that need no DOM or chrome global. Run npm test and npm run build, list dist/, and paste the output in the pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request with `src/theme.ts`, `public/theme.css`, a test file under `src/__tests__/`, a `<link>` line in each of `popup.html` and `options.html`, and edits to `src/popup.tsx` and `src/options.tsx` for the select, the mount-time apply and the storage listener. The description shows the Jest summary and the webpack build output from the sandbox and the `dist/` listing with the stylesheet in place.

The repository's `build` workflow runs `npm ci`, `npm run build` and `npm test` on Node 20 and 22 for the pull request, so the checks on it are the same commands the agent ran. The manifest does not change, because the `storage` permission is already declared and no new page is added.

A reviewer loads `dist/` as an unpacked extension, opens the options page, switches the theme and reopens the popup, then checks that `system` tracks the OS setting and that the colour options still save. Then they merge.

## Run it on every theme request

A team shipping several extensions from one design system starts one run per repository from the console or with a `POST /o/{org}/sessions` that names the repository in `resources`. Everyone in the org can open the run and read it as it works, and its starter or an org admin can send a follow-up when the first screenshot of the dark popup needs a different contrast, so the palette is settled in the run rather than in review rounds.

Each run reads its own `webpack` config and pages, so an extension that already has a stylesheet gets the variables added to it rather than a second file, and each run opens its own pull request.

## Why run it on Rush?

Dark mode in an extension touches two HTML pages, a bundler config, the options store and the popup's lifecycle, and the usual outcome of doing it quickly is a popup that flashes light before it reads the preference. In a Rush sandbox the agent reads the whole build before it writes, keeps the logic in a pure function with tests, and the build output and the `dist/` listing are kept in the run's record beside the diff.

Because a theme is a judgement call as much as a change, the run is something the team can watch and steer: a follow-up changes the dark palette or asks for the content script to follow too, and the same request then works unchanged on the next extension.

## Frequently asked questions

### Does the manifest change?

No. The `storage` permission is already declared for the colour options, and the theme is stored under the same `chrome.storage.sync` API. No new page, script or permission is added.

### Why not restyle the content script too?

The content script changes the background of the page the user is on when the popup sends a message; that colour belongs to the page, not to the extension's UI. The pull request leaves it alone and says a theme for injected UI would be a separate change.

### How is it tested without a browser?

`resolveTheme` is a pure function and `applyTheme` takes any object with `setAttribute`, so Jest runs them under its default Node environment with the existing `ts-jest` setup. The browser check is loading `dist/` unpacked, which a reviewer does before merging.

### Will the popup update when the option changes?

Yes. The popup listens to `chrome.storage.onChanged` for the `theme` key while it is open and reads the stored value on mount, so a change on the options page is applied the next time the popup opens or at once if it is already open.

## More Chrome Extension jobs

- [Add Dependabot to a Chrome Extension Repository](https://getrush.ai/run/add-dependabot/chrome-extension/)
- [Add Playwright End-to-End Tests to a Chrome Extension](https://getrush.ai/run/add-e2e-tests/chrome-extension/)
- [Add ESLint to a Chrome Extension](https://getrush.ai/run/add-eslint/chrome-extension/)

## The same job on other stacks

- [Add Dark Mode to an Electron App](https://getrush.ai/run/add-dark-mode/electron/)
- [Add a Dark Mode Toggle to an Eleventy Site](https://getrush.ai/run/add-dark-mode/eleventy/)
- [Add Dark Mode to a Gatsby Blog](https://getrush.ai/run/add-dark-mode/gatsby/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
