Class: JSObfu
Overview
The primary class, used to parse and obfuscate Javascript code.
Defined Under Namespace
Modules: Disable, Utils Classes: ECMANoWhitespaceVisitor, Hoister, Obfuscator, Scope
Instance Attribute Summary collapse
-
#scope ⇒ JSObfu::Scope
readonly
The global scope.
Instance Method Summary collapse
-
#<<(str) ⇒ Object
Add
strto the un-obfuscated code. -
#ast ⇒ RKelly::Nodes::SourceElementsNode
The abstract syntax tree.
-
#initialize(code) ⇒ JSObfu
constructor
Saves
codefor later obfuscation with #obfuscate. -
#obfuscate(opts = {}) ⇒ self
Parse and obfuscate.
-
#sym(sym) ⇒ String
Returns the obfuscated name for the variable or function
sym. -
#to_s ⇒ String
The (possibly obfuscated) code.
Methods included from Disable
Constructor Details
#initialize(code) ⇒ JSObfu
Saves code for later obfuscation with #obfuscate
22 23 24 25 |
# File 'lib/jsobfu.rb', line 22 def initialize(code) @code = code.to_s @scope = Scope.new end |
Instance Attribute Details
#scope ⇒ JSObfu::Scope (readonly)
Returns the global scope.
18 19 20 |
# File 'lib/jsobfu.rb', line 18 def scope @scope end |
Instance Method Details
#<<(str) ⇒ Object
Add str to the un-obfuscated code. Calling this method after #obfuscate is undefined
29 30 31 |
# File 'lib/jsobfu.rb', line 29 def <<(str) @code << str end |
#ast ⇒ RKelly::Nodes::SourceElementsNode
Returns the abstract syntax tree.
39 40 41 |
# File 'lib/jsobfu.rb', line 39 def ast @ast || parse end |
#obfuscate(opts = {}) ⇒ self
Parse and obfuscate
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 78 79 80 81 82 83 84 85 86 |
# File 'lib/jsobfu.rb', line 53 def obfuscate(opts={}) return self if JSObfu.disabled? iterations = opts.fetch(:iterations, 1).to_i strip_whitespace = opts.fetch(:strip_whitespace, true) iterations.times do |i| obfuscator = JSObfu::Obfuscator.new(opts.merge(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
92 93 94 95 |
# File 'lib/jsobfu.rb', line 92 def sym(sym) return sym.to_s if @renames.nil? @renames[sym.to_s] end |
#to_s ⇒ String
Returns the (possibly obfuscated) code.
34 35 36 |
# File 'lib/jsobfu.rb', line 34 def to_s @code end |