DEV Community

Luca Mancini
Luca Mancini

Posted on Originally published at mancinux.com

what a small chrome extension taught me

You find a useful page, open an extension to save it as a task, and discover you need to sign in. Fair enough. You sign in, reopen the extension, and it offers to save the page you are now looking at.

The login destination. Very useful, grazie.

This is the case I kept coming back to while building Capture to Operately. The feature is small: take the current page's title and URL, let you choose a project or task board, and create a task. Task management stays in Operately.

The interesting engineering lives in the interruptions. The popup can disappear, the active tab can change, and the session can expire between loading the form and submitting it. A working API call only covers part of that experience.

the popup is a temporary view

Chrome closes an extension popup when the user clicks outside it. Opening a sign-in tab means leaving the interface where the capture started.

Keeping the page in a variable inside the popup is therefore a very optimistic storage strategy.

Moving that variable into the background worker doesn't solve persistence either. In Manifest V3, the service worker can shut down and lose its globals. Both pieces of JavaScript have shorter lives than the user's intention.

For the sign-in handoff, I save the capture in chrome.storage.local before opening the new tab. This is the relevant part of the implementation:

await storage.set({
  pendingCapture: {
    ...state.page,
    title: elements.taskName.value?.trim() || state.page.title,
  },
});

const tab = await chrome.tabs.create({
  url: `${state.baseUrl}/log_in`,
});

await storage.set({ authTabId: tab.id });
window.close();
Enter fullscreen mode Exit fullscreen mode

storage here is chrome.storage.local. The edited task title travels with the original URL. Otherwise, someone can spend time making the title useful and lose that work at the exact moment we ask them to do something else.

The worker watches the sign-in tab. When it finishes loading a page inside the configured server, the worker tries an authenticated API request before treating the session as usable. A changed URL alone doesn't prove that sign-in worked.

When the popup initializes on the Operately tab, it prefers the pending capture over that tab's title and URL. After a successful task creation, it clears the pending capture. Changing servers in settings clears it too, so an interrupted capture doesn't silently follow the user into a different server.

There is a deliberate limit here: version 0.1.0 keeps one pending capture for the sign-in flow. It isn't an autosaved draft for every popup edit, or a queue of captures from multiple windows. Those would need their own rules for identity, expiry, and cancellation. One storage key is enough only while the product promises one pending action.

two different reasons to ask for access

This extension needs to read something from one website and send something to another. Separating those jobs made the permissions easier to reason about.

The first job only needs the selected page's title and URL. activeTab gives temporary access after the user invokes the extension, including those tab properties. The implementation doesn't need a content script, access to the page body, or a listener collecting every page someone visits.

The second job needs host access to the Operately server for API requests. That's separate from reading the page being captured. The relevant manifest fields are:

{
  "permissions": ["activeTab", "storage"],
  "host_permissions": ["https://app.operately.com/*"],
  "optional_host_permissions": [
    "https://*/*",
    "http://localhost/*",
    "http://127.0.0.1/*"
  ]
}
Enter fullscreen mode Exit fullscreen mode

The broad optional HTTPS pattern exists because Operately can be self-hosted. We don't know that server's hostname ahead of time. It declares what access the extension may request; it doesn't grant access to every HTTPS site on installation.

When someone saves their server in settings, the extension requests permission for that server's host pattern. If they decline, it doesn't save the new configuration. Remote servers must use HTTPS; plain HTTP is accepted only for the two local development hosts above.

That puts the permission prompt next to the decision that explains it: “Use this server.” Much easier to understand than a large request for access before the user has done anything.

test the detour, too

The extension uses plain HTML, CSS, and JavaScript modules, with no runtime dependencies or build step. That suited the scope. The popup handles the form, the worker handles API messages and the sign-in handoff, and ordinary modules handle URLs, destinations, and request payloads.

Those ordinary modules are where the automated tests have useful work to do. They check that a task reaches the selected board, a remembered destination still exists, remote HTTP servers are rejected, and an unauthorized response becomes an authentication error.

One of my favourite small checks is this:

assert.equal(
  belongsToServer(
    "https://app.operately.com.evil.test/acme",
    "https://app.operately.com",
  ),
  false,
);
Enter fullscreen mode Exit fullscreen mode

A server check based on a string prefix would be a poor foundation for deciding whether to resume a capture. Parsing the URL lets the implementation compare the actual origin.

The Node tests don't prove that Chrome will reopen the popup after sign-in, or that a real browser session will behave like a mocked fetch. We also loaded the extension in Chrome and tested it there. For an extension like this, I'd make the interrupted path part of the acceptance test: start on a page while signed out, go through sign-in, and check that the original page survives all the way into the task.

Also test expiry at submission time. Successfully loading a destination list doesn't guarantee that the session will still be valid when someone clicks create.

the zip is one release artifact

Version 0.1.0 was packaged, tested in Chrome, released on GitHub, and submitted for store review on November 20. Getting to that submission had its own small reminders.

We uploaded the ZIP, filled the listing, saved the draft, and still had more to do. Google's publishing workflow also has privacy declarations and distribution settings. The working extension doesn't supply those answers for you.

We needed a 128×128 icon, a screenshot, a small promotional image, a privacy policy, and explanations for the permissions. I keep the listing copy and assets in the repository alongside the code. When behaviour changes, there is somewhere obvious to update the explanation of that behaviour too.

Even “just the title and URL” deserves a careful description. A URL can point to an internal issue. A title can contain a customer's name. In this extension, the captured title and URL are sent to the configured Operately server when the user chooses to create the task. That timing is part of the feature, and it belongs in the product explanation.

Next time, I'd write the permission justifications while designing the flow and prepare the store assets before calling the release ready. Discovering another empty form after the code is finished is an oddly effective way to learn project planning.

The source is small enough to read. If you're building something similar, try the signed-out flow early. Close the popup, change tabs, come back. See how much of the user's work your extension still remembers.

Top comments (0)