This code will basically run until the currently-loading page is done loading in Safari. This code is working in Safari 4.0.3 running on OS X 10.6; I haven't tested in other versions of Safari or the OS.
Mac OS X Hints
http://hints.macworld.com/article.php?story=20091101035318405