0

I'm trying to detect caller function and where it came from.

For example :

Let assume we have following line on HTML content.

<script src="/somefunc.js"></script>

And following function located in somefunc.js

exampleFunction = function(){
    var f = document.getElementById("hello")
}

As you see exampleFunction is calling getElementById() function. In order to detect that calls, I've override getElemenyById() with following codes.

document._oldGetElementById = document.getElementById;
document.getElementById = function(elemIdOrName) {
    var result = document._oldGetElementById(elemIdOrName);
    if (! result) {
        var elems = document.getElementsByName(elemIdOrName); 
        if (elems && elems.length > 0) {
            result = elems[0];
        }
    }
    console.log("someone called me! Caller function = "+arguments.callee.caller);
    return result;
};

but I couldn't manage to get information which function caller and what is it location ?I mean I want to get information like following output.

Caller = exampleFunction
Caller location = site.com/somefunc.js

Any idea ?

Mehmet Ince
  • 1,298
  • 9
  • 20
  • exampleFunction = function(){ var f = document.getElementById("hello") } is anonymous. try adding function name to get the function name something like "exampleFunction = function exFunction(){ var f = document.getElementById("hello") }" – pmverma Dec 09 '14 at 11:53
  • pmverma - that sounds like an answer. To the OP - what output did you get? Just undefined's or something else? – Danny Staple Dec 09 '14 at 11:55
  • @DannyStaple I did not run this. But you cannot get the actual function name without giving a name to that..as Mehmet Ince does. Am I wrong? – pmverma Dec 09 '14 at 11:58
  • @pmverma Actually I'm getting full content of exampleFunction() via arguments.callee.caller . But main problem is getting function location.I need to know what is the location of caller function ? – Mehmet Ince Dec 09 '14 at 12:03
  • @MehmetInce arguments.callee.name return function name. and to be honest, getting function location, is outOFKnowledgeException for me. – pmverma Dec 09 '14 at 12:15
  • How about doing a search in js files for the function, via using an external script- a Python script maybe. You can use Ajax to run the script; there are answers on that: http://stackoverflow.com/questions/13175510/call-python-function-from-javascript-code – sha1 Dec 09 '14 at 13:20
  • Thanks @sha1 but what about external sources ? I need to do it on client side.. – Mehmet Ince Dec 09 '14 at 13:25
  • In the Python script, you might send a GET request to the server for the html file which ran the js function. After that, you can parse the HTML file, and look for references for js files. Then with GET requests, you can get the external js files, and search on them for the function. Not much straightforward though. – sha1 Dec 09 '14 at 13:47

0 Answers0