检测 JavaScript

发布于 2024-08-07 11:53:52 字数 361 浏览 6 评论 0原文

我想检测 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

夏见 2024-08-14 11:53:52

如果您不限于 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.

烟火散人牵绊 2024-08-14 11:53:52

这可能就是您正在寻找的内容:

http://ejohn.org/blog/ javascript-getters-and-setters/

将日志放入 setter 中即可。当然,我不知道这实际上在哪里起作用,是否必须是受信任的代码,或者任何网页,或者什么。

根据 John Resig 的代码,我会在调试代码中添加以下内容:

var placeLoggerInSetter = function(variableName, obj){ //obj may be window
    obj.__defineSetter__(variableName, function(val){
         console.log(val); //log whatever here
         obj[variableName] = val;
    });
}

var something = 0;
placeLoggerInSetter("something", window);
something = 1;
//out goes "1" in the console
something = 2
//out goes "2" in the console

是不是更像这样?再说一次,我没有测试它,并且 obj[variableName] 行对我来说看起来相当递归......你能在这里告诉我它是否有效吗? :)

另外,使用 Firebug

console.dir(obj)

您可以在控制台中看到该对象的所有属性,而不是像 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:

var placeLoggerInSetter = function(variableName, obj){ //obj may be window
    obj.__defineSetter__(variableName, function(val){
         console.log(val); //log whatever here
         obj[variableName] = val;
    });
}

var something = 0;
placeLoggerInSetter("something", window);
something = 1;
//out goes "1" in the console
something = 2
//out goes "2" in the console

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,

console.dir(obj)

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.

内心激荡 2024-08-14 11:53:52

Firebug(Firefox 的插件)具有日志记录功能,您可以将其写入控制台。比使用alert(“foo”)好得多;对于所有

示例日志语句:

console.log("foo="+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:

console.log("foo="+foo);

See this post for more info (including more advanced usage)

她比我温柔 2024-08-14 11:53:52

我不知道通用的解决方案或任何存在的东西,但只是将这些变量包装在处理 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.

琉璃繁缕 2024-08-14 11:53:52

可以设置调试器;作为文档就绪事件的第一件事,只需逐步执行代码即可。

..弗雷德里克

You can set debugger; as first thing on the document ready event and just step through your code step by step.

..fredrik

小嗲 2024-08-14 11:53:52

我使用 Rhino 的 cvshead 版本来检测代码。

Using the cvshead version of Rhino, I instrumented the code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文