Search code examples
javascriptgoogle-chromeapplescriptloadingautomator

Reload Webpage in Google Chrome until HTML contains specific text


This code works for Safari (reload webpage till HTML contains specific text). How to fix it for Chrome?

tell application "Safari"

set keyword to "Authentication mail"
repeat
    set myWindow to current tab of first window
    activate
    
    do JavaScript "window.location.reload()" in myWindow
    
    repeat while (do JavaScript "document.readyState" in document 1) is not "complete"
        delay 0.5
    end repeat
    
    set pageContent to do JavaScript ("window.document.documentElement.outerHTML") in myWindow
    
    if pageContent contains keyword then
        beep beep beep
        exit repeat
    end if
    delay 2 -- wait a bit before running again
end repeat end tell

Solution

  • I didn't know the website you were targeting so I had no way of testing the code below.

    tell application "Google Chrome"
        set keyword to "Authentication mail"
        repeat
            set myWindow to active tab of first window
            tell myWindow
                activate
                execute javascript "window.location.reload();"
                repeat while (execute javascript "document.readyState;") is not "complete"
                    delay 0.5
                end repeat
                set pageContent to execute javascript "window.document.documentElement.outerHTML;"
                if pageContent contains keyword then
                    tell current application to beep (beep (beep))
                    exit repeat
                end if
                delay 2 -- wait a bit before running again
            end tell
        end repeat
    end tell