Skip to content

feat: screenshot capture messenger#1535

Merged
jxiwang merged 10 commits into
mainfrom
feat/hm-background-capture
Feb 19, 2026
Merged

feat: screenshot capture messenger#1535
jxiwang merged 10 commits into
mainfrom
feat/hm-background-capture

Conversation

@jxiwang
Copy link
Copy Markdown
Collaborator

@jxiwang jxiwang commented Feb 11, 2026

Summary

Extracts cross-window messenger functionality from plugin-autocapture-browser into a shared module in analytics-core, and adds background capture support that both autocapture and session-replay can use through a single shared messenger instance.

Problem

Previously, plugin-autocapture-browser owned its own WindowMessenger class, which handled all cross-window postMessage communication (visual tagging, ping/pong, etc.). Background capture is a new feature that needs to be available to both plugin-autocapture-browser and session-replay-browser. Without a shared messenger, each plugin would attach its own message listener and load its own copy of external scripts, causing duplicate listeners, duplicate script loads, and double notifications.

Solution

New messenger/ module in analytics-core:

  • BaseWindowMessenger — Generic, feature-agnostic cross-window messenger using postMessage. Handles message routing, request/response correlation (with timeouts), action handler dispatch, and idempotent script loading. The class is not exported as a value; consumers must use the singleton factory.
  • getOrCreateWindowMessenger() — Singleton factory stored on globalScope. A branded property (__AMPLITUDE_MESSENGER_INSTANCE__) is used instead of instanceof for reliable cross-bundle identity checks. This ensures that even when autocapture and session-replay are loaded as separate bundles, they share the same messenger instance and single message listener.
  • enableBackgroundCapture(messenger) — Composable enabler function that registers background-capture action handlers on a messenger instance. Idempotent via a brand key — safe to call from both autocapture and session-replay; only the first call takes effect.
  • Shared constants — Origin URLs (AMPLITUDE_ORIGIN, AMPLITUDE_ORIGIN_EU, AMPLITUDE_ORIGIN_STAGING) and the background capture script URL are now defined in analytics-core and re-exported from plugin-autocapture-browser for backwards compatibility.

Refactored plugin-autocapture-browser:

  • The old WindowMessenger class is replaced by an enableVisualTagging(messenger, options) function that follows the same composable pattern — it registers visual-tagging-specific action handlers on the shared messenger.
  • autocapture-plugin.ts now uses the singleton: getOrCreateWindowMessenger()enableVisualTagging()enableBackgroundCapture()messenger.setup().
  • The messenger option is removed from visualTaggingOptions since the singleton handles this.

Checklist

  • Does your PR title have the correct title format?
  • Does your PR have a breaking change?:

Note

Medium Risk
Touches cross-window messaging and dynamic script loading used by multiple plugins; mistakes could cause missed/duplicated postMessage handling or load failures across autocapture/session replay, though changes are guarded by singleton/idempotency and covered by tests.

Overview
Adds a new analytics-core messenger/ module, exposing getOrCreateWindowMessenger() (global singleton with branded identity), shared origin constants, and utilities for buffered action handling plus deduped external script loading.

Introduces enableBackgroundCapture() to register background-capture actions and load an external capture script once, then wires both plugin-autocapture-browser and session-replay-browser to use the shared messenger (including a refactor from WindowMessenger to enableVisualTagging() and removal of the visualTaggingOptions.messenger option). Tests are added/updated to validate singleton behavior, message buffering/idempotency, and background capture initialization/teardown.

Written by Cursor Bugbot for commit 676ae38. This will update automatically on new commits. Configure here.

@jxiwang jxiwang force-pushed the feat/hm-background-capture branch from 3f6f0cf to 4ed3252 Compare February 18, 2026 18:19
@jxiwang jxiwang marked this pull request as ready for review February 18, 2026 18:35
@jxiwang jxiwang requested a review from a team as a code owner February 18, 2026 18:35
Comment thread packages/analytics-core/src/messenger/constants.ts Outdated
Comment thread packages/analytics-core/src/messenger/base-window-messenger.ts
Comment thread packages/plugin-autocapture-browser/src/libs/messenger.ts Outdated
Copy link
Copy Markdown
Collaborator

@daniel-graham-amplitude daniel-graham-amplitude left a comment

Choose a reason for hiding this comment

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

Lgtm.

Comment thread packages/plugin-autocapture-browser/src/libs/messenger.ts
Comment thread packages/analytics-core/src/index.ts Outdated
Comment thread packages/analytics-core/src/messenger/base-window-messenger.ts
@jxiwang jxiwang changed the title feat: background capture messenger feat: screenshot capture messenger Feb 19, 2026
@jxiwang
Copy link
Copy Markdown
Collaborator Author

jxiwang commented Feb 19, 2026

bugbot run

Comment thread packages/analytics-core/src/messenger/base-window-messenger.ts
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

@jxiwang
Copy link
Copy Markdown
Collaborator Author

jxiwang commented Feb 19, 2026

bugbot run

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

Comment thread packages/analytics-core/src/messenger/utils.ts
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

Comment thread packages/analytics-core/src/messenger/base-window-messenger.ts
@jxiwang jxiwang merged commit 93ef551 into main Feb 19, 2026
12 checks passed
@jxiwang jxiwang deleted the feat/hm-background-capture branch February 19, 2026 20:52
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