Skip to content

fix: fix the problem that userAgentData.brands might be undefined#2058

Open
Rossonero wants to merge 1 commit into
tradingview:masterfrom
Rossonero:fix/useragent-brands-undefined
Open

fix: fix the problem that userAgentData.brands might be undefined#2058
Rossonero wants to merge 1 commit into
tradingview:masterfrom
Rossonero:fix/useragent-brands-undefined

Conversation

@Rossonero

@Rossonero Rossonero commented Feb 4, 2026

Copy link
Copy Markdown

Type of PR: bugfix

PR checklist:

Fixes #2046

Overview of change:

This PR fixes the issue that navigator.userAgentData.brands is sometimes undefined in some environments. The root causes are browser compatibility (e.g., Firefox, Safari do not support navigator.userAgentData API) and privacy policy restrictions (e.g., strict privacy mode in Chromium-based browsers disables the API).

The fix implements a compatible solution:

  1. First check if navigator.userAgentData and navigator.userAgentData.brands exist; if they do, use the standard API as before.

  2. If not, degrade to parse the traditional navigator.userAgent string to extract browser brand and version information, ensuring consistent return format and avoiding code errors.

  3. Filter the placeholder brand Not/A)Brand in brands array (added by browsers for anti-fingerprinting) to ensure the returned data is valid and usable.

This change is backward-compatible, does not affect existing functionality in supported browsers, and effectively resolves the undefined error in unsupported environments or privacy-restricted scenarios.

Is there anything you'd like reviewers to focus on?

  1. Whether the compatible logic of navigator.userAgentData and navigator.userAgent is complete, and if there are any edge cases (e.g., special browser userAgent strings) not covered.

  2. Whether the return format of the browser information is consistent with the original design, to avoid affecting subsequent business logic that depends on this data.

  3. Whether the filtering logic of Not/A)Brand is reasonable and does not filter out valid brand information.

截屏2026-02-04 20 21 44

@SlicedSilver SlicedSilver requested a review from Copilot February 4, 2026 14:47
@SlicedSilver SlicedSilver added the bug Unexpected problem or unintended behavior. label Feb 4, 2026

Copilot AI left a comment

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.

Pull request overview

This PR fixes a runtime error where navigator.userAgentData.brands could be undefined in certain browsers or privacy-restricted environments, causing the isChromiumBased() function to fail.

Changes:

  • Added explicit check for navigator.userAgentData.brands array existence before attempting to access it
  • Prevents TypeError when brands property is undefined due to browser compatibility or privacy settings

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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

Labels

bug Unexpected problem or unintended behavior.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lightweight Charts used in an Ionic project - Chrome Simulator crashes

4 participants