Question

Are any of the popular JavaScript obfuscators capable of inlining constants and renaming public properties/methods of objects?

ie.

var CONST1 = 5;
var CONST2 = 10;

function Test() {
    abc = 123;
    this.xyz = 234;
}

Test.prototype = {
    do_it: function(argX, argY) {
        return (argX + abc) / CONST1 + (argY + this.xyz) / CONST2;
    }
};

document.getElementById("button").onclick = function() {
    x = document.getElementById("x").value;
    y = document.getElementById("y").value;
    alert("Done it: " + new Test().do_it(x, y));
}
  • I would like CONST1 and CONST2 to effectively disappear and have their values inlined.

  • I would like all of the names (abc, xyz, Test, do_it, argX, argY) to be replaced/mangled.

  • I would like the obfuscator to assume that nothing external relies on any object/method/constant names in my source.

Can Google Closure or any other obfuscator do this?

Was it helpful?

Solution 2

Yes, Closure Compiler can do so in advanced compilation mode, however annotations are required to prevent automatic removal of the prototype property. For example, this:

var CONST1 = 5;
var CONST2 = 10;

function Test() {
    abc = 123;
    this.xyz = 234;
}

Test.prototype = {
    do_it: function(argX, argY) {
        return (argX + abc) / CONST1 + (argY + this.xyz) / CONST2;
    }
};

is annotated as this:

var CONST1 = 5;
var CONST2 = 10;

/** @constructor */
function Test() {

abc = 123;
this.xyz = 234;
}

/** @expose */
Test.prototype = {
do_it: function(argX, argY) {
return (argX + abc) / CONST1 + (argY + this.xyz) / CONST2;
}
};

window["Test"] = Test;

then when it is run, converted to this:

function a() {
  abc = 123;
  this.a = 234;
}
a.prototype = {b:function(b, c) {
  return(b + abc) / 5 + (c + this.a) / 10;
}};
window.Test = a;

OTHER TIPS

Google Closure can do all of the above. They have a web-based UI available at http://closure-compiler.appspot.com/home for you to test with your own code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top