I am using Google Closure Compiler in "SIMPLE_OPTIMIZATIONS" mode. The JavaScript uses an "Eval" statement with the variable "_u" embedded in the string. When Google Closure Compiler obfuscates the code, the variable name is changed to "a" and I get an error that "_u" is not defined in the console. My understanding is that an Extern will solve this problem, but I'm not sure how to write it. Thoughts?
Code Snippet:
var FuncName = (function(){
var ht=escape(_w.location.href)
function _fC(_u){
_aT=_sp+',\\/,\\.,-,_,'+_rp+',%2F,%2E,%2D,%5F';
_aA=_aT.split(',');
for(i=0;i<5;i++){
eval('_u=_u.replace(/'+_aA[i]+'/g,_aA[i+5])')
}
return _u
};
return {
O_LC:function(){
_w.open('https://someurl?referer='+_fC(_ht))
}
};
})();
After Google Closure Compiler modifies the code:
var FuncName = function() {
function a(a) {
_aT = _sp + ",\\/,\\.,-,_," + _rp + ",%2F,%2E,%2D,%5F";
_aA = _aT.split(",");
for (i = 0;5 > i;i++) {
eval("_u=_u.replace(/" + _aA[i] + "/g,_aA[i+5])");
}
return a;
}
escape(_w.location.href);
return {O_LC:function() {
_w.open("https://someurl?referer=" + a(_ht));
}};
}();
You can only prevent renaming of global variables or properties with externs. Locals will always be renamed. You can modify the code in a number of ways to fix this, including using a function constructor:
new Function("_u", "... function body ...")
But I would rewrite the code to avoid using eval to construct the regex. This would work:
_u=_u.replace(new RegExp(_aA[i], "g"), _aA[i+5]);
You also may be interested in:
Escape string for use in Javascript regex
General documentation regarding the Closure Compiler limitations are here:
https://developers.google.com/closure/compiler/docs/limitations