简体   繁体   English

匿名函数javascript,如何访问源代码?

[英]anonymous functions javascript, how to access source code?

I got some JS Code that gets inside a random Anonymous js function. 我有一些JS代码进入一个随机的匿名js函数。 I want that code (for example alert('hello') ) to dump/alert the entire script block/object which it was injected into. 我希望该代码(例如alert('hello') )转储/警告注入它的整个脚本块/对象。

kinda like document.body.innerHTML but for the anonymous function block 有点像document.body.innerHTML,但对于匿名功能块

result should be like : 结果应该是这样的:

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

or 要么

Try { some code; mycode; } catch(e) { }
  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 . 使用“javascript block” “javascript 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 : 退一步,现在您可以使用Function.caller来获取调用代码的函数

 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 . Function.caller将来可能会被删除 ,例如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?) (两者都很麻烦。如果调用堆栈上的交叉原点函数包含私有api密钥代码怎么办?js引擎如何内联你的代码呢?)

    When the time comes, 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. 当时间到了,或者当调用者为空时 (当它是全局代码时),你仍然可以获得文本 new Error().stack 跟踪new Error().stack )和当前脚本元素document.currentScript ),但是它们的功能很漂亮有限。

    You can get a script element's code - if any - with its textContent or innerHTML property. 您可以使用textContent或innerHTML属性获取脚本元素的代码(如果有)。

  2. Your question sounds like an XY Problem . 你的问题听起来像一个XY问题 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. 函数具有toString()方法。 (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: 您可以将其记录到js控制台:

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. 但是,这不适用于Universe中的每个函数。

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

Some functions are not implemented with javascript, but in the compiled code of the browser or JS engine. 有些函数不是用javascript实现的,而是在浏览器或JS引擎的编译代码中实现的。 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 (ie a named function expression) using (non-standard) .caller 如果你没有处于严格模式,你可以使用(非标准) .caller从可引用的东西(即命名函数表达式) .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; 探索代码的最佳方法实际上是使用Console ,您可以使用debugger; statement or breakpoints to follow exactly what is happening and when. 声明或断点,以确切了解发生的事情和时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM