Skip to content

@sentry/browser not working in chrome extensions #5289

Closed as not planned
Closed as not planned

Description

@lforst

When importing @sentry/browser from a service worker file (e.g: background.js) like

import * as Sentry from '@sentry/browser';

we get

Uncaught TypeError: Cannot read properties of undefined (reading 'visibilityState')
var initHiddenTime = () => {
  return (0,_sentry_utils__WEBPACK_IMPORTED_MODULE_0__.getGlobalObject)().document.visibilityState === 'hidden' ? 0 : Infinity;
};

It crashes at the import level, because @sentry/browser relies on the document which doesn't exist in the context of a background script.


Edit: That's because I was loading BrowserTracing, when removing it it doesn't crash!

Originally posted by @Vadorequest in #4098 (comment)

Activity

  1. lforst commented on Jun 21, 2022

    @lforst
    ContributorAuthor

    When tackling this issue we should set up an example chrome extension to properly test our fix.

    I think it also might be a good idea to rethink how we make use of getGlobalObject() in the SDK. Right now it basically acts as a type cast and is very error-prone.

  2. dchenk commented on Dec 15, 2022

    @dchenk

    I'm getting a similar error (Cannot read properties of undefined (reading 'email')) originating from chrome-extension://dbilanlcioamaadkbepcenpombaejbla/dist/inject_content.js but I've got a normal app, not a Chrome extension.

  3. gnir-work commented on Jan 2, 2023

    @gnir-work

    Hello there :)
    Me and my team are developing a manifest V3 extension and I cant explain how much sentry has helped us so far!
    We wanted to take our monitoring to the next level with sentry tracing, however we encourted the same thing as described in this issue. After some playing around we managed to bypass the current usage of WINDOW.document.visibilityState like this:

    // Run this code before Sentry.init(...)
    Sentry.WINDOW.document = {
      visibilityState: 'hidden',
      addEventListener: () => {},
    };

    Ofcourse this isn't a long term solution and we will update as soon as this issue is closed 😄

    BTW, if there are other people arriving on this thread while trying to add tracing to their manifest V3 extension make sure that you create a new transaction around the code that you need to be traced. It took us quite some time to understand that there is no transaction like pageload or something similar in extensions (This is my speculation after debugging for quite some time, in case I am mistaken please correct me 😄).

    chrome.alarms.onAlarm.addListener(async (alarm: Alarm) => {
        // Generic Sentry.init(....)
        setupSentry();
        const transaction = Sentry.startTransaction({ name: `Alarm ${alarm.name} rang` });
        Sentry.getCurrentHub().configureScope((scope) => scope.setSpan(transaction));
    	// Do your thing
    	// ....
    	transaction.finish();
    }

    Hope this helps :)

  4. abhishek871 commented on May 29, 2023

    @abhishek871

    I'm getting a similar error (Cannot read properties of undefined (reading 'email')) originating from chrome-extension://dbilanlcioamaadkbepcenpombaejbla/dist/inject_content.js but I've got a normal app, not a Chrome extension.

    Is ur issue resolved now??
    As I am also getting the same error.

  5. JUDE-EQ commented on Jun 15, 2023

    @JUDE-EQ

    I'm getting a similar error (Cannot read properties of undefined (reading 'email')) originating from chrome-extension://dbilanlcioamaadkbepcenpombaejbla/dist/inject_content.js but I've got a normal app, not a Chrome extension.

    I'm also getting this error.

  6. lforst commented on Jun 23, 2023

    @lforst
    ContributorAuthor

    @abhishek871 @JUDE-EQ Usually these errors originate from browser extensions that your users are using. You can filter these out with the denyUrls option: https://docs.sentry.io/platforms/javascript/configuration/filtering/#decluttering-sentry

  7. moved this to Waiting for: Product Owner in GitHub Issues with 👀on Jul 24, 2023
  8. 42 remaining items

  9. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 2on Apr 18, 2024
  10. stevenmason commented on Apr 18, 2024

    @stevenmason

    Thank you for the response. 100% of the time, no errors are coming through when using Sentry.captureException(e);

  11. moved this to Waiting for: Product Owner in GitHub Issues with 👀 2on Apr 18, 2024
  12. lforst commented on Apr 18, 2024

    @lforst
    ContributorAuthor

    @stevenmason I think this is a separate issue. Would you mind opening a new one and providing a reproduction example? Thank you!

  13. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 2on Apr 18, 2024
  14. moved this to Waiting for: Community in GitHub Issues with 👀 3on Jun 17, 2024
  15. getsantry commented on Jul 9, 2024

    @getsantry

    This issue has gone three weeks without activity. In another week, I will close it.

    But! If you comment or otherwise update it, I will reset the clock, and if you remove the label Waiting for: Community, I will leave it alone ... forever!


    "A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions