lib/iegrip.rb in iegrip-0.1.0 vs lib/iegrip.rb in iegrip-0.1.3

- old
+ new

@@ -19,13 +19,12 @@ @raw_object.visible = true fs = FileSystemObject.instance ver = fs.GetFileVersion(@raw_object.FullName) @majorVersion = ver.split(/\./)[0].to_i @urlDownloadToFile = Win32API.new('urlmon', 'URLDownloadToFileA', %w(l p p l l), 'l') - #@event = WIN32OLE_EVENT.new(@raw_object,"DWebBrowserEvents2") - @downloading = 0 - #setup_event() + @event = WIN32OLE_EVENT.new(@raw_object,"DWebBrowserEvents2") + setup_event() end def version @majorVersion end @@ -33,82 +32,47 @@ def document() doc = Document.new(@raw_object.Document, self) end def navigate(url) + before_wait() @raw_object.navigate(url) - sleep 0.5 wait_stable() end + def before_wait() + @location_url = nil + @complete_flag = nil + end + COMPLETE_STATE = 4 - def wait_stable() - stable_counter = 0 + def wait_stable(timeout=30) + start_time = Time.now loop do - break if stable_counter >= 3 - if (@raw_object.Busy != true) and (@raw_object.ReadyState == COMPLETE_STATE) - stable_counter += 1 - else - sleep 0.5 - stable_counter = 0 - end + break if @complete_flag + break if (Time.now - start_time) > timeout + WIN32OLE_EVENT.message_loop end end def export(href, filename) @urlDownloadToFile.call(0, href, filename, 0, 0) end private def setup_event() - @event.on_event("BeforeNavigate2") { - @downloading += 1 - #puts " BeforeNavigate2. @downloading=#{@downloading}" + @event.on_event("NavigateComplete2") {|param| + unless @location_url # Keep First location + @location_url = param.LocationURL + end } - #@event.on_event("CommandStateChange") { - # puts " CommandStateChange" - #} @event.on_event("DocumentComplete") {|param| - # a document is completely loaded and initialized. - if param.LocationURL == @raw_object.LocationURL - @downloading = 0 - #puts " DocumentComplete. LocationURL match! : #{param.LocationURL} @downloading=#{@downloading}" - else - @downloading -= 1 if @downloading > 0 - #puts " DocumentComplete. #{param.LocationURL} @downloading=#{@downloading}" + if @location_url == param.LocationURL + @complete_flag = true end } - #@event.on_event("DownloadBegin") { - # # a navigation operation begins. - # puts " DownloadBegin. " - #} - #@event.on_event("DownloadComplete") { - # # a navigation operation finishes, is halted, or fails. - # puts " Download Complete." - #} - #@event.on_event("NavigateComplete2") { - # # a navigation to a link is completed on a window element or a frameSet element. - # puts " NavigateComplete2." - #} - #@event.on_event("NewProcess") { - # puts " NewProcess" - #} - #@event.on_event("ProgressChange") { - # puts " ProgressChange" - #} - #@event.on_event("PropertyChange") { - # puts " PropertyChange" - #} - #@event.on_event("StatusTextChange") { - # puts " StatusTextChange" - #} - #@event.on_event("UpdatePageStatus") { - # puts " UpdatePageStatus" - #} - - end end module ElementParent def parentNode @@ -211,10 +175,11 @@ private def get_inner(tag) inner = [tag.tagName] + outer = nil inner.push "id='#{tag.ID}'" if tag.ID != "" case tag.tagName when "a" href = tag.href @@ -471,10 +436,11 @@ @raw_object.value end alias text value def click + @ie_obj.before_wait() if @ie_obj.version >= 10 @raw_object.click(false) else @raw_object.click end @@ -541,9 +507,13 @@ if next_element parent.insertBefore(new_element, next_element) else parent.appendChild(new_element) end + end + + def document + Document.new(@raw_object.document, @ie_obj) end private def replace_cr_code(text)