Sha256: 877925144d787968f489cf3e70208e350a9d2bdc0092ba63bb71426635d01354

Contents?: true

Size: 1.3 KB

Versions: 13

Compression:

Stored size: 1.3 KB

Contents

module Wovnrb
  class ScriptReplacer < ReplacerBase
    def initialize(store)
      @store = store
    end

    def replace(dom, lang)
      remove_embed_wovn_script(dom)
      add_wovn_script(dom, lang)
    end

    private
    def remove_embed_wovn_script(dom)
      dom.xpath('//script').each do |script_node|
        if script_node['src'] && script_node['src'] =~ /^\/\/j.(dev-)?wovn.io(:3000)?\//
          script_node.remove
        end
      end
    end

    def add_wovn_script(dom, lang)
      parent_node = dom.at_css('head') || dom.at_css('body') || dom.at_css('html')

      # INSERT BACKEND WIDGET
      insert_node = Nokogiri::XML::Node.new('script', dom)
      insert_node['src'] = '//j.wovn.io/1'
      insert_node['async'] = true
      version = defined?(VERSION) ? VERSION : ''
      insert_node['data-wovnio'] = "key=#{@store.settings['user_token']}&backend=true&currentLang=#{lang.lang_code}&defaultLang=#{@store.settings['default_lang']}&urlPattern=#{@store.settings['url_pattern']}&version=#{version}"
      # do this so that there will be a closing tag (better compatibility with browsers)
      insert_node.content = ' '
      if parent_node.children.size > 0
        parent_node.children.first.add_previous_sibling(insert_node)
      else
        parent_node.add_child(insert_node)
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
wovnrb-0.2.07.1 lib/wovnrb/html_replacers/script_replacer.rb
wovnrb-0.2.13 lib/wovnrb/html_replacers/script_replacer.rb
wovnrb-0.2.12 lib/wovnrb/html_replacers/script_replacer.rb
wovnrb-0.2.11 lib/wovnrb/html_replacers/script_replacer.rb
wovnrb-0.2.10 lib/wovnrb/html_replacers/script_replacer.rb
wovnrb-0.2.09 lib/wovnrb/html_replacers/script_replacer.rb
wovnrb-0.2.08 lib/wovnrb/html_replacers/script_replacer.rb
wovnrb-0.2.07 lib/wovnrb/html_replacers/script_replacer.rb
wovnrb-0.2.06 lib/wovnrb/html_replacers/script_replacer.rb
wovnrb-0.2.05 lib/wovnrb/html_replacers/script_replacer.rb
wovnrb-0.2.04 lib/wovnrb/html_replacers/script_replacer.rb
wovnrb-0.2.03 lib/wovnrb/html_replacers/script_replacer.rb
wovnrb-0.2.02 lib/wovnrb/html_replacers/script_replacer.rb