JScrambler is an online JavaScript obfuscator and code optimization tool available as a Web application and Web API.
For more information vist https://jscrambler.com/en
-
Install JScrambler at the command prompt if you haven't yet:
gem install jscrambler
Or by simply adding it to your Gemfile:
gem 'jscrambler'
-
Create (if not there already) a
config
folder in your project root directory -
In this folder add a
jscrambler_config.json
with the following content:
{
"filesSrc": ["REPLACE_WITH_SOURCE_FILES_PATH"],
"filesDest": "REPLACE_WITH_DESTINATION_PATH",
"host": "api.jscrambler.com",
"port": 443,
"apiVersion": 3,
"keys": {
"accessKey": "REPLACE_WITH_ACCESS_KEY",
"secretKey": "REPLACE_WITH_SECRET_KEY"
},
"params": {
"string_splitting": "%DEFAULT%",
"function_reorder": "%DEFAULT%",
"function_outlining": "%DEFAULT%",
"dot_notation_elimination": "%DEFAULT%",
"expiration_date": "2199-01-01",
"rename_local": "%DEFAULT%",
"whitespace": "%DEFAULT%",
"literal_duplicates": "%DEFAULT%"
},
"deleteProject": true
}
This is the bare minimum to setup this gem. Now we need to change the config file to adapt to our needs.
Most of the parameters in the config file won't need to be altered, but some of them require mandatory changes.
-
Create your account at the JScrambler website. This will give you both the
accessKey
andsecretKey
. Once you have them replaceREPLACE_WITH_ACCESS_KEY
with your access key andREPLACE_WITH_SECRET_KEY
with your secret key. -
Change where to pick up your html and js file from by replacing the
["REPLACE_WITH_SOURCE_FILES_PATH"]
array. Some examples:
"filesSrc": ["/some/path/script.js"],
"filesSrc": ["/some/path/script.js", "/some/path/jquery.js", "/some/path/index.html"],
"filesSrc": ["/some/path/scripts/*"],
"filesSrc": ["/some/path/scripts/**"],
"filesSrc": ["/some/path/scripts/**", "/some/path/html_files/*", "/some/path/vendor/jquery.js"],
-
Change where to store your obfuscated files after being processed by JScrambler by replacing
REPLACE_WITH_DESTINATION_PATH
. Example:"filesDest": ["/some/path/processed_files"],
There are 2 ways to process your files using this gem:
- This gem comes packed with a rake task that you can run to process your files.
- Using gem's internal API.
In order to process files using this method, first of all open your Rakefile
and add the following:
spec = Gem::Specification.find_by_name 'jscrambler'
load "#{spec.gem_dir}/lib/tasks/jscrambler.rake"
This will make sure that when you run bundle exec rake
it know where to search for JScrambler tasks.
Once this is done simply run:
bundle exec rake jscrambler:process
It will read your config file, take all the files specified in your filesSrc
parameter, archive them, send them to JScrambler, download them and store them in your filesDest
path.
Alternatively you can provide a custom config file:
bundle exec rake jscrambler:process['/some/path/to/config.json']
If you're a developer and want to integrate JScrambler functionalities directly into your ruby code, this will be your method of choice.
require 'jscrambler'
# Will simply update your source code in filesSrc to JScrambler and create a new JScrambler::Project
JScrambler.upload_code
# Will wait until a given project has been processed.
# `requested_project` - can either be a project ID hash or a JScrambler::Project
JScrambler.poll_project(requested_project)
# Will download processed source code for a given project.
# `requested_project` - can either be a project ID hash or a JScrambler::Project
JScrambler.download_code(requested_project)
# Will retrieve a JScrambler::Project object for a given project
# `requested_project` - can either be a project ID hash or a JScrambler::Project
JScrambler.get_info(requested_project)
# Takes `upload_code`, `poll_project` and `download_code` and runs them one after the other.
JScrambler.process
# Retrieves all projects
JScrambler.projects
This gem integrates directly with Rails. Here's how your config file should look like:
{
"filesSrc": ["public/assets/*.js"],
"filesDest": "public/assets/",
"host": "api.jscrambler.com",
"port": 443,
"apiVersion": 3,
"keys": {
"accessKey": "REPLACE_WITH_ACCESS_KEY",
"secretKey": "REPLACE_WITH_SECRET_KEY"
},
"params": {
"string_splitting": "%DEFAULT%",
"function_reorder": "%DEFAULT%",
"function_outlining": "%DEFAULT%",
"dot_notation_elimination": "%DEFAULT%",
"expiration_date": "2199-01-01",
"rename_local": "%DEFAULT%",
"whitespace": "%DEFAULT%",
"literal_duplicates": "%DEFAULT%"
},
"deleteProject": true
}
Make sure that you first run rake assets:precompile
task. This will run the obfuscation on top of the already generated and unified javascript files in your public/assets
folder. For more information on Rails assets pipeline read here.
JScrambler is released under the MIT License.