2

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.

2
  • Wouldn't it be easier to include the function into you Java-Code, BEFORE you GWT-compile it to JavaScript? Commented Jan 18, 2011 at 7:57
  • I think you're referring to an inline native JS function in GWT? I don't think that will work, this code is in the Javascript loader, not the compiled application. If you know of a way to make that work please let me know. Commented Jan 18, 2011 at 15:23

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That would be the ideal solution, I'm actually using the cross-site linker (xs). I couldn't find any docs/example on how to do that.
1

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.

Comments

1

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.

3 Comments

I thought about that, especially since I'm targeting the very end of the file, but the regex would get pretty crazy and I'm trying to do something that works for both the PRETTY and OBFUSCATED outputs so I though parsing JS is the way to go...
But do you need regex? Sometimes string splitting is much simpler and almost always performance better to boot :)
I see where you're going with this, not a bad idea. I'll give it a shot and see if it works
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.