Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I wait for Google Chrome to load a webpage before continuing in AutoHotkey?

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?

like image 777
Questionmark Avatar asked May 16 '14 14:05

Questionmark


3 Answers

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).

like image 163
Jason Cramer Avatar answered Oct 03 '22 16:10

Jason Cramer


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")
like image 38
Karthik S Avatar answered Oct 03 '22 17:10

Karthik S


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.

like image 5
Robert Ilbrink Avatar answered Oct 03 '22 15:10

Robert Ilbrink