Full Code of aemkei/jsfuck for AI

main 6f8fdf31f18a cached
13 files
37.9 KB
12.5k tokens
7 symbols
1 requests
Download .txt
Repository: aemkei/jsfuck
Branch: main
Commit: 6f8fdf31f18a
Files: 13
Total size: 37.9 KB

Directory structure:
gitextract_chhm66xl/

├── .eslintrc.json
├── .gitignore
├── .npmignore
├── CNAME
├── Gruntfile.js
├── LICENSE.txt
├── README.md
├── fuck.js
├── index.html
├── jsfuck.js
├── output.txt
├── package.json
└── test/
    └── jsfuck_test.js

================================================
FILE CONTENTS
================================================

================================================
FILE: .eslintrc.json
================================================
{
	"rules": {
		"no-use-before-define": 2,
		"no-caller": 2,
		"no-eq-null": 2
	},
	"parserOptions": { "ecmaVersion": 6 },
	"env": {
		"browser": true,
		"node": true
	}
}


================================================
FILE: .gitignore
================================================
node_modules
.DS_Store

================================================
FILE: .npmignore
================================================
docs
test
.DS_Store
.editorconfig
.gitattributes
.jshintrc
.travis.yml
Gruntfile.js

================================================
FILE: CNAME
================================================
jsfuck.com

================================================
FILE: Gruntfile.js
================================================
/*global module:false*/
module.exports = function(grunt) {

  // Project configuration.
  grunt.initConfig({
    // Task configuration.
    eslint: {
      gruntfile: {
        src: 'Gruntfile.js'
      },
      lib_test: {
        src: ['jsfuck.js', 'lib/**/*.js', 'test/**/*.js']
      }
    },
    nodeunit: {
      files: ['test/**/*_test.js']
    },
    watch: {
      gruntfile: {
        files: '<%= eslint.gruntfile.src %>',
        tasks: ['eslint:gruntfile']
      },
      lib_test: {
        files: '<%= eslint.lib_test.src %>',
        tasks: ['eslint:lib_test', 'nodeunit']
      }
    }
  });

  // These plugins provide necessary tasks.
  grunt.loadNpmTasks('grunt-contrib-nodeunit');
	grunt.loadNpmTasks("gruntify-eslint");
  grunt.loadNpmTasks('grunt-contrib-watch');

  // Default task.
  grunt.registerTask('default', ['eslint', 'nodeunit']);

};


================================================
FILE: LICENSE.txt
================================================
           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
                   Version 2, December 2004

Copyright (C) 2012 Martin Kleppe <http://jsfuck.com>

Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.

           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

 0. You just DO WHAT THE FUCK YOU WANT TO.

================================================
FILE: README.md
================================================
# JSFuck `[]()!+`

JSFuck is an esoteric and educational programming style based on the atomic parts of JavaScript. It uses only six different characters to write and execute code.

It does not depend on a browser, so you can even run it on Node.js.

Demo: [jsfuck.com](http://www.jsfuck.com)

By [@aemkei](https://twitter.com/aemkei) and [friends](https://github.com/aemkei/jsfuck/graphs/contributors).

### Example

The following source will do an `alert(1)`:

```js
[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[
]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]][([][(![]+[])[+[]]+([![]]+[][[]
])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+
(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+
!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![
]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]
+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[
+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!!
[]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![
]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[
]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]((![]+[])[+!+[]]+(![
]+[])[!+[]+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]+(!![]+[])[+[]]+(!
[]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])
[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[!+[]+!+[]+[+[]]]+[+!+[]]+(
!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[
])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[!+[]+!+[]+[+[]]])()
```

### Basics

    false       =>  ![]
    true        =>  !![]
    undefined   =>  [][[]]
    NaN         =>  +[![]]
    0           =>  +[]
    1           =>  +!+[]
    2           =>  !+[]+!+[]
    10          =>  +[[+!+[]]+[+[]]]
    Array       =>  []
    Number      =>  +[]
    String      =>  []+[]
    Boolean     =>  ![]
    Function    =>  []["filter"]
    run         =>  []["filter"]["constructor"]( CODE )()
    eval        =>  []["filter"]["constructor"]("return eval")()( CODE )
    window      =>  []["filter"]["constructor"]("return this")()

See the full list [here](https://github.com/aemkei/jsfuck/blob/master/jsfuck.js).  

# How it Works

**Note:** Feel free to join the discussion here: https://gitter.im/aemkei/jsfuck

## `[]` – Brackets

Let's start with the opening and closing brackets and see what is possible here. They are super useful for this project and are considered as a core element because they provide a way to:

1. deal with arrays
2. access properties and methods.


### `[]` – Array Literals

Create new arrays:

```js
[]   // an empty array
[[]] // an array with one element (another array)
```

### `[X][i]` – Array / Object Access

```js
[][[]] // undefined, same as [][""]
```

Later we will be able to do this:

```js
"abc"[0]     // get single letter
[]["length"] // get property
[]["fill"]   // get methods
```

### `[X][0]` - Array wrapping trick

By wrapping an expression in an array and then getting the element at index zero, we can apply several operators on one expression. This means brackets `[]` can replace parenthesis `()` to isolate expressions:

```js
          [X][0]           // X
++[ ++[ ++[X][0] ][0] ][0] // X + 3
```

## `+` – Plus Sign

This symbol is useful, because it allows us to:

1. create numbers
2. add two values
3. concatenating strings
4. create strings

The current version of JSFuck uses it a lot but we not sure if they are fundamental.

### Cast to Number

```js
+[] // 0 - the number 0
```

### Increment Numbers

Using the array wrapping trick mentioned above:

```js
++[ 0  ][  0  ] // 1
++[ [] ][ +[] ] // 1
```

### Getting `undefined`

Getting an element by index in an empty array will return `undefined`:

```js
[][   0 ] // undefined
[][ +[] ] // get first element (undefined)
[][  [] ] // look for property ""
```

### Getting `NaN`

Casting `undefined` to Number will result in not-a-number:

```js
+[][[]]    // +undefined = NaN
```

### Add Numbers

```js
          1 +           1 // 2
++[[]][+[]] + ++[[]][+[]] // 2
```

A shorter way using ++:

```js
++[          1][  0] // 2
++[++[[]][  0]][  0] // 2
++[++[[]][+[]]][+[]] // 2
```

Using this technique, we are able to access all digits:

`0`, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`

### `+[]` – Casting to String

Combining the plus sign and brackets will turn other values into strings:

```js
  []        +[] // "" - empty string
 +[]        +[] // "0"
  [][[]]    +[] // "undefined"
++[][[]]    +[] // "NaN"
++[[]][+[]] +[] // "1"
```

### `"word"[i]` – Get Single Characters

As we have strings, we can also get single characters:

```js
  "undefined"          [  0] // "u"
[ "undefined"    ][  0][  0] // "u"
[  undefined +[] ][+[]][+[]] // "u"
[  [][[]]    +[] ][+[]][+[]] // "u"
```

```js
  "undefined"   [           1 ] // "n"
[[][[]]+[]][+[]][ ++[[]][+[]] ] // "n"
```

Since we have "NaN" and "undefined", we got the following characters:

`N`,`a`,`d`,`e`,`f`,`i`,`n`,`u`.

## `+` – Combine Characters

Now we can concat characters to new words.

```js
// can be written using []+ only:
"undefined"[4] // "f"
"undefined"[5] // "i"
"undefined"[6] // "n"
"undefined"[3] // "d"

// combine using +
"f"+"i"+"n"+"d" // "find"
```

## `"e"` – Numbers in exponential notation

As we have the character "e" from "undefined", we can use exponential notation to construct very big numbers and get a reference to `Infinity`:

```js
+("1e309")         //  Infinity
+("1e309")     +[] // "Infinity"
+("11e100")        //  1.1e+101
+("11e100")    +[] // "1.1e+101"   (gives us `.` and `+`)
+("0.0000001")     //  1e-7
+("0.0000001") +[] // "1e-7"       (gives us `-`)
```

Resulting chars:

`I`,`f`,`i`,`n`,`t`,`y`,`.`,`+`,`-`.


## `[]["method"]` – Access Methods

Newly combinded characters can form method names. These can be accessed using the square brackets notation:

```js
[]["f"+"i"+"n"+"d"] // where "f" is the first char of "false" and so on
[]["find"]          // same as the dot syntax:
[] .find
```

*Note*: With the characters from "undefined", "NaN" and "Infinity", the only method we are able to find in the objects we have is `Array.prototype.find`.

## `method+[]` – Get Method Definitions

We can cast a method to a String and get its definition as a String:

```js
[]["find"] +[]
```

This will return the following String:

```js
"function find() { [native code] }"
```

*Note*: String representations of native functions are not part of the ECMAScript standard and differ between browsers. For example, Firefox will output a slightly different string with additional line breaks using `\n`.

Resulting characters:

* `a`,`c`,`d`,`e`,`f`,`i`,`n`,`o`,`t`,`u`,`v`
* ` `, `{`, `}`, `(`, `)`, `[`,`]`

Resulting methods:

* `.concat`
* `.find`.

## `!` – Logical NOT operator

This is the fourth character in the original JSFuck set and used to create booleans.

Note: This symbol could also be replaced by others, like `<` or `=`. See the section "Alternatives" below.

### `!X` – Cast to Boolean

The logical "Not" operator can be used to create booleans `false` and `true`:

```js
 ![] // false
!![] // true
```

### `!X+[]` – Get "true" and "false"

Booleans can be casted to string:

```js
 ![] +[] // "false"
!![] +[] // "true"
```

This will give us access to more characters:

`a`, `e`, `f`, `l`, `r`, `s`, `t`, `u`.

Together with the set above, we will have `{}()[]+. INacdefilnorstuvy` with access to these methods:

* `call`
* `concat`
* `constructor`
* `entries`
* `every`
* `fill`
* `filter`
* `find`
* `fontcolor`
* `includes`
* `italics`
* `reduce`
* `reverse`
* `slice`
* `sort`

*Important:* We might use another symbols like `=` to create booleans, because they are more powerful (see section "Alternatives" below).

## `X["constructor"]` – Primitive wrappers names

With `.constructor` we have a reference to the function that created the instance. For primitives values, it returns the corresponding built-in wrappers:

```js
0       ["constructor"] // Number
""      ["constructor"] // String
[]      ["constructor"] // Array
false   ["constructor"] // Boolean
[].find ["constructor"] // Function
```

Use `+[]` to convert them to strings and retrieve their function name in order to get more chars:

```js
0["constructor"]+[] // "function Number() { ... }"
```

New chars available :
`m`, `b`, `S`, `g`, `B`, `A`, `F`.

… and more methods and properties:

* `arguments`
* `big`
* `bind`
* `bold`
* `name`
* `small`
* `some`
* `sub`
* `substr`
* `substring`
* `toString`
* `trim`


## `()` – Parenthesis

### Calling Methods

Since we have access to methods, we can call them to get more power. To do this we need to introduce two more symbols `(` and `)` here.

Example without arguments:

```js
""["fontcolor"]()   // "<font color="undefined"></font>"
[]["entries"]() +[] // "[object Array Iterator]"
```

New characters:

`j`, `<`, `>`, `=`, `"`, `/`

### Calling method with more than one argument

Calling a method with more than one argument is non trivial - to do it you can use the following [technique](https://stackoverflow.com/q/63601330/860099) (discovered by trincot) - for example: 

calling string method `"truefalse".replace("true","1")` can be written as `["true", "1"].reduce("".replace.bind("truefalse"))` and finally:

```js
["true"]["concat"]("1")["reduce"](""["replace"]["bind"]("truefalse"))
```

calling array method `[1,2,3].slice(1,2)` can be written as `[1,2].reduce([].slice.bind([1,2,3]))` and finally:

```js
[1]["concat"](2)["reduce"]([]["slice"]["bind"]([1,2,3]))
```


### Calling string method with more than one argument in "flow way"

To be able to call a method (with multiple arguments) in right side on results of previous method you can use this [technique](https://stackoverflow.com/q/63604058/860099) (discovered by trincot) - for example: `"truefalse".replace("true","1").replace("false","0")` can be written as 

```js
"truefalse"
    .split().concat([["true", "1"]]).reduce("".replace.apply.bind("".replace))
    .split().concat([["false", "0"]]).reduce("".replace.apply.bind("".replace))
``` 

and finally:

```js
"truefalse"
  ["split"]()["concat"]([["true"]["concat"]("1")])["reduce"](""["replace"]["apply"]["bind"](""["replace"]))
  ["split"]()["concat"]([["false"]["concat"]("0")])["reduce"](""["replace"]["apply"]["bind"](""["replace"]))

```

### Calling array method with more than one argument in "flow way"

To call array methods in righthand side (flow) way" we use similar technique like for strings but with additional tricks (details [here](https://stackoverflow.com/q/63631908/860099)) presented in following example: `[3,4,5].slice(1,2).concat(6)` can be written as `[[3,4,5]].concat([[1,2]]).reduce([].slice.apply.bind([].slice)).concat(6)` (similar like for strings) but now we need to find right-hand side way to wrap array `[3,4,5]` and get `[[3,4,5]]` which can be done as follows `[3,4,5].map([].constructor).concat([[[]]])[0].slice(-1)` so we get

```js
[3,4,5]
    // call: slice(1,2) 
    .map([].constructor).concat([[[]]])[0].slice(-1)
    .concat([[1,2]]).reduce([].slice.apply.bind([].slice))
    // call next method (in flow)
    .concat(6) 
```
and finally (after remove dots and commas)

```js
[3]["concat"](4)["concat"](5)
    ["map"]([]["constructor"])["concat"]([[[]]])[0]["slice"](-1)
    ["concat"]([[1]["concat"](2)])["reduce"]([]["slice"]["apply"]["bind"]([]["slice"]))
    ["concat"](6) 
```



### `number.toString(x)` – Getting any lowercase letter

Number's `toString` method has an optional argument specifying the base to use (between 2 and 36). With base 36 we can retrieve any *lowercase* letter:

```js
10["toString"](36) // "a"
11["toString"](36) // "b"
...
34["toString"](36) // "y"
35["toString"](36) // "z"
```
Exposed characters: `abcdefghijklmnopqrstuvwxyz`

### `Function("code")()` – Evaluate Code

The Function constructor is the master key in JSFuck: It takes a String as an argument and returns a new anonymous function with this string as the function body. So it basically lets you evaluate any code as a String. This is like `eval`, without the need for a reference to the global scope (a.k.a. `window`). We can get the Function constructor e.g. with `[]["find"]["constructor"]`.

This is the first major step and an essential part of a JS-to-JSFuck compiler.
...

### `Function("return this")()` – window

When evaluating `function anonymous() { return this }`, we get the invocation context which is a reference to the global scope here: `window`!

Getting a reference to `window` is another huge step forward for JSFuck. With the brackets characters, we could only dig in the available objects: numbers, arrays, some functions... With a reference to the global scope, we now have access to any global variable and the inner properties of these globals.

### Create regular expression object

You can create regular expression e.g. `/pattern/g` as follows

```js
[]["fill"]["constructor"]("return RegExp")()("pattern","g")
```

which after removing the comma (by using [multi-arguments technique](#calling-method-with-more-than-one-argument) without `bind`ing) looks as follows

```js
["pattern"]["concat"]("g")["reduce"]([]["fill"]["constructor"]("return RegExp")())
```

---

# Alternatives


## Combine Characters

Instead of `+` we could use `.concat` to combine strings:

```js
"f"["concat"]("i")["concat"]("l")["concat"]("l") // fill
```

Problem: We need to combine "c", "o", "n", "c", "a" and "t" to get "concat".

## Booleans

The `!` might be replaced with more "powerful" characters that have more than one use.

### `=` – Boolean + Assign Values

```js
X == X // true
X == Y // false
X = Y  // assign a new value
```

### `>` – Boolean + Create Numbers

```js
X > Y  // true
X > X  // false
X >> Y // number
```

A more complex example is to get character "f" with `[]>+` only:

```js
[[ []>[] ] + [] ] [[]>>[]] [[]>>[]]
[[ false ] + [] ] [     0] [     0]
[ "false"       ] [     0] [     0]
  "false"                  [     0]
```

## Numbers

Instead of `+` we could use booleans and bitshift operators to create numbers:

```js
true >> false         // 1
true << true          // 2
true << true << true  // 4
```

Problem: Some number (like `5`) are harder to get. But it is possible when using strings, eg `"11" >> true`.

## Execute Functions

Ways of executing functions other than using `()`:

1. using backticks: `` ` ``
2. handle events: `on...`
3. constructor: `new ...`
4. type conversion: `toString|valueOf`
5. symbol datatype: `[Symbol...]`

### Using Backticks

Instead of using opening and closing parentheses, we could use backticks ` to execute functions. In ES6 they can be used to interpolate strings and serve an expression for tagged template literals.

```js
([]["entries"]``).constructor // Object
```

This would give us characters from "Object" and access to its methods.

Unfortunately, we can only pass a single string (from our basic alphabet eg. `[]!+`) as the parameter. It is not possible to call methods with multiple arguments or a precompiled string. To do that, we have to use expression interpolation using `${}` which would introduce new characters.

The possibilities of backticks were discussed in detail [in the Gitter chat room](https://gitter.im/aemkei/jsfuck).

### Mapping Type Conversion

Another approach to execute functions without parentheses would be to map the `.toString` or `.valueOf` method and call them implicitly.

```js
A = []
A["toString"] = A["pop"]
A+"" // will execute A.pop
```

Note: There is no way to pass arguments and it requires to `=` be present in our basic alphabet. And it only works for methods that return basic types.

So far the only use-case is to wire `.toSource` in Firefox to get special characters like the backslash `\`.

### Trigger Event Handler

Function or methods could also be executed by assinging them to an event hander. There are several ways to do that, e.g:

```js
// override onload event on start
onload = f

// write image tags
document.body.innerHTML = '<img onerror=f src=X />'

// throw and handle error
onerror=f; throw 'x'

// trigger event
onhashchange = f; location.hash = 1;
```

Note: We need `=` to assign the handler.

Problem: We do not have access to `window` or DOM elements to attatch the event handlers.

### Constructor

We could also use the `new` operator to call the function as a pseudo object type:

```js
new f
```

Problem: The `new` operator is not (yet) available with our basic set of symbols.

### Symbol

A symbol is a unique and immutable data type and may be used as an identifier for object properties. This can be used to implicitly call a function.

```js
f[Symbol.toPrimitive] = f;  f++;
f[Symbol.iterator]    = f; [...f];
```

Note: We need `=` to assign the function.

Problem: We do not have access to `Symbol` using our reduced character set.

# Further Readings

JSFuck was not the first approach! Many people around the world are trying to break the so-called "Wall". Read more here:

* [Esolang Wiki: JSFuck](https://esolangs.org/wiki/JSFuck)
* [sla.ckers.org](http://sla.ckers.org/forum/read.php?24,32930) – Original Discussion
* [Xchars.js](http://slides.com/sylvainpv/xchars-js/) – Sylvain Pollet-Villard
* [Non Alphanumeric JavaScript](http://patriciopalladino.com/blog/2012/08/09/non-alphanumeric-javascript.html) – Patricio Palladino
* [Non-alphanumeric code](http://www.businessinfo.co.uk/labs/talk/Nonalpha.pdf) – Gareth Heyes
* [Executing non-alphanumeric JavaScript without parenthesis](http://blog.portswigger.net/2016/07/executing-non-alphanumeric-javascript.html) – Portswigger


================================================
FILE: fuck.js
================================================
#!/usr/bin/env node

var stream = require('stream');
var util = require('util');
var lib = require("./jsfuck.js");
var repl = require('repl');

if(process.argv.length !== 3) {

  function Stream() {
    stream.Transform.call(this);
  }
  util.inherits(Stream, stream.Transform);

  Stream.prototype._transform = function (chunk, encoding, callback) {
    var script = lib.JSFuck.encode(chunk.toString());
    var lines = script.split(/\n+/);
    for (var i = 0; i < lines.length; i++) {
      // ignore empty lines
      if (lines[i] !== '') this.push(lines[i] + '\n');
    }
    callback();
  };

  var fuckScript = new Stream();
  repl.start({
    prompt: "FUCK> ",
    input: fuckScript,
    useColors: true,
    output: process.stdout
  });

  process.stdin.pipe(fuckScript);
} else {
  var data = require("fs").readFileSync(process.argv[2], "utf8");
  var output = lib.JSFuck.encode(data, false);
  console.log(output);
}


================================================
FILE: index.html
================================================
<html>
<head>
  <title>JSFuck - Write any JavaScript with 6 Characters: []()!+</title>
  <meta name="description" content="JSFuck is an esoteric and educational programming style based on the atomic parts of JavaScript. It uses only six different characters to execute code.">
  <meta charset="utf-8" />
  <meta property="og:image" content="http://www.jsfuck.com/preview.png" />
  <meta name="viewport" content="width=device-width" />
  <style>
    
    body {
      padding: 20px;
    }
    
    body, * {
      font-family: monospace;
      font-size: 14px;
      line-height: 1.4em;
    }
    
    h1 {
      font-size: 2em;
      position: absolute;
      top: 70px;
      font-weight: normal;
      left: 140px;
    }
    
    h2 {
      width: 90px;
      text-align: right;
      padding: 50px 5px 5px;
      background: #F0DB4E;
      color: #323230;
      font-weight: bold;
      font-size: 20px;
      line-height: 1em;
    }
    
    h3 {
      font-weight: bold;
    }
        
    p, li, textarea, .actions {
      width: 100%;
      max-width: 600px;
    }
    
    textarea {
      display: block;
      height: 200px;
      margin: 1em 0;
    }
    
    ul.pre li{
      white-space: pre;
    }
    
    .checkbox {
      display: inline-block;
    }
    
    .actions a {
      float: right;
    }
    
    .actions {
      clear: both;
    }
    
  </style>
</head>
<body>
  <h1>JSFuck</h1>
  
  <h2>()+<br>[]!</h2>
  
  <p>JSFuck is an esoteric and educational programming style based on the
  atomic parts of JavaScript. It uses only six different characters to
  write and execute code.</p>
  
  <p>It does not depend on a browser, so you can even run it on Node.js.</p>
  
  <p>Use the form below to convert your own script. Uncheck "eval source" to
  get back a plain string.</p>

  <input id="input" type="text" value="alert(1)"/> 
  <button id="encode" type="text">Encode</button>
  
  <div class="checkbox">
    <input id="eval" type="checkbox" checked />
    <label for="eval">Eval Source</label>
  </div>
    <div class="checkbox">
    <input id="scope" type="checkbox" checked />
    <label for="scope">Run In Parent Scope</label>
  </div>
  
  <textarea id="output"></textarea>
  <div class="actions">
    <span id="stats">…</span>
    <a id="run" href="#">Run This</a>
  </div>
  
  <h3>Links</h3>
  
  <ul>
    <li>
      Share on
      <a href="https://twitter.com/intent/tweet?text=This%20project%20is%20crazy:%20http://jsfuck.com%20%E2%80%93%20Write%20any%20JavaScript%20with%206%20characters%20[]()!%2B%20//%20via%20@aemkei" target="_blank">Twitter</a>
    </li>
    <li>View source on <a href="http://github.com/aemkei/jsfuck">GitHub</a></li>
    <li>Follow <a href="http://twitter.com/aemkei">@aemkei</a> (Martin Kleppe)</li>
    <li>Original discussion at <a href="http://sla.ckers.org/forum/read.php?24,32930">Sla.ckers.org</a></li>
  </ul>
  
  <h3>Alternatives</h3>
  <ul>
    <li><a href="https://github.com/alcuadrado/hieroglyphy">Hieroglyphy</a> (8 chars, browser only)</li>
    <li><a href="http://utf-8.jp/public/jsfuck.html">utf-8.jp</a> (broken)</li>
    <li><a href="http://discogscounter.getfreehosting.co.uk/js-noalnum.php">JS-NoAlnum</a> (broken)</li>
  </ul>
  
  <h3>Basics</h3>
  
  <ul class="pre">
    <li>false       =>  ![]</li>
    <li>true        =>  !![]</li>
    <li>undefined   =>  [][[]]</li>
    <li>NaN         =>  +[![]]</li>
    <li>0           =>  +[]</li>
    <li>1           =>  +!+[]</li>
    <li>2           =>  !+[]+!+[]</li>
    <li>10          =>  [+!+[]]+[+[]]</li>
    <li>Array       =>  []</li>
    <li>Number      =>  +[]</li>
    <li>String      =>  []+[]</li>
    <li>Boolean     =>  ![]</li>
    <li>Function    =>  []["filter"]</li>

    <li>eval        =>  []["filter"]["constructor"]( CODE )()</li>
    <li>window      =>  []["filter"]["constructor"]("return this")()</li>
  </ul>

  <p>See the full list <a href="https://github.com/aemkei/jsfuck/blob/master/jsfuck.js">here</a>.</p>

  <script src="jsfuck.js"></script>
  <script>

    function $(id){
      return document.getElementById(id);
    }
    
    function encode(){
      var output = JSFuck.encode($("input").value, $("eval").checked, $("scope").checked);
      $("output").value = output;
      $("stats").innerHTML = output.length + " chars";
    }
  
    $("encode").onclick = encode;
    $("eval").onchange = encode;
    $("scope").onchange = encode;
    
    encode();
    
    $("run").onclick = function(){
      value = eval($("output").value);

      if (!$("eval").checked){
        alert('"' + value + '"');
      }
      return false;
    };
  </script>
  
  <script type="text/javascript">

    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-57649-11']);
    _gaq.push(['_trackPageview']);

    (function() {
      var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
      ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
      var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();

  </script>
</body>
</html>

================================================
FILE: jsfuck.js
================================================
/*! JSFuck 0.5.0 - http://jsfuck.com */

(function(self){
  const MIN = 32, MAX = 126;

  const SIMPLE = {
    'false':      '![]',
    'true':       '!![]',
    'undefined':  '[][[]]',
    'NaN':        '+[![]]',
    'Infinity':   '+(+!+[]+(!+[]+[])[!+[]+!+[]+!+[]]+[+!+[]]+[+[]]+[+[]]+[+[]])' // +"1e1000"
  };

  const CONSTRUCTORS = {
    'Array':    '[]',
    'Number':   '(+[])',
    'String':   '([]+[])',
    'Boolean':  '(![])',
    'Function': '[]["at"]',
    'RegExp':   'Function("return/"+false+"/")()'
  };

  const MAPPING = {
    'a':   '(false+"")[1]',
    'b':   '([]["entries"]()+"")[2]',
    'c':   '([]["at"]+"")[3]',
    'd':   '(undefined+"")[2]',
    'e':   '(true+"")[3]',
    'f':   '(false+"")[0]',
    'g':   '(false+[0]+String)[20]',
    'h':   '(+(101))["to"+String["name"]](21)[1]',
    'i':   '([false]+undefined)[10]',
    'j':   '([]["entries"]()+"")[3]',
    'k':   '(+(20))["to"+String["name"]](21)',
    'l':   '(false+"")[2]',
    'm':   '(Number+"")[11]',
    'n':   '(undefined+"")[1]',
    'o':   '(true+[]["at"])[10]',
    'p':   '(+(211))["to"+String["name"]](31)[1]',
    'q':   '("")["fontcolor"]([0]+false+")[20]',
    'r':   '(true+"")[1]',
    's':   '(false+"")[3]',
    't':   '(true+"")[0]',
    'u':   '(undefined+"")[0]',
    'v':   '(+(31))["to"+String["name"]](32)',
    'w':   '(+(32))["to"+String["name"]](33)',
    'x':   '(+(101))["to"+String["name"]](34)[1]',
    'y':   '(NaN+[Infinity])[10]',
    'z':   '(+(35))["to"+String["name"]](36)',

    'A':   '(NaN+[]["entries"]())[11]',
    'B':   '(+[]+Boolean)[10]',
    'C':   'Function("return escape")()(("")["italics"]())[2]',
    'D':   'Function("return escape")()([]["at"])["at"]("-1")',
    'E':   '(RegExp+"")[12]',
    'F':   '(+[]+Function)[10]',
    'G':   '(false+Function("return Date")()())[30]',
    'H':   null,
    'I':   '(Infinity+"")[0]',
    'J':   null,
    'K':   null,
    'L':   null,
    'M':   '(true+Function("return Date")()())[30]',
    'N':   '(NaN+"")[0]',
    'O':   null,
    'P':   null,
    'Q':   null,
    'R':   '(+[]+RegExp)[10]',
    'S':   '(+[]+String)[10]',
    'T':   '(NaN+Function("return Date")()())[30]',
    'U':   null,
    'V':   null,
    'W':   null,
    'X':   null,
    'Y':   null,
    'Z':   null,

    ' ':   '(NaN+[]["at"])[11]',
    '!':   null,
    '"':   '("")["fontcolor"]()[12]',
    '#':   null,
    '$':   null,
    '%':   'Function("return escape")()([]["at"])[22]',
    '&':   '("")["fontcolor"](")[13]',
    '\'':  null,
    '(':   '([]["at"]+"")[11]',
    ')':   '(""+[]["at"])[12]',
    '*':   null,
    '+':   '(+(+!+[]+(!+[]+[])[!+[]+!+[]+!+[]]+[+!+[]]+[+[]]+[+[]])+[])[2]',
    ',':   '[[]]["concat"]([[]])+""',
    '-':   '(+(.+[0000001])+"")[2]',
    '.':   '(+(+!+[]+[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+[!+[]+!+[]]+[+[]])+[])[+!+[]]',
    '/':   '(false+[0])["italics"]()[10]',
    ':':   '(RegExp()+"")[3]',
    ';':   '("")["fontcolor"](NaN+")[21]',
    '<':   '("")["italics"]()[0]',
    '=':   '("")["fontcolor"]()[11]',
    '>':   '("")["italics"]()[2]',
    '?':   '(RegExp()+"")[2]',
    '@':   null,
    '[':   '([]["entries"]()+"")[0]',
    '\\':  '(RegExp("/")+"")[1]',
    ']':   '([]["entries"]()+"")[22]',
    '^':   null,
    '_':   null,
    '`':   null,
    '{':   '([0]+false+[]["at"])[20]',
    '|':   null,
    '}':   '([]["at"]+"")["at"]("-1")',
    '~':   null
  };

  const GLOBAL = 'Function("return this")()';

  function fillMissingDigits(){
    var output, number, i;

    for (number = 0; number < 10; number++){

      output = "+[]";

      if (number > 0){ output = "+!" + output; }
      for (i = 1; i < number; i++){ output = "+!+[]" + output; }
      if (number > 1){ output = output.substr(1); }

      MAPPING[number] = "[" + output + "]";
    }
  }

  function replaceMap(){
    var character = "", value, i, key;

    function replace(pattern, replacement){
      value = value.replace(
        new RegExp(pattern, "gi"),
        replacement
      );
    }

    function digitReplacer(_,x) { return MAPPING[x]; }

    function numberReplacer(_,y) {
      var values = y.split("");
      var head = +(values.shift());
      var output = "+[]";

      if (head > 0){ output = "+!" + output; }
      for (i = 1; i < head; i++){ output = "+!+[]" + output; }
      if (head > 1){ output = output.substr(1); }

      return [output].concat(values).join("+").replace(/(\d)/g, digitReplacer);
    }

    for (i = MIN; i <= MAX; i++){
      character = String.fromCharCode(i);
      value = MAPPING[character];
      if(!value) {continue;}

      for (key in CONSTRUCTORS){
        replace("\\b" + key, CONSTRUCTORS[key] + '["constructor"]');
      }

      for (key in SIMPLE){
        replace(key, SIMPLE[key]);
      }

      replace('(\\d\\d+)', numberReplacer);
      replace('\\((\\d)\\)', digitReplacer);
      replace('\\[(\\d)\\]', digitReplacer);

      replace("GLOBAL", GLOBAL);
      replace('\\+""', "+[]");
      replace('""', "[]+[]");

      MAPPING[character] = value;
    }
  }

  function replaceStrings(){
    var regEx = /[^\[\]\(\)\!\+]{1}/g,
      all, value, missing,
      count = MAX - MIN;

    function findMissing(){
      var all, value, done = false;

      missing = {};

      for (all in MAPPING){

        value = MAPPING[all];

        if (value && value.match(regEx)){
          missing[all] = value;
          done = true;
        }
      }

      return done;
    }

    function mappingReplacer(a, b) {
      return b.split("").join("+");
    }

    function valueReplacer(c) {
      return missing[c] ? c : MAPPING[c];
    }

    for (all in MAPPING){
      if (MAPPING[all]){
        MAPPING[all] = MAPPING[all].replace(/\"([^\"]+)\"/gi, mappingReplacer);
      }
    }

    while (findMissing()){
      for (all in missing){
        value = MAPPING[all];
        value = value.replace(regEx, valueReplacer);

        MAPPING[all] = value;
        missing[all] = value;
      }

      if (count-- === 0){
        console.error("Could not compile the following chars:", missing);
      }
    }
  }

  function escapeSequence(c) {
    var cc = c.charCodeAt(0);
    if (cc < 256) {
      return '\\' + cc.toString(8);
    } else {
      var cc16 = cc.toString(16);
      return '\\u' + ('0000' + cc16).substring(cc16.length);  
    }
  }

  function escapeSequenceForReplace(c) {
    return escapeSequence(c).replace('\\', 't');
  }

  function encode(input, wrapWithEval, runInParentScope){
    var output = [];

    if (!input){
      return "";
    }

    var unmappped = ''
    for(var k in MAPPING) {
      if (MAPPING[k]){
        unmappped += k;
      }
    }
    unmappped = unmappped.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
    unmappped = new RegExp('[^' + unmappped + ']','g');
    var unmappedCharactersCount = (input.match(unmappped) || []).length;
    if (unmappedCharactersCount > 1) {
      // Without this optimization one unmapped character has encoded length
      // of about 3600 characters. Every additional unmapped character adds 
      // 2000 to the total length. For example, the length of `~` is 3605,
      // `~~` is 5600, and `~~~` is 7595.
      // 
      // The loader with replace has encoded length of about 5300 characters
      // and every additional character adds 100 to the total length. 
      // In the same example the length of `~~` becomes 5371 and `~~~` -- 5463.
      // 
      // So, when we have more than one unmapped character we want to encode whole input
      // except select characters (that have encoded length less than about 70)
      // into an escape sequence.
      //
      // NOTE: `t` should be escaped!
      input = input.replace(/[^0123456789.adefilnrsuN]/g, escapeSequenceForReplace);
    } else if (unmappedCharactersCount > 0) {
      //Because we will wrap the input into a string we need to escape Backslash 
      // and Double quote characters (we do not need to worry about other characters 
      // because they are not mapped explicitly).
      // The JSFuck-encoded representation of `\` is 2121 symbols,
      // so escaped `\` is 4243 symbols and escaped `"` is 2261 symbols
      // however the escape sequence of that characters are 
      // 2168 and 2155 symbols respectively, so it's more practical to 
      // rewrite them as escape sequences.
      input = input.replace(/["\\]/g, escapeSequence);
      //Convert all unmapped characters to escape sequence
      input = input.replace(unmappped, escapeSequence);
    }

    var r = "";
    for (var i in SIMPLE) {
      r += i + "|";
    }
    r+= ".";

    input.replace(new RegExp(r, 'g'), function(c) {
      var replacement = SIMPLE[c];
      if (replacement) {
        output.push("(" + replacement + "+[])");
      } else {
        replacement = MAPPING[c];
        if (replacement){
          output.push(replacement);
        } else {
          throw new Error('Found unmapped character: ' + c);
        }
      }
    });

    output = output.join("+");

    if (/^\d$/.test(input)){
      output += "+[]";
    }

    if (unmappedCharactersCount > 1) {
      // replace `t` with `\\`
      output = "(" + output + ")[" + encode("split") + "](" + encode ("t") + ")[" + encode("join") +"](" + encode("\\") + ")";
    }

    if (unmappedCharactersCount > 0) {
      output = "[][" + encode("at") + "]"+
      "[" + encode("constructor") + "]" +
      "(" + encode("return\"") + "+" + output + "+" + encode("\"") + ")()";
    }

    if (wrapWithEval){
      if (runInParentScope){
        output = "[][" + encode("at") + "]" +
          "[" + encode("constructor") + "]" +
          "(" + encode("return eval") + ")()" +
          "(" + output + ")";
      } else {
        output = "[][" + encode("at") + "]" +
          "[" + encode("constructor") + "]" +
          "(" + output + ")()";
      }
    }

    return output;
  }

  fillMissingDigits();
  replaceMap();
  replaceStrings();

  self.JSFuck = {
    encode: encode
  };
})(typeof(exports) === "undefined" ? window : exports);


================================================
FILE: output.txt
================================================
` ` 58
`!` 2708
`"` 335
`#` 2717
`$` 2722
`%` 1688
`&` 675
`'` 2737
`(` 54
`)` 61
`*` 2717
`+` 70
`,` 231
`-` 135
`.` 72
`/` 215
`0` 8
`1` 10
`2` 14
`3` 19
`4` 24
`5` 29
`6` 34
`7` 39
`8` 44
`9` 49
`:` 1303
`;` 677
`<` 203
`=` 331
`>` 209
`?` 1298
`@` 2698
`A` 185
`B` 372
`C` 1840
`D` 1842
`E` 1304
`F` 401
`G` 2485
`H` 2700
`I` 70
`J` 2706
`K` 2711
`L` 2716
`M` 2486
`N` 16
`O` 528
`P` 2704
`Q` 2706
`R` 1299
`S` 374
`T` 2488
`U` 1513
`V` 2730
`W` 2735
`X` 2709
`Y` 2711
`Z` 2715
`[` 171
`\` 1509
`]` 189
`^` 2735
`_` 2740
``` 2714
`a` 15
`b` 177
`c` 55
`d` 22
`e` 25
`f` 13
`g` 384
`h` 913
`i` 27
`j` 182
`k` 902
`l` 19
`m` 373
`n` 18
`o` 54
`p` 924
`q` 678
`r` 16
`s` 24
`t` 14
`u` 16
`v` 918
`w` 927
`x` 932
`y` 84
`z` 957
`{` 63
`|` 2745
`}` 216
`~` 2755


================================================
FILE: package.json
================================================
{
  "name": "jsfuck",
  "version": "0.5.0",
  "description": "Write any JavaScript with just 6 characters: []()!+",
  "main": "jsfuck.js",
  "bin": "fuck.js",
  "repository": {
    "type": "git",
    "url": "git://github.com/aemkei/jsfuck.git"
  },
  "keywords": [
    "esoteric",
    "obfuscation"
  ],
  "author": {
    "name": "Martin Kleppe",
    "url": "https://github.com/aemkei"
  },
  "licenses": [
    {
      "type": "WTFPL",
      "url": "https://raw.github.com/aemkei/jsfuck/master/LICENSE.txt"
    }
  ],
  "bugs": {
    "url": "https://github.com/aemkei/jsfuck/issues"
  },
  "scripts": {
    "start": "grunt default watch"
  },
  "devDependencies": {
    "grunt": "^0.4.5",
    "grunt-cli": "~1.2.0",
    "grunt-contrib-nodeunit": "~0.2.0",
    "grunt-contrib-watch": "~0.5.3",
    "gruntify-eslint": "^5.0.0"
  }
}


================================================
FILE: test/jsfuck_test.js
================================================
/*jshint -W061 */
'use strict';

var JSFuck = require('../jsfuck.js').JSFuck,
	test_encode = function (test, value) {
		var encoded = JSFuck.encode(value),
			unencoded = eval(encoded);

		test.strictEqual(value, unencoded, 'encoding "' + value + '" failed');
	};
var MIN = 32, MAX = 127;
var fs = require('fs');

exports['integration'] = {
	'test': function(test) {
		var file = fs.openSync('output.txt', 'w+');

		for (var i = MIN; i < MAX; i++) {
			var c = String.fromCharCode(i),
				encoded = JSFuck.encode(c);
			fs.writeSync(file, '`' + c + '` ' + encoded.length + '\n');
		}

		fs.closeSync(file);
		test.done();
	}
};

var test = function (c, test) {
	test_encode(test, c);
	test.done();
};

var createTest = function (input) {
	exports['encode_tests']['encode "'+input+'"'] = test.bind(undefined, input);
};

exports['encode_tests'] = {};

createTest('false');
createTest('falsefalsetrue');
createTest('ABCDEFGHIJKLMNOPQRSTUVWXYZ');
createTest('abcdefghijklmnopqrstuvwxyz');
createTest(';&');
createTest('\n');
createTest('\r');
createTest('\r\n');
createTest('\u2028\u2029');
createTest('false');
createTest('true');
createTest('undefined');
createTest('NaN');
createTest('Infinity');
createTest('undefinedundefined');
createTest('0undefined0');
createTest('undefinedArray');
createTest('ArrayundefinedBoolean');
createTest('undefinedBooleanArray');
createTest('NaNNaN');
createTest('InfinityInfinity');
createTest('InfinityInfinity');
createTest('NaNtrue');
createTest('trueNaN');
createTest('undefinedNaN');
createTest('~\\"');
createTest('t~');
createTest('~t');
createTest('[(~t~)]');
createTest('~0123456789 abcdefghijklmnopqrstuvwxyz()+.~');
createTest('~0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ()+.~');

for(var i=MIN; i<MAX ;i++) {
	createTest(String.fromCharCode(i));
}
Download .txt
gitextract_chhm66xl/

├── .eslintrc.json
├── .gitignore
├── .npmignore
├── CNAME
├── Gruntfile.js
├── LICENSE.txt
├── README.md
├── fuck.js
├── index.html
├── jsfuck.js
├── output.txt
├── package.json
└── test/
    └── jsfuck_test.js
Download .txt
SYMBOL INDEX (7 symbols across 2 files)

FILE: fuck.js
  function Stream (line 10) | function Stream() {

FILE: jsfuck.js
  function fillMissingDigits (line 115) | function fillMissingDigits(){
  function replaceMap (line 130) | function replaceMap(){
  function replaceStrings (line 179) | function replaceStrings(){
  function escapeSequence (line 231) | function escapeSequence(c) {
  function escapeSequenceForReplace (line 241) | function escapeSequenceForReplace(c) {
  function encode (line 245) | function encode(input, wrapWithEval, runInParentScope){
Condensed preview — 13 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (42K chars).
[
  {
    "path": ".eslintrc.json",
    "chars": 172,
    "preview": "{\n\t\"rules\": {\n\t\t\"no-use-before-define\": 2,\n\t\t\"no-caller\": 2,\n\t\t\"no-eq-null\": 2\n\t},\n\t\"parserOptions\": { \"ecmaVersion\": 6 "
  },
  {
    "path": ".gitignore",
    "chars": 22,
    "preview": "node_modules\n.DS_Store"
  },
  {
    "path": ".npmignore",
    "chars": 90,
    "preview": "docs\r\ntest\r\n.DS_Store\r\n.editorconfig\r\n.gitattributes\r\n.jshintrc\r\n.travis.yml\r\nGruntfile.js"
  },
  {
    "path": "CNAME",
    "chars": 10,
    "preview": "jsfuck.com"
  },
  {
    "path": "Gruntfile.js",
    "chars": 867,
    "preview": "/*global module:false*/\nmodule.exports = function(grunt) {\n\n  // Project configuration.\n  grunt.initConfig({\n    // Task"
  },
  {
    "path": "LICENSE.txt",
    "chars": 477,
    "preview": "           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE\n                   Version 2, December 2004\n\nCopyright (C) 2012 M"
  },
  {
    "path": "README.md",
    "chars": 17766,
    "preview": "# JSFuck `[]()!+`\n\nJSFuck is an esoteric and educational programming style based on the atomic parts of JavaScript. It u"
  },
  {
    "path": "fuck.js",
    "chars": 927,
    "preview": "#!/usr/bin/env node\n\nvar stream = require('stream');\nvar util = require('util');\nvar lib = require(\"./jsfuck.js\");\nvar r"
  },
  {
    "path": "index.html",
    "chars": 5131,
    "preview": "<html>\n<head>\n  <title>JSFuck - Write any JavaScript with 6 Characters: []()!+</title>\n  <meta name=\"description\" conten"
  },
  {
    "path": "jsfuck.js",
    "chars": 9985,
    "preview": "/*! JSFuck 0.5.0 - http://jsfuck.com */\n\n(function(self){\n  const MIN = 32, MAX = 126;\n\n  const SIMPLE = {\n    'false': "
  },
  {
    "path": "output.txt",
    "chars": 761,
    "preview": "` ` 58\n`!` 2708\n`\"` 335\n`#` 2717\n`$` 2722\n`%` 1688\n`&` 675\n`'` 2737\n`(` 54\n`)` 61\n`*` 2717\n`+` 70\n`,` 231\n`-` 135\n`.` 72"
  },
  {
    "path": "package.json",
    "chars": 831,
    "preview": "{\n  \"name\": \"jsfuck\",\n  \"version\": \"0.5.0\",\n  \"description\": \"Write any JavaScript with just 6 characters: []()!+\",\n  \"m"
  },
  {
    "path": "test/jsfuck_test.js",
    "chars": 1788,
    "preview": "/*jshint -W061 */\n'use strict';\n\nvar JSFuck = require('../jsfuck.js').JSFuck,\n\ttest_encode = function (test, value) {\n\t\t"
  }
]

About this extraction

This page contains the full source code of the aemkei/jsfuck GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 13 files (37.9 KB), approximately 12.5k tokens, and a symbol index with 7 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!