Class: JSObfu
- Inherits:
-
Object
- Object
- JSObfu
- Defined in:
- lib/jsobfu.rb
Overview
The primary class, used to parse and obfuscate Javascript code.
Defined Under Namespace
Modules: Utils Classes: ECMANoWhitespaceVisitor, Hoister, Obfuscator, Scope
Instance Attribute Summary collapse
-
#scope ⇒ JSObfu::Scope
readonly
The global scope.
Instance Method Summary collapse
-
#<<(str) ⇒ Object
Add
str
to the un-obfuscated code. -
#ast ⇒ RKelly::Nodes::SourceElementsNode
The abstract syntax tree.
-
#initialize(code) ⇒ JSObfu
constructor
Saves
code
for later obfuscation with #obfuscate. -
#obfuscate(opts = {}) ⇒ String
Parse and obfuscate.
-
#sym(sym) ⇒ String
Returns the obfuscated name for the variable or function
sym
. -
#to_s ⇒ String
The (possibly obfuscated) code.
Constructor Details
Instance Attribute Details
#scope ⇒ JSObfu::Scope (readonly)
Returns the global scope.
13 14 15 |
# File 'lib/jsobfu.rb', line 13 def scope @scope end |
Instance Method Details
#<<(str) ⇒ Object
Add str
to the un-obfuscated code. Calling this method after #obfuscate is undefined
24 25 26 |
# File 'lib/jsobfu.rb', line 24 def <<(str) @code << str end |
#ast ⇒ RKelly::Nodes::SourceElementsNode
Returns the abstract syntax tree.
34 35 36 |
# File 'lib/jsobfu.rb', line 34 def ast @ast || parse end |
#obfuscate(opts = {}) ⇒ String
Parse and obfuscate
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/jsobfu.rb', line 46 def obfuscate(opts={}) iterations = opts.fetch(:iterations, 1).to_i strip_whitespace = opts.fetch(:strip_whitespace, true) iterations.times do |i| obfuscator = JSObfu::Obfuscator.new(scope: @scope) @code = obfuscator.accept(ast).to_s if strip_whitespace @code.gsub!(/(^\s+|\s+$)/, '') @code.delete!("\n") @code.delete!("\r") end new_renames = obfuscator.renames.dup if @renames # "patch up" the renames after each iteration @renames.each do |key, prev_rename| @renames[key] = new_renames[prev_rename] end else # on first iteration, take the renames as-is @renames = new_renames end unless i == iterations-1 @scope = Scope.new @ast = nil # force a re-parse end end self end |
#sym(sym) ⇒ String
Returns the obfuscated name for the variable or function sym
83 84 85 86 |
# File 'lib/jsobfu.rb', line 83 def sym(sym) raise RuntimeError, "Must obfuscate before calling #sym" if @renames.nil? @renames[sym.to_s] end |
#to_s ⇒ String
Returns the (possibly obfuscated) code.
29 30 31 |
# File 'lib/jsobfu.rb', line 29 def to_s @code end |