You are here: follwoup: PHP, timers, and event handler callbacks « PHP Programming Language « IT news, forums, messages
follwoup: PHP, timers, and event handler callbacks

Posted by Csaba Gabor on 09/04/07 13:51

On Sep 3, 5:57 pm, shimmyshack <matt.fa...@gmail.com> wrote:
> > > Cant you just fire the navigate() method, then sleep for $timeoutTime
> > > and then execute onDocumentComplete or similar, assume that if it
> > > doesn't return true that you are safe to fire an error.
>
> > Thanks, this is a great suggestion.
>
> > > However you
> > > recognise that even IE cant tell you whether all the frames in a page
> > > have loaded, and what does onDocumentComplete mean for some remote but
> > > unessential elements - adverts say..
>
> > This is a good point, but I believe I am aware of the relevant issues
> > (including the difficulty of associating frames with their
> > containers). Actually, downloadComplete is the event that I key on.
> > For example, hit that back button on IE and documentComplete does not
> > fire, whereas downloadComplete does. However, downloadComplete is
> > exceptionally poorly designed since all you get back is the IE object,
> > and not even a handle to the window in question. Nevertheless, for a
> > situation where I only need access to the top level DOM,
> > downloadComplete (with $ie->readyState >= 3) suffices.
....
> I see, I have to say some of the .NET stuff feels more native and
> there are great sites like codeproject.com to help out.
> I once made a php script to navigate ie, so I could "follow" my users
> around one of my sites, and see what they did, there was a javascript
> in the head that just sent the users actions back to my home server
> where a php script listened, and automated IE, I used it for a while
> and then got tired of the weird syntax of php+COM.

What an awesome user support idea. I may have to do that, too.

OK, here's the followup / conclusions.

Firstly, the example below is for page sequencing of IE using http://php.net/COM
(again this is with CLI PHP. There is no notion of PHP acting as a
server for these purposes). The actions are therefore navigations,
and button/image/link clicks. Thus, I am particularly interested in
ie's event handlers for when a page finishes loading (which
unfortunately seem to be inadequate) sufficiently that I can work with
it. For my purposes, this means that I am NOT interested in IFRAMES,
etc.

Before worrying about timeouts, I hooked things up to ie's
downloadComplete event, as previously mentioned, so that when it
fired, I knew my page had sufficiently loaded to have a DOM and I was
ready to kick off the next callback function, which in turned
navigated to a subsequent page and so on.

It was blazingly fast. Too fast, in fact. What happened is that as I
loaded the first page (say php.net), I would get the downloadComplete
event firing, so I would start navigating to the next page. That
means that I set a new callback function and started the new
navigation function. And then downloadComplete would fire ON THE OLD
PAGE (say a frame or image had to be loaded in). So my new callback
function would get invoked even though ie was still on the previous
page. And furthermore, I have no idea how many of these
downloadComplete events I'll be receiving in general.

None too happy about this, I grumbled and hooked things up to
documentComplete. Now I got farther, but this time a new issue bit
me. Navigation to the subsequent page involves clicking a button.
Trouble is, the button and containing form get installed via an onload
javascript. The reason it's a problem is that the callback which is
doing the clicking (and finding of the button/form) is reached from
the documentComplete handler, and until that handler returns,
the .onload function does not kick off. Oops. This is a problem,
which as far as I can figure, really force me into using
window.setTimeout functionality.

Seeing no help for it, I wrote a PHP function,
function timer ($msdelay, $callbackName) { // optional arguments }
which keeps a hidden copy of $ie around just so that it can tie into
$ie's window.setTimeout()
So, my documentComplete handler's final line is:
timer (0, "callbackName", $this->ie);
And this works just fine.

But I am not so happy about having to wait around for ads to load just
so $ie's ready state can get to 4 so that documentComplete can fire.
No, I'd much rather the faster downloadComplete. So in the case where
I am assured distinct page as a result of the next navigation, I send
along the beginning (prefix) of the expected url along with the
callback function so that when documentComplete fires, it checks
whether the beginning of its resultant href matches the urlPrefix. If
so, it will dispatch to the callback, otherwise it has to wait for
documentComplete. This is working well, in initial tests.

Finally, I returned to the idea the timeout. Since my actions are no
longer directly invoked directly from within event handlers (ie. I get
there from the event handlers via a timer(...)), I can either use your
loop suggestion or directly use another timer. However, every
callback should have an argument to the tune of $timedOut=false and a
static variable to recognize that it has already been entered. That
is to say, for every action with a timeout, there will be a firing
upon both the action conclusion and upon the timeout. Therefore, the
called function must be reentrant and be able to deal with the second
invokation. For the nonce, I am a happy camper.

Csaba Gabor from Vienna
PS. Initial thread at
http://groups.google.com/group/comp.lang.php/browse_frm/thread/fd9e5b193a244739/

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация