@@ -127,6 +127,9 @@ declare const __SENTRY_RELEASE__: string | undefined;
127127 * @see {@link BrowserOptions } for documentation on configuration options.
128128 */
129129export function init ( browserOptions : BrowserOptions = { } ) : Client | undefined {
130+ if ( _checkBailForBrowserExtension ( browserOptions ) ) {
131+ return ;
132+ }
130133 return _init ( browserOptions , getDefaultIntegrations ( browserOptions ) ) ;
131134}
132135
@@ -142,30 +145,18 @@ export function initWithDefaultIntegrations(
142145 browserOptions : BrowserOptions = { } ,
143146 getDefaultIntegrationsImpl : ( options : BrowserOptions ) => Integration [ ] ,
144147) : BrowserClient | undefined {
148+ if ( _checkBailForBrowserExtension ( browserOptions ) ) {
149+ return ;
150+ }
151+
145152 return _init ( browserOptions , getDefaultIntegrationsImpl ( browserOptions ) ) ;
146153}
147154
148155/**
149156 * Acutal implementation shared by init and initWithDefaultIntegrations.
150157 */
151- function _init (
152- browserOptions : BrowserOptions = { } ,
153- defaultIntegrations : Integration [ ] ,
154- ) : BrowserClient | undefined {
158+ function _init ( browserOptions : BrowserOptions = { } , defaultIntegrations : Integration [ ] ) : BrowserClient {
155159 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- }
169160 const clientOptions = getClientOptions ( options , defaultIntegrations ) ;
170161 return initAndBind ( BrowserClient , clientOptions ) ;
171162}
@@ -253,52 +244,49 @@ export function onLoad(callback: () => void): void {
253244}
254245
255246function shouldShowBrowserExtensionError ( ) : boolean {
256- const windowWithMaybeExtension =
257- typeof WINDOW . window !== 'undefined' && ( WINDOW as typeof WINDOW & ExtensionProperties ) ;
258- if ( ! windowWithMaybeExtension ) {
247+ if ( typeof WINDOW . window === 'undefined' ) {
259248 // No need to show the error if we're not in a browser window environment (e.g. service workers)
260249 return false ;
261250 }
262251
263- const extensionKey = windowWithMaybeExtension . chrome ? 'chrome' : 'browser' ;
264- const extensionObject = windowWithMaybeExtension [ extensionKey ] ;
252+ const _window = WINDOW as typeof WINDOW & ExtensionProperties ;
265253
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' ] ;
268261
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' ] ;
270268
271269 // Running the SDK in a dedicated extension page and calling Sentry.init is fine; no risk of data leakage
272270 const isDedicatedExtensionPage =
273- ! ! runtimeId && WINDOW === WINDOW . top && extensionProtocols . some ( protocol => href . startsWith ( `${ protocol } //` ) ) ;
271+ WINDOW === WINDOW . top && extensionProtocols . some ( protocol => href . startsWith ( `${ protocol } : //` ) ) ;
274272
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 ;
280274}
281275
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+ }
297288
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 ;
302290 }
303291}
304292
0 commit comments