Class: Marta::Injector::Syringe

Inherits:
Object
  • Object
show all
Includes:
OptionsAndPaths, Server
Defined in:
lib/marta/injector.rb

Overview

Note:

It is believed that no user will use it

We are injecting things to the page using the Syringe.

Instance Method Summary collapse

Constructor Details

#initialize(engine, marta_what, title = 'Something important', old_data = Hash.new, folder = gem_libdir, custom_vars = Array.new, custom_scripts = Array.new) ⇒ Syringe

Returns a new instance of Syringe.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/marta/injector.rb', line 25

def initialize(engine, marta_what, title = 'Something important',
               old_data = Hash.new, folder = gem_libdir,
               custom_vars = Array.new, custom_scripts = Array.new)
  @what = marta_what
  @title = title
  @data = old_data
  @data ||= Hash.new
  @engine = engine
  @folder = folder
  @custom_vars = custom_vars
  @custom_scripts = custom_scripts
  @default_vars = [{"marta_what": "\"#{@title}\""},
    {"old_marta_Data": @data.to_s.gsub('=>',':').gsub('nil','null')},
    {"martaPort": SettingMaster.port.to_s}]
  @default_scripts =
            ["document.marta_add_data(); document.marta_connect();"]
end

Instance Method Details

#actual_injectionObject

It is never used without get_result. But it can be used to show some message for user



114
115
116
117
118
# File 'lib/marta/injector.rb', line 114

def actual_injection
  files_to_page
  set_vars(@default_vars + @custom_vars)
  all_scripts(@default_scripts + @custom_scripts)
end

#all_scripts(scripts_array) ⇒ Object

Syringe can run an array of scripts



106
107
108
109
110
# File 'lib/marta/injector.rb', line 106

def all_scripts(scripts_array)
  scripts_array.each do |script|
    run_script script
  end
end

#files_to_pageObject

Injecting everything to the page



80
81
82
83
84
# File 'lib/marta/injector.rb', line 80

def files_to_page
  insert_to_page('div', html)
  insert_to_page('script', js, false)
  insert_to_page('style', style, false)
end

#get_resultObject

Retrieving result if js var = marta_confirm_mark is true we are returning js var = marta_result. So custom js should always return both.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/marta/injector.rb', line 123

def get_result
  result = false
  while result != true
    # When Marta can't get a result she is reinjecting her stuff
    result = MartaServer.wait_user_dialog_response
    # We need double check for iframes here. It should be 100% changed.
    if !result
      result = @engine.execute_script("return document.marta_confirm_mark")
    end
    if (!result and !@engine.element(id: 'marta_s_everything').exists?)
      actual_injection
    end
  end
  run_script("return document.marta_result")
end

#get_where(first) ⇒ Object

“first” or “last”.



44
45
46
# File 'lib/marta/injector.rb', line 44

def get_where(first)
  first ? "first" : "last"
end

#htmlObject

Taking a correct html file to inject



70
71
72
# File 'lib/marta/injector.rb', line 70

def html
  File.read(@folder + "/data/#{@what}.html")
end

#insert_to_page(tag, inner, first = true) ⇒ Object

Inserting to the page



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/marta/injector.rb', line 49

def insert_to_page(tag, inner, first = true)
  where = get_where(first)
  if tag != "script"
    script = <<-JS
    var newMartaObject = document.createElement('#{tag}');
    newMartaObject.setAttribute('martaclass','marta_#{tag}');
    newMartaObject.innerHTML   = '#{inner}';
    document.body.insertBefore(newMartaObject,document.body.#{where}Child);
    JS
  else
    script = inner
  end
  run_script(script)
end

#jsObject

Taking a correct js file to inject



65
66
67
# File 'lib/marta/injector.rb', line 65

def js
  File.read(@folder + "/data/#{@what}.js")
end

#run_script(script) ⇒ Object

Syringe runs scripts



92
93
94
# File 'lib/marta/injector.rb', line 92

def run_script(script)
  @engine.execute_script script.gsub("\n",'')
end

#set_var(var, value) ⇒ Object

Syringe sets javascript variables



87
88
89
# File 'lib/marta/injector.rb', line 87

def set_var(var, value)
  insert_to_page('script', "document.#{var} = #{value};", false)
end

#set_vars(vars_array) ⇒ Object

Syringe takes array of hashes to set lots of variables



97
98
99
100
101
102
103
# File 'lib/marta/injector.rb', line 97

def set_vars(vars_array)
  vars_array.each do |var_hash|
    var_hash.each_pair do |var_name, var_value|
      set_var var_name.to_s, var_value
    end
  end
end

#styleObject

Taking a correct css file to inject



75
76
77
# File 'lib/marta/injector.rb', line 75

def style
  File.read(@folder + "/data/style.css")
end