SPB Git

Repository Public Research preview

wait-for-load.applescript 1.2 KB 32 lines

1-- Wait until a tab has finished navigating. Prints "loaded" or "timeout".2-- Usage: osascript wait-for-load.applescript <window-id> <tab-index> [timeout-seconds]3-- Safari sets "URL of tab" to the destination as soon as a navigation starts, while the old4-- document is still the one that answers JavaScript. The page is ready when the document's own5-- URL has caught up AND readyState is "complete" — checking readyState alone reports the OLD page.6on run argv7	set w to (item 1 of argv) as integer8	set t to (item 2 of argv) as integer9	set limit to 3010	if (count of argv) > 2 then set limit to (item 3 of argv) as integer11	set deadline to (current date) + limit12	set calm to 013	repeat while (current date) < deadline14		try15			tell application "Safari"16				set tabURL to URL of tab t of window id w17				set state to do JavaScript "location.href + '\\n' + document.readyState" in tab t of window id w18			end tell19			if state is (tabURL & linefeed & "complete") then20				set calm to calm + 121				if calm ≥ 2 then return "loaded"22			else23				set calm to 024			end if25		on error26			set calm to 0 -- the tab is between documents: try again27		end try28		delay 0.229	end repeat30	return "timeout"31end run32
Files main
Menu

Theme