17
votes

I am working on a AutoHotkey script that does a repetitive job in Google Chrome. Every time that I click a link from the script, I have to tell my script to sleep while the page loads.

I am wondering if there is a way in AHK for me to tell the script to wait for the browser to finish loading the page rather than sleeping for a set amount of time. Is this possible?

6
The only way is probably to check if the page loading icon is there. With a pixel search you can check the color and if the color is not the color of the loading icon then it is loaded. autohotkey.com/board/topic/…Dávid Szabó
Yes you can do it other way, but it needs an userscript as you seen it, if you would like to determine if the page is loaded, then the developers should make a code to for every possible browser, which isn't the best, you can try the userscript too, that's why i sent you the link, next time use google.Dávid Szabó

6 Answers

10
votes

While Karthik's answer is better than a sleep command, and it can function fairly well for many sites, I found that there are some problems which can creep up especially if you use this quite often.

I needed a reliable solution myself, which I believe I've finally found. Go to the Chrome Web Store and add the Google Chrome extension called Control Freak. This easily allows you to set up code chunks which can be triggered on a single page, a whole domain, or any site/page.

Control Freak Chrome Extension

Once you've got it loaded, click the cog wheel button you now have for the extension. Click at the top of the pop-up window in the appropriate context/space you need to have the page load detection. i.e. All

Now you can click on the Libs tab. Scroll down to jQuery. You can pick any version from the list you like, but I chose the latest. Notice that the url for the CDN is added at the bottom. Now after you click the Save button at the bottom you will always have jQuery available in whatever context was chosen.

Switch back to the JavaScript tab and add this code, and modify to how you like:

jQuery(function() {
  prompt("Diagnostic: Page Status", "loaded");
});

Once you click Save, this code will execute upon the jQuery Ready event. Now you have a reliable way to detect the page load, which AHK can use!

With this in place, you can have AutoHotkey wait for a window who's title starts by saying "The page at"...., which will undoubtedly be our prompt, pre-filled with our word "loaded". You can tell AHK to send a Control+c to copy that, then check its value, or just presume that since you saw it, then you likely have what you expected. The following AHK code can wait for the prompt pop-up (which could just as easily be done with an alert btw).

clipboard=
WinWait, The page at
WinWaitActive
Send ^c
ClipWait
WinClose
; The rest here is entirely optional tooltip code, but can be useful.
tooltip, The diagnostic data provided was: %clipboard%,0,0
sleep, 1000
tooltip

So let's optimize it and put it into a function you can call over and over again:

Function Definition:

waitForPageLoad()
{
  WinWait, The page at
  WinWaitActive
  WinClose
}

Function Call:

waitForPageLoad()

Other thoughts..

Now I've noticed this code to trigger even at times when the page isn't truly being changed or reloaded, such as when the page URL might change, but they specifically coded it to not leave the page, and typically you see new content there. That is perfect for my purposes, but this could be filtered out by setting some kind of variable and checking if it had already been set (within the JavaScript code you added in Control Freak).

7
votes

This is the code I use to check if the page has loaded or not. It checks the color at a particular pixel postion defined by x,y. If the pixel color matches the function exits from the loop back to the main script.

Function Definition :

waitForPageLoad(x, y, color)
{
  Loop
  {
    PixelGetColor, Loaded, %x%, %y%
    if Loaded = %color%
      break
  }
}

Function Call:

waitForPageLoad(399, 265, "0xE4C2A2")
5
votes

You can use the mouse cursor status like this:

Sleep, 200
while (A_Cursor = "AppStarting" or A_Cursor = "Wait") ; Wait for browser to be ready (page loaded)
    continue
Sleep, 200
while (A_Cursor = "AppStarting" or A_Cursor = "Wait") ; Wait for browser to be ready (page loaded)
    continue

I do this twice, just to make sure that a brief status change of the mouse cursor is not giving the wrong data back.

1
votes

I have modified Karthik S's function to add an optional seconds parameter. If specified, this will set a maximum wait time to prevent the function from getting caught in an infinite loop. I also changed the color comparison to RGB for compatibility with Window Spy. (Previously compared as BGR)

Argument Descriptions:

  • x, y: The X and Y coordinates of the pixel to wait for.
  • color: RGB hexadecimal color to wait for at specified coordinates. (ex: 0xBBCFE3)
  • seconds: How many seconds to wait before timing out and setting ErrorLevel to 1.

Function Definition:

; Wait for page to load by checking pixel color at specified x,y coordinates
WaitForPageLoadByColor(x, y, color, seconds:=-1)
{
   start := A_TickCount
   while ( (seconds = -1) || (A_TickCount-start <= seconds*1000) )
   {
      PixelGetColor, Loaded, %x%, %y%, RGB
      if Loaded = %color%
         return
   }
   ErrorLevel := 1 ; Page failed to load
}

Function Call:

waitForPageLoad(399, 265, "0xBBCFE3", 10)
1
votes

I've had some success looking for a control to be present on the target page. Depends on the contents of the page of course. I used WinSpy to figure out a suitable target.

Here's the code that worked for me:

loaded := false
check_count := 0
MAX_CHECKS := 20

while !loaded {
    Sleep, 100
    ControlGetPos, x, y, ,,Chrome Legacy Window
    if x == ""
        loaded := false
    else
        loaded := true

    check_count++
    if (check_count >= MAX_CHECKS) {
        Exit
    }
}
0
votes

The following script can be used to get the color at (x, y) on the loaded page

!q::
    x := 100
    y := 200
    msgbox, start
    mousemove, x, y
    PixelGetColor, pColor, x, y, RGB
    Msgbox, Pixel color %pcolor% at %x% %y%
return

The following script can be used to wait until the page gets loaded

!z::
    x := 100
    y := 200
    loop
         { 
         sleep, 500
         PixelGetColor, pColor, x, y, RGB
         If (pColor = "0x4285F4")
            {
            msgbox, found color: %pcolor%
            break
            }
         }
    ;The next script comes here
    send, {enter}
    ;...
return

Reference: https://www.autohotkey.com/boards/viewtopic.php?t=65554