Anonymous javascript functions, how to access the source code?

I have JS code that gets into the random function Anonymous js. I want this code (e.g. alert('hello') ) to dump / warning the entire block / script object into which it was entered.

sort of like document.body.innerHTML, but for an anonymous function block

The result should look like this:

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

or

 Try { some code; mycode; } catch(e) { } 
+4
source share
3 answers
  • Pay attention to your conditions. "(browser) script block" literally means the code of the script element by specification . Use “javascript block” or “javascript object” to mean block or a object . Do not create confusing new terms; read and explore.

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

  • Backtracking, now you can use Function.caller to get the function that calls 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 

Please note that you get the entire function code, not the function block code, which excludes the parameters and function name.

  1. The .caller function may be removed in the future , for example, arguments.caller . (Both problems. What if the cross origin function in the call stack contains private api key code? How should the js mechanisms inline your code?)

    When the time comes or when the caller is null (when it is global code), you can still get the textual stacktrace ( new Error().stack ) and the current script element ( document.currentScript ), but their capabilities are quite limited.

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

  2. Your question sounds like an XY issue. You want to do what a modern language should not do, but never say for what purpose. Try to describe your real problem.

+3
source

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

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

So you can warn him:

 alert(fn.toString()); 

You can register it in js console:

 console.log(fn.toString()); 

Or even write it to a page.

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

However, this will not work for every function in the universe.

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

Some functions are not implemented using javascript, but in compiled browser code or JS engine. For these features provided by the environment, you will get this less useful output.

+1
source

If you are not in strict mode, you can raise the stack from what was referenced (i.e. the named function expression) using (non-standard) .caller

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

Now you can do something like

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

This only works for functions; you cannot do this on top-level code.


The best way to actually learn your code is through the console, and you can use the debugger; statement debugger; or breakpoints to exactly follow what happens and when.

0
source

Source: https://habr.com/ru/post/1439940/


All Articles