All jobs

Make a Chrome Extension Popup Remember Its State

Yes. An agent moves a Chrome extension popup's state into chrome.storage so the count and badge survive the popup closing, adds Jest tests, and opens a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads the extension to find where state lives today. In an extension built on chibat/chrome-extension-typescript-starter, public/manifest.json is Manifest V3 with an action popup at popup.html, an options_ui page, a background.service_worker at js/background.js, a content script on <all_urls> and only the storage permission. src/popup.tsx is a React 18 component that keeps count in useState(0), writes it to the badge with chrome.action.setBadgeText in an effect, reads the active tab's URL with chrome.tabs.query, and sends a colour message to the content script. Every time the popup closes, React unmounts and count goes back to zero on the next open, while the badge still shows the last number, so the two disagree after one close. src/options.tsx already does the right thing for its own fields: it loads favoriteColor and likesColor from chrome.storage.sync.get with defaults and saves them with chrome.storage.sync.set. src/background.ts is a setTimeout loop that logs nothing. webpack/webpack.common.js bundles four entries and splits a vendor chunk for everything except the service worker, jest.config.js runs ts-jest over src with tsconfig.test.json, and the only test is src/__tests__/sum.ts.

It adds a small storage module, src/storage.ts, that owns the keys: getCount() and setCount(n) on chrome.storage.local, since a per-device counter should not sync across the user's browsers, and getOptions() and setOptions() on chrome.storage.sync, with the same favoriteColor and likesColor defaults the options page uses today. Both read through chrome.storage.*.get with a defaults object so a fresh install gets a value, and both are promise-based, which the chrome.* APIs support under Manifest V3 and which keeps the React effects short.

Then it rewires the popup: on mount, an effect calls getCount() and sets state from it; the count up button calls setCount(count + 1) and writes through setCount in the module; a chrome.storage.onChanged listener keeps two open popups, or the popup and the options page, in step. The badge write moves out of the popup into src/background.ts, which sets the badge from storage on chrome.runtime.onInstalled and chrome.runtime.onStartup and updates it from the same onChanged listener, so the badge is right even when the popup has never been opened in this session. The options page switches to getOptions and setOptions so both pages share one definition of the keys and defaults. Nothing changes in the manifest: the storage permission is already there.

The agent writes src/__tests__/storage.ts with a fake chrome.storage object on globalThis that records get and set calls and returns the stored values, and asserts the defaults on first read, the round trip, and that getCount reads local while getOptions reads sync. It runs npm ci, npx jest and npm run build as .github/workflows/build.yml does on Node 20 and 22, then loads the built dist directory in a browser in its sandbox, clicks count up twice, closes and reopens the popup, and reads the count and the badge to confirm they agree.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Make the popup of this Chrome extension remember its state across closes and open a pull request. Read public/manifest.json, src/popup.tsx, src/options.tsx, src/background.ts, src/content_script.tsx, webpack/webpack.common.js, jest.config.js, tsconfig.test.json and .github/workflows/build.yml first. Add src/storage.ts with promise-based getCount and setCount on chrome.storage.local and getOptions and setOptions on chrome.storage.sync, using the same favoriteColor and likesColor defaults the options page has. In popup.tsx load the count from storage on mount, write it on each count up, and listen to chrome.storage.onChanged. Move the badge update into background.ts: set it from storage on onInstalled and onStartup and on every onChanged. Switch options.tsx to the storage module. Do not change the manifest or the content script. Add src/__tests__/storage.ts with a fake chrome.storage on globalThis covering defaults, round trips and which area each key uses. Run npm ci, npx jest and npm run build, then load dist in a browser, click count up twice, close and reopen the popup and confirm the count and badge agree. Paste the test and build 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 that adds src/storage.ts and src/__tests__/storage.ts and edits src/popup.tsx, src/options.tsx and src/background.ts, with public/manifest.json, the content script and the webpack config untouched. The description explains the split between local and sync, pastes the jest output with the new test file listed and the webpack build summary with the four entries and the vendor chunk, and describes the manual check in the browser: the count after reopening and the badge text beside it.

On a repository with the starter's build workflow, the pull request runs npm ci, npm run build and npm test on Node 20 and 22, so the new test runs on both before review. The run's record keeps the browser session as well, so a reviewer can see the popup being reopened rather than take the description's word for it.

A reviewer checks that the badge is owned by the service worker alone, so no two writers race; that the onChanged listener filters on the area and key it cares about; and that moving favoriteColor behind a module did not change the options page's behaviour. Then they decide whether the counter should also reset on onInstalled with reason install.

Run it on every extension with popup-only state

A team that maintains several extensions sends one POST /o/{org}/sessions per repository with this prompt and that repository in resources. Each run reads its own popup, so an extension whose state is a list rather than a counter gets the same module shape with the keys it actually has, and each run opens its own pull request. Send a request_id with each request so a retried call returns the run it already started.

Give the key a webhook_url and every run it starts posts run.completed when its turn ends, with data.session_url to read the pull request link and the test output from, so nobody polls.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

The bug here is invisible to a unit test: the popup is correct every time it renders, and only the next open shows the stale badge. Checking the fix means building the extension, loading the unpacked dist in a real browser, clicking, closing and reopening, which is exactly what a laptop session tends to skip. A Rush sandbox has a computer and a browser in it, so the agent does that check and keeps it in the run's record beside the jest and webpack output.

Because the result is a branch with a pull request, the reviewer sees the storage module, the rewired popup and the evidence together, and the same request works on any Manifest V3 extension whose state is trapped in a popup component.

Frequently asked questions

Why local for the count and sync for the options?

chrome.storage.sync follows the signed-in user across browsers and has tighter write limits; it suits settings. A counter is per-device and changes often, so chrome.storage.local is the right area. The module makes that choice explicit in two function names, and a team that wants the counter to sync changes one line.

Why move the badge into the service worker?

The popup only exists while open, so a badge set from the popup is set only when the user looks. The service worker runs on install and startup and receives onChanged events, so it can keep the badge equal to storage at all times. It also removes the case where the popup and another page both write the badge.

Does this need a new permission?

No. The manifest already declares storage, and chrome.action.setBadgeText needs none. The pull request changes nothing in public/manifest.json, so there is no new permission prompt for users on update.

How are the chrome APIs tested under Jest?

The test file installs a fake chrome.storage on globalThis before importing the module, with local and sync objects whose get and set keep values in a plain map. The tests assert what was read and written and from which area; the browser check covers the real API.

More Chrome Extension jobs

More jobs like this: Hand off a long task

Keep reading