Skip to content

Commit 18cba74

Browse files
CaptObviouskamilogorek
authored andcommitted
feat: Handle JavaScript loaded in the browser inside a blob
1 parent 9ec1dd4 commit 18cba74

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

vendor/TraceKit/tracekit.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,47 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
441441
element.func = UNKNOWN_FUNCTION;
442442
}
443443

444+
if (element.url && element.url.substr(0, 5) === 'blob:') {
445+
// Special case for handling JavaScript loaded into a blob.
446+
// We use a synchronous AJAX request here as a blob is already in
447+
// memory - it's not making a network request. This will generate a warning
448+
// in the browser console, but there has already been an error so that's not
449+
// that much of an issue.
450+
var xhr = new XMLHttpRequest();
451+
xhr.open('GET', element.url, false);
452+
xhr.send(null);
453+
454+
// If we failed to download the source, skip the element.
455+
if (xhr.status === 200) {
456+
var source = xhr.responseText;
457+
458+
// We trim the source down to the last 300 characters here as
459+
// sourceMappingURL is usually at the end of the file.
460+
source = source.substr(source.length - 300);
461+
462+
// Now we dig out the source map URL
463+
var sourceMaps = source.match(/\/\/# {0,1}sourceMappingURL=(.*) {0,1}/);
464+
465+
// If we don't find a source map comment or we find more than one,
466+
// continue on to the next element. We check for a length of 2 as the
467+
// first result is always the entire match, subsequent indices are
468+
// the group matches.
469+
if (sourceMaps.length === 2) {
470+
var sourceMapAddress = sourceMaps[1];
471+
472+
// Now we check to see if it's a relative URL. If it is, convert it
473+
// to an absolute one.
474+
if (sourceMapAddress.substr(0, 1) === '~') {
475+
sourceMapAddress = window.location.origin + sourceMapAddress.substr(1);
476+
}
477+
478+
// Now we strip the '.map' off of the end of the URL and update the
479+
// element so that Sentry can match the map to the blob.
480+
element.url = sourceMapAddress.substr(0, sourceMapAddress.length - 4);
481+
}
482+
}
483+
}
484+
444485
stack.push(element);
445486
}
446487

0 commit comments

Comments
 (0)