Class: JSObfu
- Inherits:
-
Object
- Object
- JSObfu
- Includes:
- Disable
- Defined in:
- lib/jsobfu.rb,
lib/jsobfu/version.rb
Overview
The primary class, used to parse and obfuscate Javascript code.
Defined Under Namespace
Modules: Disable, Utils Classes: ECMANoWhitespaceVisitor, Hoister, Obfuscator, Scope
Constant Summary collapse
- VERSION =
"0.4.2"
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.
-
#code=(code) ⇒ Object
Sets the code that this obfuscator will transform.
-
#initialize(code = nil, opts = {}) ⇒ JSObfu
constructor
Saves
code
for 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
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
32 33 34 |
# File 'lib/jsobfu.rb', line 32 def <<(str) @code << str end |
#ast ⇒ RKelly::Nodes::SourceElementsNode
Returns the abstract syntax tree.
42 43 44 |
# File 'lib/jsobfu.rb', line 42 def ast @ast ||= parse end |
#code=(code) ⇒ Object
Sets the code that this obfuscator will transform
48 49 50 51 |
# File 'lib/jsobfu.rb', line 48 def code=(code) @ast = nil # invalidate any previous parses @code = code end |
#obfuscate(opts = {}) ⇒ self
Parse and obfuscate
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/jsobfu.rb', line 67 def obfuscate(opts={}) return self if JSObfu.disabled? raise ArgumentError.new("code must be present") if @code.nil? 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 if @renames # "patch up" the renames after each iteration @renames.merge! (obfuscator.renames) else # on first iteration, take the renames as-is @renames = obfuscator.renames.dup end unless i == iterations-1 @scope = Scope.new @ast = nil # force a re-parse end end # Enter all of the renames into current scope @scope.renames.merge!(@renames || {}) self end |
#sym(sym) ⇒ String
Returns the obfuscated name for the variable or function sym
107 108 109 110 |
# File 'lib/jsobfu.rb', line 107 def sym(sym) return sym.to_s if @renames.nil? @renames[sym.to_s] end |
#to_s ⇒ String
Returns the (possibly obfuscated) code.
37 38 39 |
# File 'lib/jsobfu.rb', line 37 def to_s @code end |