Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@sentry/browser not working in chrome extensions #5289

Closed
lforst opened this issue Jun 21, 2022 · 22 comments
Closed

@sentry/browser not working in chrome extensions #5289

lforst opened this issue Jun 21, 2022 · 22 comments

Comments

@lforst
Copy link
Member

lforst commented Jun 21, 2022

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)

@lforst
Copy link
Member Author

lforst commented Jun 21, 2022

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.

@dchenk
Copy link

dchenk commented Dec 15, 2022

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.

@gnir-work
Copy link

gnir-work commented Jan 2, 2023

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 :)

@abhishek871
Copy link

abhishek871 commented May 29, 2023

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.

@JUDE-EQ
Copy link

JUDE-EQ commented Jun 15, 2023

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.

@lforst
Copy link
Member Author

lforst commented Jun 23, 2023

@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

@flazouh
Copy link

flazouh commented Apr 1, 2024

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 :)

This solved my issue, but my Sentry.captureException seem to not get received in my dashboard even though they get sent.
getsentry/sentry#67630 (comment)

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 2 Apr 1, 2024
@lforst
Copy link
Member Author

lforst commented Apr 2, 2024

This solved my issue, but my Sentry.captureException seem to not get received in my dashboard even though they get sent.
getsentry/sentry#67630 (comment)

@alexandredepape seems like you solved it?

@flazouh
Copy link

flazouh commented Apr 2, 2024

This solved my issue, but my Sentry.captureException seem to not get received in my dashboard even though they get sent.
getsentry/sentry#67630 (comment)

@alexandredepape seems like you solved it?

It solved the fact that the service worker was not working after adding the call to Sentry.init but after doing this, it fixed it in the sense that it was not crashing anymore:
Even if there is this, but I can ts-ignore it.

image

My bigger issue is here

#11399

Where the request are sent but nothing is displayed in my dashboard, maybe I'm missing something

@lforst
Copy link
Member Author

lforst commented Apr 15, 2024

I opened a PR to stomp places that look like may fail in web workers: #11598

@vilkinsons
Copy link

@lforst Yet to test this, but just wanted to say thanks. Appreciate this issue getting some love, as it's been a big blocker to us in several respects 🚀

@lforst
Copy link
Member Author

lforst commented Apr 17, 2024

@nonparibus Yeah sorry about neglecting this. Personally, I would love for us to have some lightweight SDK that doesn't depend on any runtime specific API. Technically this is already possible for you to implement but it requires a ton of boilerplate.

@stevenmason
Copy link

Hi there, I am trying to integrate Sentry with my extension to log errors from the background service worker and an offscreen webpage.
Logging messages works however logging Errors doesn't. I get the following from the debug log:
image

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 2 Apr 18, 2024
@lforst
Copy link
Member Author

lforst commented Apr 18, 2024

@stevenmason This message should appear as part of the deduplicating logic in the SDK. Are you attempting to send the same error instance twice? Or is this happening 100% of the time?

@stevenmason
Copy link

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

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 2 Apr 18, 2024
@lforst
Copy link
Member Author

lforst commented Apr 18, 2024

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

@getsantry
Copy link

getsantry bot commented Jul 9, 2024

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 🥀

@getsantry getsantry bot added the Stale label Jul 9, 2024
@getsantry getsantry bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Archived in project
Archived in project
Development

No branches or pull requests