Kevin
Kevin

Reputation: 1106

Applescript : wait that the page is load on chrome

tell application "Safari"
        repeat until (do JavaScript "document.readyState" in tab 2 of window 1) is "complete"
        end repeat
    end tell
end if

how can I have this working with Chrome ?

Upvotes: 3

Views: 3401

Answers (3)

cpit
cpit

Reputation: 209

The loop repeat until (loading of tab 1 of window 1 is false) may work but only when there's a single tab in the active window. A better way to wait until the most recent tab is done loading in Chrome is using active tab:

tell application "Google Chrome"
    repeat until (loading of active tab of window 1 is false)
        delay 0.5
    end repeat
end tell

Upvotes: 0

user137369
user137369

Reputation: 5705

For Chromium browsers (Google Chrome, Brave Browser, Microsoft Edge, Opera, Vivaldi, …) you don’t need JavaScript workarounds, as they have proper support for checking if a tab is still loading.

In AppleScript:

tell application "Google Chrome" to return loading of tab 2 of window 1

In JavaScript for Automation (JXA):

Application('Google Chrome').windows[1].tabs[2].loading()

Upvotes: 1

CRGreen
CRGreen

Reputation: 3429

You should be able to do

tell application "Google Chrome"
repeat until (execute tab 1 of window 1 javascript "document.readyState" is "complete")
end repeat
    execute tab 1 of window 1 javascript "document.readyState"
end tell

but you may have pages that return "interactive" forever, or for a very long time.

Doing it with just AppleScript is supposed to be:

tell application "Google Chrome"
    repeat until (loading of tab 1 of window 1 is false)
        1 + 1 --just an arbitary line
    end repeat
    loading of tab 1 of window 1 --this just returns final status
end tell

but, to tell you the truth, I find it suspicious. I have a page that's pretty bare html in the body (http://www.crgreen.com/boethos/boethos.html), and it works with that, but for most pages (I've tried yahoo, macupdate, bbc news, jedit.org, etc.) I'm not getting a "loading = true", ever. It may be buggy.

Upvotes: 2

Related Questions