Question about WIN32OLE_EVENT.

M

Michael Kelly

------_=_NextPart_001_01C59992.3C23E412
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

So, I'm clear on how to attach to events:
=20
@ie =3D WIN32OLE.new( 'InternetExplorer.Application' )
browserEvents =3D WIN32OLE_EVENT.new( @ie, 'DWebBrowserEvents2' )
browserEvents.on_event { |*args| eventHandler( *args ) }
=20
But how does one detach from events?
=20
I'm working on a little utility that will generate Watir scripts using
IE automation. In the following code I am attaching to the browser
events to capture when a new HTML document is loaded. When a document
is loaded I want to attach to the document's events. When a new
document is loaded I want to detach from the old document's events.
=20
Here's the code so far. At the moment, each newly loaded document adds
to a growing list of event handlers, causing each click to produce
multiple calls to printClickStatement.
=20
Thanks,
=20
-=3Dmichael=3D-
=20
#requires
require 'win32ole'
=20
##//////////////////////////////////////////////////////////////////////
///////////////////////////////
##
## Main WatirMaker class. It handles the IE automation, which primarily
means event handling.
##
##//////////////////////////////////////////////////////////////////////
///////////////////////////////
class WatirMaker=20
=20

=20
##//////////////////////////////////////////////////////////////////////
////////////////////////////
##
## Initializer executed when "new" is called.
##
=20
##//////////////////////////////////////////////////////////////////////
////////////////////////////
def initialize
=20
end
=20

=20
##//////////////////////////////////////////////////////////////////////
////////////////////////////
##
## Handles all events from the browser and the HTML document.
##
=20
##//////////////////////////////////////////////////////////////////////
////////////////////////////
def eventHandler( event, *args )
case event
when "BeforeNavigate2"
webBrowser =3D args[0]
url =3D args[1]
frameName =3D args[3]
=20
if frameName =3D=3D ""
@webBrowsers =3D Hash.new
if @documentEventHandlers !=3D nil
@documentEventHandlers.each_value { |value|
value.on_event( 'onclick' ) {} }
end
@documentEventHandlers =3D Hash.new
end
=20
@webBrowsers =3D webBrowser printNavigateCommen...20 ------_=_NextPart_001_01C59992.3C23E412--
 
D

Dave Burt

Michael Kelly said:
So, I'm clear on how to attach to events:

@ie = WIN32OLE.new( 'InternetExplorer.Application' )
browserEvents = WIN32OLE_EVENT.new( @ie, 'DWebBrowserEvents2' )
browserEvents.on_event { |*args| eventHandler( *args ) }

But how does one detach from events?

# Replace the current event handler with an empty one:
browserEvents.on_event {}
I'm working on a little utility that will generate Watir scripts using
IE automation. ...

Let us know - that sounds really useful!

Cheers,
Dave
 
M

Masaki Suketa

In message "Question about WIN32OLE_EVENT."
So, I'm clear on how to attach to events:

@ie = WIN32OLE.new( 'InternetExplorer.Application' )
browserEvents = WIN32OLE_EVENT.new( @ie, 'DWebBrowserEvents2' )
browserEvents.on_event { |*args| eventHandler( *args ) }

But how does one detach from events?

Unfortunately, there is no way to detach from events.
So, use global flag variable instead.

$attached = true
def eventHandler(*args)
if ($attached)
....
end
end
# attach event handler
browserEvents.on_event{|*args| eventHandler(*args)}
...
# dettach event handler
$attached = false

In future release, Win32OLE provides the method to detach event.

Regards,
Masaki Suketa
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top