-
Notifications
You must be signed in to change notification settings - Fork 0
/
caller.js
39 lines (38 loc) · 1.38 KB
/
caller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// https://stackoverflow.com/a/22165114/2191332
export function getCallerInfo() {
const result = {};
try {
//Throw an error to generate a stack trace
throw new Error();
} catch(e) {
//Split the stack trace into each line
var stackLines = e.stack.split('\n');
var callerIndex = 0;
//Now walk though each line until we find a path reference
for(var i in stackLines){
if(!stackLines[i].match(/:\/\//)) continue;
//We skipped all the lines with out an http so we now have a script reference
//This one is the class constructor, the next is the getScriptPath() call
//The one after that is the user code requesting the path info (so offset by 2)
callerIndex = Number(i) + 2;
break;
}
//Now parse the string for each section we want to return
if (stackLines.length > callerIndex) {
const relevantLine = stackLines[callerIndex];
const httpUrl = relevantLine.match(/(http[s]?:\/\/.+\/[^\/]+\.js):/);
if (httpUrl && httpUrl.length > 1) {
result.url = httpUrl[1];
}
const fileName = relevantLine.match(/\/([^\/]+\.js):/);
if (fileName && fileName.length > 1) {
result.fileName = fileName[1];
}
const filePath = relevantLine.match(/file:\/\/(\/.+\.js):/);
if (filePath && filePath.length > 1) {
result.filePath = filePath[1];
}
}
}
return result;
}