0

I am trying to follow this guide: https://github.com/javascript-obfuscator/javascript-obfuscator

Because I want to obfuscate my javascript.

I tried this but it returns an error: require() is not defined

<script src="https://cdn.jsdelivr.net/npm/javascript-obfuscator/dist/index.browser.js"></script>
<script>
var JavaScriptObfuscator = require('javascript-obfuscator');
var obfuscationResult = JavaScriptObfuscator.obfuscate(
    `
       function hi() {
           console.log("Hello World!");
       }
       hi();
    `,
    {
        compact: false,
        controlFlowFlattening: true,
        controlFlowFlatteningThreshold: 1,
        numbersToExpressions: true,
        simplify: true,
        stringArrayShuffle: true,
        splitStrings: true,
        stringArrayThreshold: 1
    }
);

console.log(obfuscationResult.getObfuscatedCode());
</script>

I also tried this but it also returns an error: obfuscate() is not defined

<script src="https://cdn.jsdelivr.net/npm/javascript-obfuscator/dist/index.browser.js"></script>
<script>
var obfuscationResult = obfuscate(
    `
        function hi() {
            console.log("Hello World!");
        }
        hi();
    `,
    {
        compact: false,
        controlFlowFlattening: true,
        controlFlowFlatteningThreshold: 1,
        numbersToExpressions: true,
        simplify: true,
        stringArrayShuffle: true,
        splitStrings: true,
        stringArrayThreshold: 1
    }
);
console.log(obfuscationResult.getObfuscatedCode());
</script>

Does anyone know how to fix this, please? Thanks

  • Simply, the `require()` function is not supported by client-side JavaScript. Perhaps [this](https://stackoverflow.com/questions/5168451/javascript-require-on-client-side) may help. – Jacob Lee Aug 06 '22 at 07:10
  • even if I remove require() function it still does not work – Genisys Mythic Aug 06 '22 at 07:11
  • Well yes, because then the tokens would not be defined. You cannot use the `require()` function in client-side JavaScript, so the above code does not work. Additionally, removing the `require()` call results in the functions defined in `javascript-obfuscator` not being defined. – Jacob Lee Aug 06 '22 at 07:13
  • Off-topic, but you probably want to obfuscate your JS code on the server side or as a build step. If you do it on the client side, users can still find the unobfuscated code too. – FZs Aug 06 '22 at 07:16

1 Answers1

0

<script src="https://cdn.jsdelivr.net/npm/javascript-obfuscator/dist/index.browser.js"></script>
<script>
  const obfuscationResult = JavaScriptObfuscator.obfuscate( // Change 'obfuscate' to 'JavaScriptObfuscator.obfuscate'
    `
        function hi() {
            console.log("Hello World!");
        }
        hi();
    `, {
      compact: false,
      controlFlowFlattening: true,
      controlFlowFlatteningThreshold: 1,
      numbersToExpressions: true,
      simplify: true,
      stringArrayShuffle: true,
      splitStrings: true,
      stringArrayThreshold: 1,
    }
  )

  console.log(obfuscationResult.getObfuscatedCode())
</script>
kennarddh
  • 2,186
  • 2
  • 6
  • 21