Sha256: b4e24f47a35b9c54b5260c6f1865d525c6b6df5b10faa2a042d3818ce555f724

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

module SeoCache
  class PageRender
    def initialize
      init_driver
    end

    def get(url, quit_after_render = true)
      @driver.get(url)

      sleep SeoCache.wait_time_for_page_loading if SeoCache.wait_time_for_page_loading

      return @driver.page_source
    rescue StandardError => error
      SeoCache.log_error(error.message)

      return false
    ensure
      @driver&.quit if quit_after_render
    end

    def close_connection
      @driver&.quit
    end

    private

    def init_driver
      if SeoCache.logger_path
        Selenium::WebDriver.logger.output = SeoCache.logger_path
        Selenium::WebDriver.logger.level  = SeoCache.logger_level
      end

      # Webdrivers.cache_time            = 86_400 * 10 # Chromedriver will be cached for 10 days (except if it detects a new version of Chrome)

      Selenium::WebDriver::Chrome.path = SeoCache.chrome_path if SeoCache.chrome_path

      browser_options = %w[headless incognito no-sandbox disable-gpu disable-infobars disable-dev-shm-usage disable-web-security disable-extensions disable-logging disable-notifications disable-sync window-size=1920x1080]
      browser_options << "remote-debugging-port=#{SeoCache.chrome_debugging_port}" if SeoCache.chrome_debugging_port

      @driver = ::Selenium::WebDriver.for(
        :chrome,
        options: Selenium::WebDriver::Chrome::Options.new(
          args: browser_options
        )
      )
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
seo_cache-2.0.0 lib/seo_cache/page_render.rb