Skip to content

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

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

ManthanNimodiya wants to merge 7 commits 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.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; all four previous findings are addressed and no new blocking issue was found.

Summary

Extracts the recorder host and browser-specific flags as groundwork for Firefox.

  • The service worker delegates recorder document lifecycle to a shared host.
  • Extension builds choose browser-specific paths and use the right extension URL scheme.
  • The extension recorder prefers a supported format that can stream uploads.
  • Mixed recording audio resumes when its audio context is suspended.

All four previous findings are addressed. No new actionable issues were found.

Reviews (5) · Last reviewed commit: "Merge remote-tracking branch 'origin/mai..." · Reviewed by Greptile

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.

@ManthanNimodiya

Copy link
Copy Markdown
Contributor Author

@greptileai

// install, and getDisplayMedia requires transient user activation so capture
// cannot start without a click inside the recorder document.
export const capabilities = {
supportsTabCapture: TARGET === "chrome",

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 Tab Capture Unguarded

supportsTabCapture is defined as false for Firefox, but the service worker tab-recording path still calls chrome.tabCapture.getMediaStreamId without checking it. When a Firefox build starts a tab-mode recording, that path can still dereference an API Firefox does not provide, so the recording flow fails before capture can start. This needs a separate guard or tab-mode disablement at the call path that uses the capability.

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

Comment:
**Tab Capture Unguarded**

`supportsTabCapture` is defined as false for Firefox, but the service worker tab-recording path still calls `chrome.tabCapture.getMediaStreamId` without checking it. When a Firefox build starts a tab-mode recording, that path can still dereference an API Firefox does not provide, so the recording flow fails before capture can start. This needs a separate guard or tab-mode disablement at the call path that uses the capability.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

@ManthanNimodiya
ManthanNimodiya force-pushed the feat/extension-target-neutral-refactor branch from 8d0d262 to 4e327b7 Compare August 6, 2026 05:19
@ManthanNimodiya

Copy link
Copy Markdown
Contributor Author

@greptileai

Comment thread apps/chrome-extension/vite.config.ts Outdated
export default defineConfig({
plugins: [react()],
define: {
__TARGET__: JSON.stringify("chrome"),

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 Chrome Target Hardcoded

This shared config now always compiles __TARGET__ as "chrome". A Firefox artifact built through this config will set supportsOffscreen and supportsTabCapture to true, so the worker can still enter the Chrome-only recorder host and tab capture paths instead of using the Firefox capability set. Please make the target value come from the build target, such as a Firefox-specific config or mode/env define.

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

Comment:
**Chrome Target Hardcoded**

This shared config now always compiles `__TARGET__` as `"chrome"`. A Firefox artifact built through this config will set `supportsOffscreen` and `supportsTabCapture` to true, so the worker can still enter the Chrome-only recorder host and tab capture paths instead of using the Firefox capability set. Please make the target value come from the build target, such as a Firefox-specific config or mode/env define.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

@ManthanNimodiya
ManthanNimodiya marked this pull request as draft October 7, 2026 03:06
@richiemcilroy

Copy link
Copy Markdown
Member

Thanks @ManthanNimodiya! Greptile is at 4/5 here. We only merge once Greptile reaches 5/5, so please address its findings in the summary above (or reply to them if you think one is wrong), then comment @greptileai review to re-run it. If the branch is behind main, please merge or rebase on main first. Once it is 5/5 we will run the full verification suite on it.

@ManthanNimodiya
ManthanNimodiya force-pushed the feat/extension-target-neutral-refactor branch from 4b7a016 to 2ae184d Compare October 22, 2026 11:54
@vercel

vercel Bot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

@ManthanNimodiya is attempting to deploy a commit to the Cap Software Inc Team on Vercel.

A member of the Team first needs to authorize it.

@ManthanNimodiya

Copy link
Copy Markdown
Contributor Author

@richiemcilroy rebased on main. On Greptile's target threads: the define is now injected in every vite config from a TARGET env (default chrome, unknown values fail the build), so a build can't silently fall back to Chrome APIs. Checked the bundles: the TARGET=firefox build contains no offscreen.createDocument or tabCapture calls at all. Also applied the getContexts ?? [] hardening and removed a variable the rebase brought back that main had deleted. @greptileai review

…-neutral-refactor

# Conflicts:
#	apps/chrome-extension/src/background/service-worker.ts
#	apps/chrome-extension/src/recorder/recorder.test.ts
#	apps/chrome-extension/src/recorder/recorder.ts
@vercel

vercel Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
cap-status Skipped Skipped Nov 17, 2026 6:37am UTC

@vercel
vercel Bot temporarily deployed to Preview – cap-status November 17, 2026 06:37 Inactive
@ManthanNimodiya

Copy link
Copy Markdown
Contributor Author

@greptileai review

@richiemcilroy merged main in. Resolved three conflicts: the recorder keeps main's createRecordingAudioContext plus a resume() for windows without user activation, main's new offscreen close moved into recorder-host as a Firefox-guarded closeRecorderHost, and main's recorder.test.ts moved to src/recorder/. All 93 extension tests pass, tsc and Biome are clean, and the TARGET=firefox bundle still has no offscreen or tabCapture calls.

@ManthanNimodiya
ManthanNimodiya marked this pull request as ready for review November 17, 2026 06:39

This branch was previously deployed

1 inactive deployment
Preview – cap-status — 65877efd Deployed Nov 17, 2026 by vercel[bot]
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.

2 participants