2019-01-16 22:56:18 +00:00
|
|
|
import { EXTERNAL_LINK_REGEX } from '../../server/utils/fileUtils';
|
2018-05-04 20:36:59 +00:00
|
|
|
|
|
|
|
export const hijackConsoleErrorsScript = (offs) => {
|
|
|
|
const s = `
|
|
|
|
function getScriptOff(line) {
|
|
|
|
var offs = ${offs};
|
|
|
|
var l = 0;
|
|
|
|
var file = '';
|
|
|
|
for (var i=0; i<offs.length; i++) {
|
|
|
|
var n = offs[i][0];
|
|
|
|
if (n < line && n > l) {
|
|
|
|
l = n;
|
|
|
|
file = offs[i][1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [line - l, file];
|
|
|
|
}
|
|
|
|
// catch reference errors, via http://stackoverflow.com/a/12747364/2994108
|
|
|
|
window.onerror = function (msg, url, lineNumber, columnNo, error) {
|
|
|
|
var string = msg.toLowerCase();
|
|
|
|
var substring = "script error";
|
|
|
|
var data = {};
|
|
|
|
if (url.match(${EXTERNAL_LINK_REGEX}) !== null && error.stack){
|
|
|
|
var errorNum = error.stack.split('about:srcdoc:')[1].split(':')[0];
|
|
|
|
var fileInfo = getScriptOff(errorNum);
|
|
|
|
data = msg + ' (' + fileInfo[1] + ': line ' + fileInfo[0] + ')';
|
|
|
|
} else {
|
|
|
|
var fileInfo = getScriptOff(lineNumber);
|
|
|
|
data = msg + ' (' + fileInfo[1] + ': line ' + fileInfo[0] + ')';
|
|
|
|
}
|
|
|
|
window.parent.postMessage([{
|
2018-11-20 23:00:54 +00:00
|
|
|
log: [{
|
|
|
|
method: 'error',
|
|
|
|
data: [data],
|
|
|
|
id: Date.now().toString()
|
|
|
|
}],
|
2018-05-04 20:36:59 +00:00
|
|
|
source: fileInfo[1]
|
|
|
|
}], '*');
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
`;
|
|
|
|
return s;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const startTag = '@fs-';
|
|
|
|
|
|
|
|
export const getAllScriptOffsets = (htmlFile) => {
|
|
|
|
const offs = [];
|
2019-08-27 17:02:07 +00:00
|
|
|
const hijackConsoleErrorsScriptLength = 36;
|
|
|
|
const embeddedJSStart = 'script crossorigin=""';
|
|
|
|
let foundJSScript = true;
|
|
|
|
let foundEmbeddedJS = true;
|
2018-05-04 20:36:59 +00:00
|
|
|
let lastInd = 0;
|
|
|
|
let ind = 0;
|
|
|
|
let endFilenameInd = 0;
|
|
|
|
let filename = '';
|
|
|
|
let lineOffset = 0;
|
2019-08-27 17:02:07 +00:00
|
|
|
while (foundJSScript) {
|
2018-05-04 20:36:59 +00:00
|
|
|
ind = htmlFile.indexOf(startTag, lastInd);
|
|
|
|
if (ind === -1) {
|
2019-08-27 17:02:07 +00:00
|
|
|
foundJSScript = false;
|
2018-05-04 20:36:59 +00:00
|
|
|
} else {
|
|
|
|
endFilenameInd = htmlFile.indexOf('.js', ind + startTag.length + 3);
|
|
|
|
filename = htmlFile.substring(ind + startTag.length, endFilenameInd);
|
2019-08-27 17:02:07 +00:00
|
|
|
lineOffset = htmlFile.substring(0, ind).split('\n').length + hijackConsoleErrorsScriptLength;
|
|
|
|
offs.push([lineOffset, filename]);
|
|
|
|
lastInd = ind + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lastInd = 0;
|
|
|
|
while (foundEmbeddedJS) {
|
|
|
|
ind = htmlFile.indexOf(embeddedJSStart, lastInd);
|
|
|
|
if (ind === -1) {
|
|
|
|
foundEmbeddedJS = false;
|
|
|
|
} else {
|
|
|
|
filename = 'index.html';
|
|
|
|
// not sure where the offset of 25 comes from
|
|
|
|
lineOffset = htmlFile.substring(0, ind).split('\n').length + 25;
|
2018-05-04 20:36:59 +00:00
|
|
|
offs.push([lineOffset, filename]);
|
|
|
|
lastInd = ind + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return offs;
|
|
|
|
};
|