Genesis

Packages

@marktiderman/genesis-extension

0.2.3Primitive · Shells

Build Chrome extensions (Manifest V3) with Genesis: checked messaging, settings storage, the UI as a floating panel, a top bar or Chrome's side panel, an in-page bar, native-helper jobs, React pieces built on Genesis UI, and a Chrome fake for tests. Part of the Genesis framework.

Build Chrome extensions (Manifest V3) with Genesis. Each extension writes only its own screens and actions; this package supplies the parts every one of them needs, done once and tested.

- **Checked messaging.** `createRouter({ pages, content })` answers the extension's own pages and, separately, its scripts inside web pages. A hostile page can drive those scripts, so they may only send the few message types listed for them, and their handlers get only their own tab. `request()` throws the other side's error. - **Settings.** `createPrefs(key, defaults)` stores settings in `chrome.storage.local`. Each write merges into the latest saved values under a Web Lock that every page and the service worker share, so two pages writing at once don't overwrite each other. A failed write doesn't block later ones. `subscribe` and React's `usePrefs` follow changes made from any page. - **Three ways to open the main page.** `openSurface(tab, { page, mode })` opens it as a floating panel over the page, a top bar across it, or Chrome's side panel. - **Panel and bar:** the page sits in an iframe in a closed shadow root, whose host pins its own style against hiding and moving. The host and the page talk over a private `MessageChannel`. Nothing is ever posted to the page: the host offers the frame a fresh channel every 200ms, addressed to the extension's own origin (`chrome-extension://<id>`, which is where a `use_dynamic_url` page really loads), with a per-open secret. The surface page takes the first offer whose secret checks out once it calls `connectSurface()`, however late, and answers on it. Until it answers, the panel stays blank, so whatever else the frame shows first is never seen; with no answer in 30 seconds, it closes. Any later load of the frame (the page navigating it, or the surface reloading itself) closes it. The secret is minted by the service worker for each open and kept in `chrome.storage.session` (which content scripts and web pages can't read). It travels only in the host's offers, never on the frame, and the surface checks it against what the worker kept, then uses it up. Any page in a frame must be confirmed this way (its address can't opt out), so a surface a web page frames itself stays hidden, and `surface.ready` resolves `false` after 30 seconds. Await `surface.ready` before reading the tab or acting for the user. Call `connectSurface()` early, before awaiting data. `connectSurface()` returns the same surface however often it's called. The frame's address (its `params` and `tab`) is visible to the page in its Resource Timing, so don't put anything sensitive in `params`. A page can still draw over the frame to trick a click, so check `surface.visible()` (Chrome's IntersectionObserver v2) before a sensitive action. - **Side panel:** `syncSidePanel(mode)` hands the icon and the shortcut to Chrome, so it opens even right after the service worker wakes. `params` reach a side panel through its path, which Chrome may not have applied yet when a panel opens for the first time. Pass anything a side panel must have this time through `chrome.storage.session` instead. - **In the page itself:** `connectSurface()` says which of the three it is, closes it, and keeps a top bar as tall as its content. - **An in-page bar.** `showPageBar(tabId, model)` draws a thin bar across a web page, such as "already in your library", with chips and buttons. It shrinks to a corner tab, is built from plain text in a closed shadow root, and reports its buttons back as `gx-page-bar` messages. Chips and buttons can carry a line icon (`check`, `download`, `captions`, `clips`, `chevron-down`, `chevron-up`, `close`) and a tooltip, a button can show only on hover (`reveal: "hover"`), and `density: "compact"` makes it one 32px row. - **Native helper jobs.** `startNativeJob(host, message)` talks to a local helper (unpacked or private builds only) and supports progress, `waitFor("step")`, `answer("need-token", …)`, `post` and `close` (cancel). `pingNativeHost` checks the helper is installed. - **Mac-aware shortcuts.** `shortcutKeys` and `<ShortcutKeys command="_execute_action" />` show "⌥ Option + ⇧ Shift + S" on a Mac, in the keys Chrome currently has for the command. - **React, on Genesis UI:** - `<ExtensionSettings tabs={…}>` is a tabbed options page. The open tab is kept in the address (`#tab=`), and only that tab is rendered, so each tab loads its data when it's opened. - `<SurfaceModePicker>` chooses panel, bar or side panel. - `useLazyList` with `<LazyListFooter>` and `<ListSkeleton>` loads a long list a page at a time as it scrolls, with placeholders and Retry.

pnpm add @marktiderman/genesis-extension

READMEChangelognpm

@marktiderman/genesis-extension

Other exports (16)

broadcast · commandKeys · connectSurface · createPrefs · createRouter · getChrome · isFromContentScript · isFromExtensionPage · isMac · openSurface · pingNativeHost · request · shortcutKeys · showPageBar · startNativeJob · syncSidePanel

And 20 types.

@marktiderman/genesis-extension/react

Hooks (3)

  • useLazyList

    A list loaded a page at a time as it's scrolled, for a library of hundreds of items: the first page shows at once, the rest follow. `load` gets the offset and page size; a page shorter than the size is the last. A newer reset drops answe...

  • usePrefs

    The extension's settings, kept current when any page changes them. `null` until loaded.

  • useSurface

    This page as a surface (panel, bar, side panel or popup); see `connectSurface`.

Other exports (5)

ExtensionSettings · LazyListFooter · ListSkeleton · ShortcutKeys · SurfaceModePicker

And 2 types.

@marktiderman/genesis-extension/testing

Other exports (1)

installChromeFake

And 2 types.