Reputation: 341
I use javascript-obfuscator package, it works fine if I do operations with files, like:
javascript-obfuscator source.js
but I need to dynamically change js source content and get output on console in way like
javascript-obfuscator "var foo = 'bar'; alert(foo);"
Any suggestion how can I get rid from saving content to file and do it like in snippet above?
Upvotes: 2
Views: 5087
Reputation: 169032
Not really considering why you'd need to do this, it looks like you'll need to use the programmatic API as described in the library's README.
Let's call this obfuscate.js
:
var JavaScriptObfuscator = require('javascript-obfuscator');
var obfuscationResult = JavaScriptObfuscator.obfuscate(
process.argv[2],
{
compact: false,
controlFlowFlattening: true
}
);
console.log(obfuscationResult.getObfuscatedCode());
$ node obfuscate.js 'console.log(1)'
will then output (for example)
var _0x2b5a = ['log'];
(function (_0x630038, _0x2944a9) {
var _0x83df37 = function (_0x2ef1a5) {
while (--_0x2ef1a5) {
_0x630038['push'](_0x630038['shift']());
}
};
_0x83df37(++_0x2944a9);
}(_0x2b5a, 0xd7));
var _0x493b = function (_0x2b48eb, _0x33884a) {
_0x2b48eb = _0x2b48eb - 0x0;
var _0x41338b = _0x2b5a[_0x2b48eb];
return _0x41338b;
};
console[_0x493b('0x0')](0x1);
Upvotes: 4