@@ -218,35 +218,19 @@ function instrumentXHR(): void {
218
218
219
219
fill ( xhrproto , 'open' , function ( originalOpen : ( ) => void ) : ( ) => void {
220
220
return function ( this : SentryWrappedXMLHttpRequest , ...args : any [ ] ) : void {
221
+ const xhr = this ; // tslint:disable-line:no-this-assignment
221
222
const url = args [ 1 ] ;
222
- this . __sentry_xhr__ = {
223
+ xhr . __sentry_xhr__ = {
223
224
method : isString ( args [ 0 ] ) ? args [ 0 ] . toUpperCase ( ) : args [ 0 ] ,
224
225
url : args [ 1 ] ,
225
226
} ;
226
227
227
228
// if Sentry key appears in URL, don't capture it as a request
228
- if ( isString ( url ) && this . __sentry_xhr__ . method === 'POST' && url . match ( / s e n t r y _ k e y / ) ) {
229
- this . __sentry_own_request__ = true ;
229
+ if ( isString ( url ) && xhr . __sentry_xhr__ . method === 'POST' && url . match ( / s e n t r y _ k e y / ) ) {
230
+ xhr . __sentry_own_request__ = true ;
230
231
}
231
232
232
- return originalOpen . apply ( this , args ) ;
233
- } ;
234
- } ) ;
235
-
236
- fill ( xhrproto , 'send' , function ( originalSend : ( ) => void ) : ( ) => void {
237
- return function ( this : SentryWrappedXMLHttpRequest , ...args : any [ ] ) : void {
238
- const xhr = this ; // tslint:disable-line:no-this-assignment
239
- const commonHandlerData = {
240
- args,
241
- startTimestamp : Date . now ( ) ,
242
- xhr,
243
- } ;
244
-
245
- triggerHandlers ( 'xhr' , {
246
- ...commonHandlerData ,
247
- } ) ;
248
-
249
- xhr . addEventListener ( 'readystatechange' , function ( ) : void {
233
+ const onreadystatechangeHandler = function ( ) : void {
250
234
if ( xhr . readyState === 4 ) {
251
235
try {
252
236
// touching statusCode in some platforms throws
@@ -258,10 +242,35 @@ function instrumentXHR(): void {
258
242
/* do nothing */
259
243
}
260
244
triggerHandlers ( 'xhr' , {
261
- ... commonHandlerData ,
245
+ args ,
262
246
endTimestamp : Date . now ( ) ,
247
+ startTimestamp : Date . now ( ) ,
248
+ xhr,
263
249
} ) ;
264
250
}
251
+ } ;
252
+
253
+ if ( 'onreadystatechange' in xhr && typeof xhr . onreadystatechange === 'function' ) {
254
+ fill ( xhr , 'onreadystatechange' , function ( original : WrappedFunction ) : Function {
255
+ return function ( ...readyStateArgs : any [ ] ) : void {
256
+ onreadystatechangeHandler ( ) ;
257
+ return original . apply ( xhr , readyStateArgs ) ;
258
+ } ;
259
+ } ) ;
260
+ } else {
261
+ xhr . addEventListener ( 'readystatechange' , onreadystatechangeHandler ) ;
262
+ }
263
+
264
+ return originalOpen . apply ( xhr , args ) ;
265
+ } ;
266
+ } ) ;
267
+
268
+ fill ( xhrproto , 'send' , function ( originalSend : ( ) => void ) : ( ) => void {
269
+ return function ( this : SentryWrappedXMLHttpRequest , ...args : any [ ] ) : void {
270
+ triggerHandlers ( 'xhr' , {
271
+ args,
272
+ startTimestamp : Date . now ( ) ,
273
+ xhr : this ,
265
274
} ) ;
266
275
267
276
return originalSend . apply ( this , args ) ;
0 commit comments