anonymous functions javascript, how to access source code? - Stack Overflow

I got some JS Code that gets inside a random Anonymous js function.I want that code (for example alert

I got some JS Code that gets inside a random Anonymous js function. I want that code (for example alert('hello') ) to dump/alert the entire script block/object which it was injected into.

kinda like document.body.innerHTML but for the anonymous function block

result should be like :

Function()({ somecode; MyAlert(...) } )()

or

Try { some code; mycode; } catch(e) { }

I got some JS Code that gets inside a random Anonymous js function. I want that code (for example alert('hello') ) to dump/alert the entire script block/object which it was injected into.

kinda like document.body.innerHTML but for the anonymous function block

result should be like :

Function()({ somecode; MyAlert(...) } )()

or

Try { some code; mycode; } catch(e) { }
Share Improve this question edited Oct 15, 2012 at 23:59 Ibu 43.9k14 gold badges82 silver badges109 bronze badges asked Oct 15, 2012 at 23:57 atigertestatigertest 811 gold badge2 silver badges5 bronze badges 9
  • 1 Try alert(arguments.callee);. – Šime Vidas Commented Oct 16, 2012 at 0:04
  • arguments is undefiend in my case (its try case) – atigertest Commented Oct 16, 2012 at 0:13
  • Is that try-statement inside a function, or is it global code? (When a function code is invoked, an arguments object is created for that invocation. So, you are either in global code, or the arguments object was manually overwritten.) – Šime Vidas Commented Oct 16, 2012 at 0:16
  • you are accurate, I am sorry if i misled with the word function, it is in global code as a separated script block, not a function. – atigertest Commented Oct 16, 2012 at 0:28
  • By default, once your JavaScript code is injected into a script of a web-page, your code has full-access to the DOM, so you can query the SCRIPT elements of that page, and retrieve the source text of those scripts. – Šime Vidas Commented Oct 16, 2012 at 0:54
 |  Show 4 more ments

3 Answers 3

Reset to default 3 +50
  1. Mind your terms. "(browser) script block" literally means script element's code by the spec. Use "javascript block" or "javascript object" to mean a block or an object. Do not create confusing new terms; do read and research.

  2. Blocks are not objects; they are language statements. Just like you cannot "get the code/variables of current line", you cannot "get the code/variables of current block", try block or not.

  3. Stepping back, for now you can use Function.caller to get the function calling your code:

var mycode = function me(){ if ( me.caller ) alert( me.caller.toString() ); };
(function(){ var some = 'code'; mycode(); })();
// Alert "function(){ var some = 'code'; mycode(); }", even when it is anonymous

Note that you get the whole function's code, not the function block's code which excludes parameters and function name.

  1. Function.caller may be removed in future, like arguments.caller. (Both are troubles. What if a cross origin function on the call stack contains private api key code? How should js engines inline your code?)

    When the time es, or when caller is null (when it is global code), you may still be able to get textual stacktrace (new Error().stack) and current script element (document.currentScript), but their capabilities are pretty limited.

    You can get a script element's code - if any - with its textContent or innerHTML property.

  2. Your question sounds like an XY Problem. You want to do something that no modern language is meant to do, but never say for what purpose. Try to describe your real problem.

Functions have a toString() method. (Yes functions have methods!)

var fn = function() { alert('hello') };
fn.toString() // "function() { alert('hello') };"

So you can alert it:

alert(fn.toString());

You can log it to the js console:

console.log(fn.toString());

Or even write it to the page.

document.getElementById('someID').innerHTML = fn.toString();

However, this won't work for every function in the universe.

[].push.toString()
"function push() { [native code] }"

Some functions are not implemented with javascript, but in the piled code of the browser or JS engine. For these environment provided functions, you will get this above less helpful output.

If you're not in strict mode you can go up the stack from something which was referenceable (i.e. a named function expression) using (non-standard) .caller

function getFunctionReference(callback) {
    var ref = getFunctionReference.caller;
    if (callback) callback(ref);
    return ref;
}

Now you can do things like

(function () {
    getFunctionReference(alert);
}());
// alerts the .toString of the IIFE

This only works for functions, you can't do this on top level code.


The best way to explore your code is actually with the Console, and you can use the debugger; statement or breakpoints to follow exactly what is happening and when.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745286256a4620580.html

相关推荐

  • anonymous functions javascript, how to access source code? - Stack Overflow

    I got some JS Code that gets inside a random Anonymous js function.I want that code (for example alert

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信