[1] 好吧,老实说,我什至不知道如何调用它。所以让我得到一些半伪代码,来展示我想要做什么。我使用 jQuery 从 AJAX 调用获取页面内 createDocument()
元素内声明的现有脚本。
GM_xmlhttprequest({ ... load:function(r){ var doc = document_from_string(r.responseText); script_content = $('body script:regex(html, local_xw_sig)', doc).html(); var scriptEl = document.createElement('script'); scriptEl.type = 'text/javascript'; scriptEl.innerHTML = script_content; // good till here (function(sc){ eval(sc.innerHTML); // not exactly like this, but you get the idea, errors alert('wont get here ' + local_xw_sig); // local_xw_sig is a global "var" inside the source })(scriptEl); } });
到目前为止一切顺利,脚本确实包含整个脚本 block 的源代码。现在,在这个“script_content”中,有自动执行函数,例如 $(document).ready(function(){...}) ,我“评估”innerHTML 的所有内容,它都会执行此代码,停止我的封装脚本。比如不存在的变量等等。
使用正则表达式删除脚本的某些部分并不是真正的选择...我真正想要的是“行走”在函数内部。就像做一个(完全虚构):
script = eval("function(){" + script_content + "};"); alert(script['local_xw_sig']); // a03ucc34095cw3495
有什么方法可以“反汇编”该函数,并能够访问其中的“var”吗? 就像这个函数:
function hello(){ var message = "hello"; } alert(hello.message); // message = var inside the function
有可能吗?或者我将不得不使用正则表达式破解我的方式? ;P
[2] 另外,有什么方法可以访问使用“createDocument”创建的文档中的 javascript 吗?
最佳答案
由于作用域的原因,简单地尝试从函数外部访问函数内部的局部变量是不可能的。然而,使用闭包你绝对可以做到这一点:
function hello(msg){
return function message(){
return msg;
}
}
alert(hello("yourMessage")()); // will alert "yourMessage"
请准确注意这里发生的情况。您正在调用一个返回函数的函数,其中“yourMessage”
现在在其作用域内定义。第二次调用该内部闭包将产生您之前设置的变量。
如果你对JS中的闭包不熟悉,我建议你阅读this wonderful FAQ .
关于javascript - "Decompile"JavaScript函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2528768/