使用 Java 操作 Javascript

发布于 2024-10-13 03:58:54 字数 896 浏览 3 评论 0原文

我有一个复杂的生成的 Javascript 文件(它由 GWT 编译器生成),我需要能够以编程方式对此进行更改并输出该文件的“清理”版本。特别是,我有:

function bookmark(){
    // lots-o-javascript
    var M=Vb+s+I+Wb;n.write(Xb+Yb+Zb+$b+_b+ac+bc+$b+_b+cc+dc+ec+M+fc+gc+hc+ic)
}
bookmark();

未混淆,函数内部如下所示:

var compiledScriptTag = '"<script src=\\"' + base + strongName + '.cache.js\\"><\/scr" + "ipt>"';
$doc_0.write('<scr' + 'ipt><!-' + '-\n' + 'blah blah blah' + 'document.write(' + compiledScriptTag + ');' + '\n-' + '-><\/scr' + 'ipt>');

所以我需要做的是在 Java servlet 中,将上面两行转换为等价的:

eval('blah blah blah');
document.body.appendChild(document.createElement('script')).src=base + strongName + ".cache.js";

What are my best options to parse and re-安排这个 Javascript 文件吗?我应该研究一下 Rhino,它是否能够提供这些句柄(以及使用 $doc.write 编写的嵌套 Javascript)?任何想法将不胜感激。

I have a complex, generated Javascript file (it's generated by the GWT compiler), and I need to be able to programmatically make changes to this and output a 'cleaned' version of the file. In particular, I have:

function bookmark(){
    // lots-o-javascript
    var M=Vb+s+I+Wb;n.write(Xb+Yb+Zb+$b+_b+ac+bc+$b+_b+cc+dc+ec+M+fc+gc+hc+ic)
}
bookmark();

Un-obfuscated, the inside of the function looks like:

var compiledScriptTag = '"<script src=\\"' + base + strongName + '.cache.js\\"><\/scr" + "ipt>"';
$doc_0.write('<scr' + 'ipt><!-' + '-\n' + 'blah blah blah' + 'document.write(' + compiledScriptTag + ');' + '\n-' + '-><\/scr' + 'ipt>');

So what I need to do is in a Java servlet, transform the above two lines into the equivalent of:

eval('blah blah blah');
document.body.appendChild(document.createElement('script')).src=base + strongName + ".cache.js";

What are my best options to parse and re-arrange this Javascript file? Should I look into Rhino, would it be able to give handles to these (as well as the nested Javascript which is being written using $doc.write)? Any ideas would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

贱人配狗天长地久 2024-10-20 03:58:54

也许更优雅、更简单的选项是修改 iframe 链接器来更改您需要的内容,或者我认为您可以创建一个辅助链接器并在加载器被混淆之前更改行。链接器是我还没有尝试过的一件事,但我知道它们可能是做你想做的事情的最佳场所。

Perhaps the more elegant and less brute force option would be to modify the iframe linker to change what you need to, or I think you can create a secondary linker and change the lines before the loader gets obfuscated. Linkers are the one thing I have yet to play around with, but I know they are probably the best place for doing what you want to do.

断爱 2024-10-20 03:58:54

您可以将 -style PRETTY 添加到 GWT 编译器以获得未混淆的 JavaScript。

要使用 Rhino 评估 JavaScript,您需要提供特定于浏览器的对象,如文档、窗口……无论如何,document.write 都会使一切变得非常复杂。

您还可以使用提供的 ECMAScript 语法使用 ANTLRv3 解析 JavaScript,但我不确定这是否对您有帮助。

You can add -style PRETTY to GWT compiler to get unobfuscated JavaScript.

To evaluate your JavaScript with Rhino you will need to provide browser specific objects like document, window, ... In any case document.write makes all pretty complicated.

You can also parse JavaScript with ANTLRv3 with provided ECMAScript grammar but I am not sure whether this will help you.

高跟鞋的旋律 2024-10-20 03:58:54

由于您需要执行的转换非常具体,因此我能看到的最简单的解决方案是将 js 作为纯文本使用,而不是将其视为一种语言。

您可以按 + 进行拆分,然后获取相关的数组值。

Since the transformation you need to do is so specific, the easiest solution i can see is working with the js as pure text instead of treating it as a language.

You can split on + and then go get the array values that are relevant.

兲鉂ぱ嘚淚 2024-10-20 03:58:54

您可以在 Java 中使用 RegEx,但我建议使用常规字符串搜索函数并在新的 StringBuilder 对象中重建 JavaScript 代码。

You can use RegEx in Java but I would recommend using the regular String search functions and rebuilding the JavaScript code in a new StringBuilder object.

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