lib/docapurl/browser.rb in docapurl-0.1.3 vs lib/docapurl/browser.rb in docapurl-0.2.0
- old
+ new
@@ -22,23 +22,28 @@
options[:path] ||= @save_path
host = fetch_domain(url)
options[:path] ||= "screenshot-#{host.to_s == '' ? '' : "#{host}-"}#{Time.now.strftime('%F-%T')}.jpg"
logger.info "browser begin to visit url #{url}"
+ set_callback("before_visit_func", options)
browser.go_to(url)
+ set_callback("after_visit_func", options)
+
+
logger.info 'visited'
max_pagedown = options[:max_pagedown] || 5
pagedown_to_bottom = options.delete :pagedown_to_bottom
visit_whole_page(browser, max_pagedown: max_pagedown, pagedown_to_bottom: pagedown_to_bottom)
sleep_before_screen = options.delete :sleep_before_screen
logger.info "sleep #{sleep_before_screen.to_i} second before screenshot"
sleep(sleep_before_screen.to_i)
- before_screenshot_func = options.delete :before_screenshot_func
- before_screenshot_func.call(self) unless before_screenshot_func.nil?
+
+ set_callback("before_screenshot_func", options)
browser.screenshot(**options)
+ set_callback("after_screenshot_func", options)
logger.info "screenshot ended, path = #{options[:path]}"
end
def close
@@ -67,9 +72,17 @@
browser.keyboard.type(:PageDown)
end
logger.info "press HOME .."
browser.keyboard.type(:Home)
end
+
+ private
+
+ def set_callback(name, options)
+ the_funcion = options.delete name
+ the_funcion.call(self) unless the_funcion.nil?
+ end
+
class << self
def cap(url, path = nil, browser_options = {}, cap_options = {})
browser = new(browser_options)
cap_options[:path] = path