Document source
Last modification: Friday, May 01, 2009 10:29 amReturns the source of the specified Safari document, if it contains the closing html tag. If the string "</html>" is not encountered the source is returned after it hasn't changed for the specified amount of seconds.
Required parameters
Parameter 1 (Safari document)The Safari document to get the source from. Specify false if the frontmost is to be used.
Parameter 2 (integer)
The amount of seconds that the function waits until returning the source if no closing html tag is found.
Function Call
safariDocumentSource(false, 2)Implementation
on safariDocumentSource(_document, _waitSeconds)(*
Returns the source of the specified Safari document,
if it contains the closing html tag. If the string
"</html>" is not encountered the source is returned
after it hasn't changed for the specified amount of
seconds.
Parameter 1 (Safari document)
The Safari document to get the source from.
Specify false if the frontmost is to be used.
Parameter 2 (integer)
The amount of seconds that the function waits until
returning the source if no closing html tag is found.
*)
-- Make sure that Safari is running
tell application "System Events"
if (exists process "Safari") is false then return false
end tell
if _document is not false then
-- Make sure that the specified Safari document exists has a window
tell application "Safari"
if (exists _document) is false then return false
end tell
else
-- No document has been specified, exit early if no document exists
tell application "Safari"
if (exists document 1) is false then return false
end tell
end if
-- Initialize variables
set _unchangedFor to 0
set _sourceCache to false
repeat 100 times
-- Get the source
try
if _document is false then
tell application "Safari" to set _source to source of document 1
else
tell application "Safari" to set _source to source of _document
end if
get _source
on error
set _source to ""
end try
-- Check for closing html tag
if _source contains "</html>" then exit repeat
-- Keep track of changes
if _source is not _sourceCache then
set _unchangedFor to 0
set _sourceCache to _source
else
set _unchangedFor to _unchangedFor + 0.1
end if
-- Exit loop on timeout
if _unchangedFor > _waitSeconds then exit repeat
delay 0.1
end repeat
return _source
end safariDocumentSource