class Marta::Injector::Syringe

We are injecting things to the page using the Syringe.

@note It is believed that no user will use it

Public Class Methods

new(engine, marta_what, title = 'Something important', old_data = Hash.new, folder = gem_libdir, custom_vars = Array.new, custom_scripts = Array.new) click to toggle source
# 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

Public Instance Methods

actual_injection() click to toggle source

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

# 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) click to toggle source

Syringe can run an array of scripts

# File lib/marta/injector.rb, line 106
def all_scripts(scripts_array)
  scripts_array.each do |script|
    run_script script
  end
end
files_to_page() click to toggle source

Injecting everything to the page

# 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_result() click to toggle source

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

# 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) click to toggle source

“first” or “last”.

# File lib/marta/injector.rb, line 44
def get_where(first)
  first ? "first" : "last"
end
html() click to toggle source

Taking a correct html file to inject

# File lib/marta/injector.rb, line 70
def html
  File.read(@folder + "/data/#{@what}.html")
end
insert_to_page(tag, inner, first = true) click to toggle source

Inserting to the page

# 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
js() click to toggle source

Taking a correct js file to inject

# File lib/marta/injector.rb, line 65
def js
  File.read(@folder + "/data/#{@what}.js")
end
run_script(script) click to toggle source

Syringe runs scripts

# File lib/marta/injector.rb, line 92
def run_script(script)
  @engine.execute_script script.gsub("\n",'')
end
set_var(var, value) click to toggle source

Syringe sets javascript variables

# 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) click to toggle source

Syringe takes array of hashes to set lots of variables

# 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
style() click to toggle source

Taking a correct css file to inject

# File lib/marta/injector.rb, line 75
def style
  File.read(@folder + "/data/style.css")
end