Skip to content

refactor(extension): extract target-neutral recorder host and platform layer#1998

Open
ManthanNimodiya wants to merge 1 commit into
CapSoftware:mainfrom
ManthanNimodiya:feat/extension-target-neutral-refactor
Open

refactor(extension): extract target-neutral recorder host and platform layer#1998
ManthanNimodiya wants to merge 1 commit into
CapSoftware:mainfrom
ManthanNimodiya:feat/extension-target-neutral-refactor

Conversation

@ManthanNimodiya

@ManthanNimodiya ManthanNimodiya commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Groundwork for a Firefox port, zero Chrome behavior change.

Renames offscreen.html→recorder.html and src/offscreen/→src/recorder/; extracts the offscreen lifecycle into src/background/recorder-host.ts; adds src/platform/ (compile-time TARGET, capability flags, EXTENSION_PROTOCOL).

Fixes three sender checks that hardcoded chrome-extension: (on Firefox they'd misclassify extension pages as web pages).

Pins the recorder to the streaming pipeline explicitly (selectRecordingPipelineFromSupport(..., { preferStreamingUpload: true })) since the UA heuristic in recorder-core returns false on Firefox; Chrome output unchanged.

Greptile Summary

This PR splits the extension recorder into target-neutral pieces. The main changes are:

  • Renamed the offscreen recorder entrypoint to recorder.html.
  • Moved recorder code under src/recorder.
  • Extracted Chrome offscreen lifecycle handling into recorder-host.ts.
  • Added platform target, capability, and extension protocol helpers.
  • Updated sender checks and recorder pipeline selection.

Confidence Score: 4/5

The Firefox-target path can still load Chrome-only recorder-host code and can default to Chrome capabilities.

  • The renamed Chrome recorder entrypoint is wired consistently.
  • The service worker imports recorder-host.ts before any offscreen capability guard can run.
  • The target helper falls back to Chrome when the build does not inject __TARGET__.

apps/chrome-extension/src/background/recorder-host.ts, apps/chrome-extension/src/platform/target.ts, apps/chrome-extension/vite.config.ts

Important Files Changed

Filename Overview
apps/chrome-extension/recorder.html Renames the recorder HTML entrypoint and points its module script at the new recorder path.
apps/chrome-extension/src/background/recorder-host.ts Extracts offscreen document lifecycle handling, but imports Chrome-only runtime APIs without a Firefox-safe guard.
apps/chrome-extension/src/background/service-worker.ts Uses the new recorder host and protocol helper while preserving the existing sender-token checks.
apps/chrome-extension/src/platform/capabilities.ts Adds target-specific capability flags for Chrome and Firefox.
apps/chrome-extension/src/platform/extension-protocol.ts Derives the extension protocol from the runtime instead of hardcoding Chrome's protocol.
apps/chrome-extension/src/platform/target.ts Adds the compile-time target helper, but an absent target define currently falls back to Chrome.
apps/chrome-extension/src/recorder/recorder.ts Moves the recorder module, adds a best-effort AudioContext resume, and pins pipeline selection to streaming upload.
apps/chrome-extension/vite.config.ts Updates the Rollup input from the old offscreen HTML file to the new recorder HTML file.
Prompt To Fix All With AI
Fix the following 2 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 2
apps/chrome-extension/src/background/recorder-host.ts:14
**Firefox Worker Imports Chrome APIs**

`recorder-host.ts` is imported by the service worker at module load time, but this line reads `chrome.runtime.ContextType.OFFSCREEN_DOCUMENT` before any capability guard can run. In a Firefox build where that Chrome-only API is missing, the background worker can throw during startup instead of reaching the `supportsOffscreen` checks.

### Issue 2 of 2
apps/chrome-extension/src/platform/target.ts:8
**Missing Target Becomes Chrome**

`TARGET` defaults to `"chrome"` whenever `__TARGET__` is absent, but the changed Vite config does not define `__TARGET__`. A Firefox build that uses this config without an injected define will enable Chrome-only capabilities like offscreen and tab capture, sending the Firefox worker into unsupported APIs.

Reviews (1): Last reviewed commit: "refactor(extension): extract target-neut..." | Re-trigger Greptile

Greptile also left 2 inline comments on this PR.

Context used:

  • Context used - CLAUDE.md (source)
  • Context used - AGENTS.md (source)

contextTypes: [chrome.runtime.ContextType.OFFSCREEN_DOCUMENT],
documentUrls: [recorderUrl],
},
(contexts) => resolve(contexts),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chrome.runtime.getContexts can invoke the callback with contexts undefined (or fail in older runtimes), which would make hasRecorderHost() throw on .length. Small hardening tweak:

Suggested change
(contexts) => resolve(contexts),
(contexts) => resolve(contexts ?? []),

return new Promise<Array<{ documentUrl?: string }>>((resolve) => {
chrome.runtime.getContexts(
{
contextTypes: [chrome.runtime.ContextType.OFFSCREEN_DOCUMENT],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Firefox Worker Imports Chrome APIs

recorder-host.ts is imported by the service worker at module load time, but this line reads chrome.runtime.ContextType.OFFSCREEN_DOCUMENT before any capability guard can run. In a Firefox build where that Chrome-only API is missing, the background worker can throw during startup instead of reaching the supportsOffscreen checks.

Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/chrome-extension/src/background/recorder-host.ts
Line: 14

Comment:
**Firefox Worker Imports Chrome APIs**

`recorder-host.ts` is imported by the service worker at module load time, but this line reads `chrome.runtime.ContextType.OFFSCREEN_DOCUMENT` before any capability guard can run. In a Firefox build where that Chrome-only API is missing, the background worker can throw during startup instead of reaching the `supportsOffscreen` checks.

How can I resolve this? If you propose a fix, please make it concise.

export type ExtensionTarget = "chrome" | "firefox";

export const TARGET: ExtensionTarget =
typeof __TARGET__ === "undefined" ? "chrome" : __TARGET__;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Missing Target Becomes Chrome

TARGET defaults to "chrome" whenever __TARGET__ is absent, but the changed Vite config does not define __TARGET__. A Firefox build that uses this config without an injected define will enable Chrome-only capabilities like offscreen and tab capture, sending the Firefox worker into unsupported APIs.

Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/chrome-extension/src/platform/target.ts
Line: 8

Comment:
**Missing Target Becomes Chrome**

`TARGET` defaults to `"chrome"` whenever `__TARGET__` is absent, but the changed Vite config does not define `__TARGET__`. A Firefox build that uses this config without an injected define will enable Chrome-only capabilities like offscreen and tab capture, sending the Firefox worker into unsupported APIs.

How can I resolve this? If you propose a fix, please make it concise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant