Browser-Agnostic Ports
Target Audience: Advanced Goal: Access every browser API through typed, injectable ports — and route messages across contexts — without coupling code to any single browser.
HexaJS ships the @hexajs-dev/ports package: a complete set of Promise-based, platform-agnostic wrappers for the browser extension API surface. Instead of scattering chrome.* / browser.* calls and platform checks throughout your code, you inject a port and call its methods. The CLI build swaps in the correct platform implementation automatically.
At a glance
| Context | Ports | Highlights |
|---|---|---|
| General | 3 | Runtime messaging, i18n, extension URLs |
| Background | 22 | Tabs, Storage, Cookies, Downloads, Alarms, Notifications, WebRequest, Scripting, Permissions… |
| UI | 1 | DevTools panels |
| Content | 1 | Clipboard |
29 built-in ports covering the full browser-API surface — all injectable, all typed, all cross-platform.
→ See the complete list with descriptions in the Ports Catalog.
Core principles
- Never call
chrome.*/browser.*directly in application classes. - Use
@hexajs-dev/portsand injected clients for every browser interaction. - Route request/response messages through background controller endpoints.
- Keep code platform-agnostic; the CLI builds per target platform.
What you'll learn
- How the ports layer abstracts every browser API behind a single injectable interface.
- How clients route messages across context boundaries via Bridged Routing.
- How
RuntimePortand other ports hide browser API differences at build time. - How to implement Custom Ports with platform switch logic when a built-in port doesn't exist for your use case.
- How background acts as the router between UI/content contexts.
Platform build behavior: Platform-aware ports use
__HEXA_PLATFORM__for tree-shaking. See Build Pipeline for details.
Further reading
- Ports Catalog — full grouped table of every built-in port
- API Reference — exhaustive method signatures and usage examples