开发者

Manipulating Javascript with Java

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-arra开发者_运维百科nge 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.


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜