Class: Win32::CaptureIE::Browser

Inherits:
Window
  • Object
show all
Includes:
ReadyState, FFI::User32
Defined in:
lib/win32/capture_ie/browser.rb

Defined Under Namespace

Modules: ReadyState

Constant Summary collapse

EMBEDDING_IE_WINDOW_CLASS_NAME =
"Internet Explorer_Server"

Constants included from ReadyState

ReadyState::COMPLETE, ReadyState::INTERACTIVE, ReadyState::LOADED, ReadyState::LOADING, ReadyState::UNINITIALIZED

Constants included from FFI::User32

FFI::User32::GW_HWNDFIRST, FFI::User32::GW_HWNDLAST, FFI::User32::GW_HWNDNEXT, FFI::User32::GW_HWNDPREV

Instance Attribute Summary collapse

Attributes inherited from Window

#hwnd

Instance Method Summary collapse

Methods included from FFI::User32

each_child_window, get_class_name, get_first_child, get_first_sibling, get_last_sibling, get_next_sibling, get_prev_sibling, get_top_window, get_window, get_window_rect, with_window_dc

Methods included from FFI::Base

#define_ffi_entry

Methods inherited from Window

#bring_window_to_top, #find_child_window_by_classname, #list_child_window, #wait_for_redraw

Constructor Details

#initialize(toplevel_hwnd, doc_hwnd = nil, browser = nil, document = nil, window = nil) ⇒ Browser

Returns a new instance of Browser.



25
26
27
28
29
30
31
32
33
# File 'lib/win32/capture_ie/browser.rb', line 25

def initialize(toplevel_hwnd, doc_hwnd=nil, browser=nil, document=nil, window=nil)
  super(toplevel_hwnd)
  @doc_hwnd = doc_hwnd || toplevel_hwnd
  @browser = browser
  @document = document
  @window = window
  @body = nil
  @embedding_ie_hwnd = nil
end

Instance Attribute Details

#browserObject (readonly)

Returns the value of attribute browser.



23
24
25
# File 'lib/win32/capture_ie/browser.rb', line 23

def browser
  @browser
end

#doc_hwndObject (readonly)

Returns the value of attribute doc_hwnd.



23
24
25
# File 'lib/win32/capture_ie/browser.rb', line 23

def doc_hwnd
  @doc_hwnd
end

Instance Method Details

#==(other) ⇒ Object



295
296
297
# File 'lib/win32/capture_ie/browser.rb', line 295

def ==(other)
  @doc_hwnd == other.doc_hwnd
end

#abs_element_area(id_or_element) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/win32/capture_ie/browser.rb', line 86

def abs_element_area(id_or_element)
  # Let's calculate the absolute position of the object on the page
  elem = to_element(id_or_element)
  x = y = 0
  p = elem
  while p
    x += p.offsetLeft
    y += p.offsetTop
    p = p.offsetParent
  end

  # This is the size of the object including its border
  w = elem.offsetWidth
  h = elem.offsetHeight

  Area.new(x, y, w, h)
end

#bodyObject



51
52
53
# File 'lib/win32/capture_ie/browser.rb', line 51

def body
  @body ||= body0 if document
end

#body_areaObject



64
65
66
# File 'lib/win32/capture_ie/browser.rb', line 64

def body_area
  Area.new(0, 0, body.scrollWidth, body.scrollHeight) if body
end

#captorObject



290
291
292
# File 'lib/win32/capture_ie/browser.rb', line 290

def captor
  Win32::CaptureIE::ScreenCaptor.new(self)
end

#capture_browser(writable, opts = nil, &rmagick_filter) ⇒ Object

call-seq:

capture_browser(writable, :format => "bmp")
capture_browser(writable, :format => "bmp", :only_drawing_area => true)
capture_browser(writable, :format => "bmp") {|image| ... }
capture_browser(writable, :format => "bmp", :only_drawing_area => true) {|image| ... }

capture IE window and write to writable.

  • writable is String, Pathname or object that implemented #write method (Such as IO stream).

  • format is image format, “GIF” or “JPG” for example.

  • You can specify optional block argument to filter Magick::ImageList. If RMagick is not installed, block argument will ignore.

ex):

Win32::CaptureIE.start do |b|
  b.capture_browser("browser.bmp")
  b.capture_browser("browser.png", :only_drawing_area => true)
  open("browser.jpg", "wb") do |w|
    b.capture_browser(w, :format => "jpg")
  end
end

RMagick example:

url = "http://win32-captureie.rubyforge.org/html/files/README_txt.html"
Win32::CaptureIE.start(url) do |b|
  b.capture_page("readme.jpg") do |image|
    area = b.abs_element_area("description")
    gc = Magick::Draw.new
    gc.stroke = "red"
    gc.fill = "red"
    gc.stroke_width = 4
    gc.stroke_opacity(0.6)
    gc.fill_opacity(0.3)
    gc.rectangle(*area.rect)
    gc.draw(image)
    image
  end
end


186
187
188
189
# File 'lib/win32/capture_ie/browser.rb', line 186

def capture_browser(writable, opts=nil, &rmagick_filter)
  opts = parse_opts_for_capture_xxx(opts)
  captor.capture_browser(opts).save(writable, opts[:format], &rmagick_filter)
end

#capture_browser_image(opts = nil) ⇒ Object

call-seq:

capture_browser_image(:format => "bmp")
capture_browser_image(:format => "bmp", :only_drawing_area => true)

capture IE window and return Magick::ImageList or BitMap blob as String.

  • format is image format, “GIF” or “JPG” for example.

ex):

Win32::CaptureIE.start do |b|
  b.capture_browser_image.write("foo.bmp")
  b.capture_browser_image(:format => "png").write("foo.png")
end


206
207
208
209
# File 'lib/win32/capture_ie/browser.rb', line 206

def capture_browser_image(opts=nil)
  opts = parse_opts_for_capture_xxx_image(opts)
  captor.capture_browser(opts).to_image(opts[:format])
end

#capture_elements(writable, id_or_elements, opts = nil, &rmagick_filter) ⇒ Object

call-seq:

capture_elements(writable, ["id", "id2"], :format => "bmp")
capture_elements(writable, ["id", "id2"], :format => "bmp") {|image| ... }

Captures specified elements (outer bounding box of all elements) and write to writable.

  • id_or_elements is ID of DOM element or WIN32OLE object.

see #capture_browser for more detail.



241
242
243
244
# File 'lib/win32/capture_ie/browser.rb', line 241

def capture_elements(writable, id_or_elements, opts=nil, &rmagick_filter)
  opts = parse_opts_for_capture_xxx(opts)
  captor.capture_elements(*id_or_elements).save(writable, opts[:format], &rmagick_filter)
end

#capture_elements_image(id_or_elements, opts = nil) ⇒ Object

capture specified elements and return Magick::ImageList or BitMap blob as String.

see #capture_browser_image for more detail.



249
250
251
252
# File 'lib/win32/capture_ie/browser.rb', line 249

def capture_elements_image(id_or_elements, opts=nil)
  opts = parse_opts_for_capture_xxx_image(opts)
  captor.capture_elements(*id_or_elements).to_image(opts[:format])
end

#capture_page(writable, opts = nil, &rmagick_filter) ⇒ Object

call-seq:

capture_page(writable, :format => "bmp")
capture_page(writable, :format => "bmp") {|image| ... }

capture whole web page and write to writable.

see #capture_browser for more detail.



218
219
220
221
# File 'lib/win32/capture_ie/browser.rb', line 218

def capture_page(writable, opts=nil, &rmagick_filter)
  opts = parse_opts_for_capture_xxx(opts)
  captor.capture_page.save(writable, opts[:format], &rmagick_filter)
end

#capture_page_image(opts = nil) ⇒ Object

capture whole web page and return Magick::ImageList or BitMap blob as String.

see #capture_browser_image for more detail.



226
227
228
229
# File 'lib/win32/capture_ie/browser.rb', line 226

def capture_page_image(opts=nil)
  opts = parse_opts_for_capture_xxx_image(opts)
  captor.capture_page.to_image(opts[:format])
end

#compatible_mode?Boolean

Returns:

  • (Boolean)


104
105
106
107
# File 'lib/win32/capture_ie/browser.rb', line 104

def compatible_mode?
  v = document.compatMode rescue nil
  return (v.nil? or v == "BackCompat")
end

#documentObject



43
44
45
# File 'lib/win32/capture_ie/browser.rb', line 43

def document
  @document ||= browser.document rescue nil if browser
end

#embedding_ie_hwndObject



123
124
125
# File 'lib/win32/capture_ie/browser.rb', line 123

def embedding_ie_hwnd
  @embedding_ie_hwnd ||= find_embedding_ie_hwnd
end

#get_element(id, parent = document) ⇒ Object

Raises:

  • (ArgumentError)


68
69
70
71
72
# File 'lib/win32/capture_ie/browser.rb', line 68

def get_element(id, parent=document)
  r = parent.getElementById(id)
  raise ArgumentError, "unknown element `##{id}'" unless r
  r
end

#hashObject



299
300
301
# File 'lib/win32/capture_ie/browser.rb', line 299

def hash
  @doc_hwnd.hash
end

#load_error?Boolean

Returns:

  • (Boolean)


109
110
111
112
# File 'lib/win32/capture_ie/browser.rb', line 109

def load_error?
  url = document.url rescue ""
  [/\Ares:\/\//, /\bshdoclc\b/].all?{|re| re.match(url) }
end


114
115
116
117
# File 'lib/win32/capture_ie/browser.rb', line 114

def navigate(url, wait=true)
  browser.Navigate(url)
  wait_for_complete if wait
end

#quit(ignore_error = true) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/win32/capture_ie/browser.rb', line 35

def quit(ignore_error = true)
  begin
    browser.Quit
  rescue WIN32OLERuntimeError => e
    raise e unless ignore_error
  end
end

#save_excursionObject

save window scroll position.



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/win32/capture_ie/browser.rb', line 133

def save_excursion
  top, left = body.scrollTop, body.scrollLeft
  begin
    yield
  ensure
    begin
      body.scrollTop, body.scrollLeft = top, left
    rescue => ignored
    end
  end
end

#to_element(id_or_element) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/win32/capture_ie/browser.rb', line 74

def to_element(id_or_element)
  case id_or_element
  when WIN32OLE
    id_or_element
  when String
    get_element(id_or_element)
  else
    raise ArgumentError, "Invalid argument #{id_or_element.inspect}:#{id_or_element.class}," +
      " expected String or WIN32OLE."
  end
end

#wait_for_complete(interval = 0.5) ⇒ Object



119
120
121
# File 'lib/win32/capture_ie/browser.rb', line 119

def wait_for_complete(interval=0.5)
  sleep(interval) while browser.ReadyState != COMPLETE
end

#windowObject



47
48
49
# File 'lib/win32/capture_ie/browser.rb', line 47

def window
  @window ||= document.parentWindow rescue nil if document
end