检测 JavaScript
我想检测 JavaScript 代码以便“记录”全局变量的值。例如,我想知道特定变量 foo 在执行期间的所有值。日志记录不是问题。
实现这个最简单的方法是什么?我正在考虑使用 Rhino (由 Mozilla 创建的 Java 中的 JavaScript 实现)生成 AST 并修改此 AST。
I would like to instrument JavaScript code in order to "log" values of global variables. For example I would like to know all the values a particular variables foo had during execution. The logging is not the problem.
What would be the easiest way to implement this? I was thinking of using Rhino (the JavaScript implementation in Java created by Mozilla) to generate an AST and modifying this AST.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您不限于 Rhino/Java,Johnson Ruby gem 将 Spidermonkey/Tracemonkey 解释器集成到 Ruby 中。它确实提供了对 AST 的访问。我还没有尝试过修改 AST;不确定这是否可能(从所查看的代码中的名称来看,可能是)。
Johnson gem 位于 github 上 jbarnette 的 Johnson 存储库中。 (切换 id;Stackoverflow 不希望我在此时添加链接...)我最近添加了tracemonkey 支持,但它尚未集成到主源中。它位于我 (smparkes) 在 github 上的 Johnson 存储库中。
If you're not restricted to Rhino/Java, the Johnson Ruby gem integrates the Spidermonkey/Tracemonkey interpreter into Ruby. It does provide access to the AST. I haven't tried modifying the AST; not sure if that's possible (it might be, judging from the names in the code at looked at).
The Johnson gem is in jbarnette's Johnson repo at github. (Switch id's; Stackoverflow doesn't want me to put links in at this point ...) I added tracemonkey support recently and it's not yet integrated into the master source. It's in my (smparkes)'s Johnson repo at github.
这可能就是您正在寻找的内容:
http://ejohn.org/blog/ javascript-getters-and-setters/
将日志放入 setter 中即可。当然,我不知道这实际上在哪里起作用,是否必须是受信任的代码,或者任何网页,或者什么。
根据 John Resig 的代码,我会在调试代码中添加以下内容:
是不是更像这样?再说一次,我没有测试它,并且
obj[variableName]
行对我来说看起来相当递归......你能在这里告诉我它是否有效吗? :)另外,使用 Firebug,
您可以在控制台中看到该对象的所有属性,而不是像
log()
那样将其转换为字符串。还有扩展属性等等。便利。This may be what you are looking for:
http://ejohn.org/blog/javascript-getters-and-setters/
Put a log in the setter and that's it. Of course, I do not know where does this actually work, if it must be trusted code, or any web page, or what.
Based on John Resig's code, I would add to my debug code something like:
Is that more like it? Again, I did not test it and the
obj[variableName]
line looks quite recursive to me... will you tell here if it works? :)Also, with Firebug,
will let you see all the properties of that object in the console, instead of converting it to string as with
log()
. And also expand the properties and so on. Handy.Firebug(Firefox 的插件)具有日志记录功能,您可以将其写入控制台。比使用alert(“foo”)好得多;对于所有
示例日志语句:
请参阅 this发布以获取更多信息(包括更高级的用法)
Firebug (plugin for firefox) has a logging feature, to which you can write to a console. Much better than using alert("foo"); for everything
example log statement:
See this post for more info (including more advanced usage)
我不知道通用的解决方案或任何存在的东西,但只是将这些变量包装在处理 get/sets 等内容的日志记录类中。
I have no idea for a generic solution or anything that exists, but simply wrap those variables around a logging class that deals with get/sets and stuff.
您可以使用 Dtrace:
http://opensolaris.org/os/project/mozilla-dtrace/
You can use Dtrace:
http://opensolaris.org/os/project/mozilla-dtrace/
可以设置调试器;作为文档就绪事件的第一件事,只需逐步执行代码即可。
..弗雷德里克
You can set debugger; as first thing on the document ready event and just step through your code step by step.
..fredrik
我使用 Rhino 的 cvshead 版本来检测代码。
Using the cvshead version of Rhino, I instrumented the code.