Wait for web page to finish loading - again

This script

tell application "Safari"
	activate
	open location "https://www.facebook.com/events/?date_filter_option=TODAY&location_id= 111948785483165&start_date=TODAY&discover_tab=CUSTOM"
	repeat while document 1's source = ""
		delay 0.5
	end repeat
	tell document 1 to repeat
		log (do JavaScript "document.readyState")
	end repeat
end tell

returns, I sh*t you not, this (I comment/explain below the result):

tell application “Safari”
activate
open location “Events Near Me | Facebook
get source of document 1
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(loading)
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(complete)
do JavaScript “document.readyState”
(complete)

First there are 23 loops when the page is complete, and then a couple of hundred (I didn’t count) loops where it is loading (After it was complete!!!) followed by complete again.

The problem is obvious, When I check if the page is fully loaded I get a positive answer in spite of there is a second loading a few moments later. And in the next step in my script it expects the page to be fully loaded, but since it isn’t my script breaks.

Are there other methods than checking the source or the readyState that could work in my situation? Maybe access Safari’s JS-engine and check if it is busy or idle?

As I sad in the previous topic, do JavaScript “document.readyState” doesn’t work consistently. In my tests following script returns empty string… That is the reason why I suggested to you 2 other scripts in the previous topic.

tell application "Safari"
	activate
	open location "https://www.facebook.com/events/?date_filter_option=TODAY&location_id= 111948785483165&start_date=TODAY&discover_tab=CUSTOM"
	tell document 1 to do JavaScript "document.readyState=\"loading\""
	repeat until ((do JavaScript "document.readyState" in document 1) is "complete")
		delay 0.5
	end repeat
	get document 1's source --> ""
end tell

.
Following works consistently:
.

tell application "Safari"
	open location "https://www.facebook.com/events/?date_filter_option=TODAY&location_id= 111948785483165&start_date=TODAY&discover_tab=CUSTOM"
	my waitSafariWebPageLoading() -- or:
	-- my waitSafariWebPageLoading2(20)
	get document 1's source
end tell

on waitSafariWebPageLoading()
	tell application "System Events" to tell application process "Safari"
		repeat until (UI element "Reload this page" of group 3 of toolbar 1 of window 1 exists) or (UI element "Reload this page" of group 2 of toolbar 1 of window 1 exists)
			delay 0.1
		end repeat
	end tell
end waitSafariWebPageLoading

on waitSafariWebPageLoading2(loadingWaitMaximumSeconds as integer)
	set lineChangingChars to {linefeed, return, character id 11, character id 12, character id 133, character id 8232, character id 8233}
	set {ATID, htmlEnding} to {AppleScript's text item delimiters, ""}
	tell application "Safari"
		repeat 100 * loadingWaitMaximumSeconds times -- wait maximum seconds
			delay 0.1
			set AppleScript's text item delimiters to {"<", ">"}
			try
				copy text item -2 of (get source of front document) to htmlEnding
			end try
			set AppleScript's text item delimiters to lineChangingChars
			set htmlEnding to text items of htmlEnding
			set AppleScript's text item delimiters to ""
			set htmlEnding to "<" & htmlEnding & ">"
			if htmlEnding is "</html>" then exit repeat
		end repeat
	end tell
	set AppleScript's text item delimiters to ATID
	if htmlEnding is "</html>" then return true
	display notification "The webpage loading failed"
	return false
end waitSafariWebPageLoading2

Perhaps it would be possible to build a JavaScript script that

  • loads the required page (perhaps in an iFrame),
  • uses a DOMContentLoaded handler,
  • in which it returns the content of the document!

that would avoid the polling and (if it’s possible at all) be a reliable way to get the content when it’s available.

Here’s a related article How to Implement an onload Event in iframe in JavaScript

Have you seen or tried this AppleScript native function

on waitPageLoaded()
	tell application "System Events"
		repeat until exists (buttons of groups of toolbar 1 of window 1 of process "Safari" whose title = "Reload this page")
			delay 0.5
		end repeat
	end tell
end waitPageLoaded