@@ -127,6 +127,9 @@ declare const __SENTRY_RELEASE__: string | undefined;
127
127
* @see {@link BrowserOptions } for documentation on configuration options.
128
128
*/
129
129
export function init ( browserOptions : BrowserOptions = { } ) : Client | undefined {
130
+ if ( _checkBailForBrowserExtension ( browserOptions ) ) {
131
+ return ;
132
+ }
130
133
return _init ( browserOptions , getDefaultIntegrations ( browserOptions ) ) ;
131
134
}
132
135
@@ -142,30 +145,18 @@ export function initWithDefaultIntegrations(
142
145
browserOptions : BrowserOptions = { } ,
143
146
getDefaultIntegrationsImpl : ( options : BrowserOptions ) => Integration [ ] ,
144
147
) : BrowserClient | undefined {
148
+ if ( _checkBailForBrowserExtension ( browserOptions ) ) {
149
+ return ;
150
+ }
151
+
145
152
return _init ( browserOptions , getDefaultIntegrationsImpl ( browserOptions ) ) ;
146
153
}
147
154
148
155
/**
149
156
* Acutal implementation shared by init and initWithDefaultIntegrations.
150
157
*/
151
- function _init (
152
- browserOptions : BrowserOptions = { } ,
153
- defaultIntegrations : Integration [ ] ,
154
- ) : BrowserClient | undefined {
158
+ function _init ( browserOptions : BrowserOptions = { } , defaultIntegrations : Integration [ ] ) : BrowserClient {
155
159
const options = applyDefaultOptions ( browserOptions ) ;
156
-
157
- const isBrowserExtension = ! options . skipBrowserExtensionCheck && shouldShowBrowserExtensionError ( ) ;
158
-
159
- /* if (DEBUG_BUILD) {
160
- logBrowserEnvironmentWarnings({
161
- browserExtension: isBrowserExtension,
162
- fetch: !supportsFetch(),
163
- });
164
- }
165
- */
166
- if ( isBrowserExtension ) {
167
- return ;
168
- }
169
160
const clientOptions = getClientOptions ( options , defaultIntegrations ) ;
170
161
return initAndBind ( BrowserClient , clientOptions ) ;
171
162
}
@@ -253,52 +244,49 @@ export function onLoad(callback: () => void): void {
253
244
}
254
245
255
246
function shouldShowBrowserExtensionError ( ) : boolean {
256
- const windowWithMaybeExtension =
257
- typeof WINDOW . window !== 'undefined' && ( WINDOW as typeof WINDOW & ExtensionProperties ) ;
258
- if ( ! windowWithMaybeExtension ) {
247
+ if ( typeof WINDOW . window === 'undefined' ) {
259
248
// No need to show the error if we're not in a browser window environment (e.g. service workers)
260
249
return false ;
261
250
}
262
251
263
- const extensionKey = windowWithMaybeExtension . chrome ? 'chrome' : 'browser' ;
264
- const extensionObject = windowWithMaybeExtension [ extensionKey ] ;
252
+ const _window = WINDOW as typeof WINDOW & ExtensionProperties ;
265
253
266
- const runtimeId = extensionObject ?. runtime ?. id ;
267
- const href = getLocationHref ( ) || '' ;
254
+ // Running the SDK in NW.js, which appears like a browser extension but isn't, is also fine
255
+ // see: https://github.com/getsentry/sentry-javascript/issues/12668
256
+ if ( _window . nw ) {
257
+ return false ;
258
+ }
259
+
260
+ const extensionObject = _window [ 'chrome' ] || _window [ 'browser' ] ;
268
261
269
- const extensionProtocols = [ 'chrome-extension:' , 'moz-extension:' , 'ms-browser-extension:' , 'safari-web-extension:' ] ;
262
+ if ( ! extensionObject ?. runtime ?. id ) {
263
+ return false ;
264
+ }
265
+
266
+ const href = getLocationHref ( ) ;
267
+ const extensionProtocols = [ 'chrome-extension' , 'moz-extension' , 'ms-browser-extension' , 'safari-web-extension' ] ;
270
268
271
269
// Running the SDK in a dedicated extension page and calling Sentry.init is fine; no risk of data leakage
272
270
const isDedicatedExtensionPage =
273
- ! ! runtimeId && WINDOW === WINDOW . top && extensionProtocols . some ( protocol => href . startsWith ( `${ protocol } //` ) ) ;
271
+ WINDOW === WINDOW . top && extensionProtocols . some ( protocol => href . startsWith ( `${ protocol } : //` ) ) ;
274
272
275
- // Running the SDK in NW.js, which appears like a browser extension but isn't, is also fine
276
- // see: https://github.com/getsentry/sentry-javascript/issues/12668
277
- const isNWjs = typeof windowWithMaybeExtension . nw !== 'undefined' ;
278
-
279
- return ! ! runtimeId && ! isDedicatedExtensionPage && ! isNWjs ;
273
+ return ! isDedicatedExtensionPage ;
280
274
}
281
275
282
- function logBrowserEnvironmentWarnings ( {
283
- fetch,
284
- browserExtension,
285
- } : {
286
- fetch : boolean ;
287
- browserExtension : boolean ;
288
- } ) : void {
289
- if ( browserExtension ) {
290
- consoleSandbox ( ( ) => {
291
- // eslint-disable-next-line no-console
292
- console . error (
293
- '[Sentry] You cannot run Sentry this way in a browser extension, check: https://docs.sentry.io/platforms/javascript/best-practices/browser-extensions/' ,
294
- ) ;
295
- } ) ;
296
- }
276
+ function _checkBailForBrowserExtension ( options : BrowserOptions ) : true | void {
277
+ const isBrowserExtension = ! options . skipBrowserExtensionCheck && shouldShowBrowserExtensionError ( ) ;
278
+
279
+ if ( isBrowserExtension ) {
280
+ if ( DEBUG_BUILD ) {
281
+ consoleSandbox ( ( ) => {
282
+ // eslint-disable-next-line no-console
283
+ console . error (
284
+ '[Sentry] You cannot run Sentry this way in a browser extension, check: https://docs.sentry.io/platforms/javascript/best-practices/browser-extensions/' ,
285
+ ) ;
286
+ } ) ;
287
+ }
297
288
298
- if ( fetch ) {
299
- logger . warn (
300
- 'No Fetch API detected. The Sentry SDK requires a Fetch API compatible environment to send events. Please add a Fetch API polyfill.' ,
301
- ) ;
289
+ return true ;
302
290
}
303
291
}
304
292
0 commit comments