Skip to content

Instantly share code, notes, and snippets.

@wsams
Last active September 20, 2018 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wsams/9e65dfebe30b8fa33c55b304612e2d52 to your computer and use it in GitHub Desktop.
Save wsams/9e65dfebe30b8fa33c55b304612e2d52 to your computer and use it in GitHub Desktop.
A script that obfuscates and minimizes an opinionated JavaScript file. See script comments for instructions.
<?php
/**
* INSTALL & RUN
*
* 1. Install uglifyjs: `npm install -g uglifyjs-es`
* 2. Write your JavaScript with every variable prefixed with two underscores. e.g. `__varName`
* The script also supports functions prefixed with two dashes but it doesn't appear to work anymore.
* 3. Run: `php compile-min-js.php my.js my.min.js` (This script obfuscates and then runs uglifyjs)
*/
/**
* Config - No need to update unless you want a different temporary file location.
* If so, edit `$tmp`.
*/
$raw = $_SERVER['argv'][1];
$tmp = "/tmp/mult.js.tmp";
$min = $_SERVER['argv'][2];
function buildList($type) {
$lower = array(97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122);
$upper = array(65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90);
$numbers = array(48, 49, 50, 51, 52, 53, 54, 55, 56, 57);
$symbols = array(32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
47, 58, 59, 60, 61, 62, 63, 64, 91, 92, 93, 94, 95, 96, 123,
124, 125, 126);
$list = array();
if (in_array("lower", $type)) {
$chr_lower = array();
foreach($lower as $v) {
$chr_lower[] = chr($v);
}
$list = array_merge($list, $chr_lower);
}
if (in_array("upper", $type)) {
$chr_upper = array();
foreach($upper as $v) {
$chr_upper[] = chr($v);
}
$list = array_merge($list, $chr_upper);
}
if (in_array("numbers", $type)) {
$chr_numbers = array();
foreach($numbers as $v) {
$chr_numbers[] = chr($v);
}
$list = array_merge($list, $chr_numbers);
}
if (in_array("symbols", $type)) {
$chr_symbols = array();
foreach ($symbols as $v) {
$chr_symbols[] = chr($v);
}
$list = array_merge($list, $chr_symbols);
}
return $list;
}
function charAt($str, $pos) {
return(substr($str, $pos, 1) !== false) ? substr($str, $pos, 1) : -1;
}
function getLast($list) {
return end($list);
}
function getNext($start, $list) {
$length = strlen($start);
$character = charAt($start, $length - 1);
if ($character === getLast($list) && $length === 1) {
return $list[0] . $list[0];
}
$root = substr($start, 0, $length - 1);
if ($character === getLast($list)) {
return getNext($root, $list) . $list[0];
}
$character = $list[intval(array_search($character, $list)) + 1];
return $root . $character;
}
$f = file_get_contents($raw);
// start: replace __
preg_match_all("/__[a-zA-Z0-9]+/", $f, $m);
usort($m[0], function ($a, $b) {
return strlen($b) - strlen($a);
});
$type = array("lower");
$list = buildList($type);
$start = "a";
foreach ($m[0] as $slvar) {
$f = preg_replace("/{$slvar}/", $start, $f);
$start = getNext($start, $list);
}
unset($m);
// end: replace __
// start: replace --
preg_match_all("/--[a-zA-Z0-9]+/", $f, $m);
usort($m[0], function ($a, $b) {
return strlen($b) - strlen($a);
});
$type = array("lower");
$list = buildList($type);
foreach ($m[0] as $slfunc) {
$f = preg_replace("/{$slfunc}/", $start, $f);
$start = getNext($start, $list);
}
// end: replace --
file_put_contents($tmp, $f);
exec("uglifyjs -o {$min} {$tmp}");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment