Wait for Chrome to finish loading Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
roysubs
Posts: 426
Joined: 29 Sep 2018, 16:37

Wait for Chrome to finish loading

13 Apr 2020, 03:52

Some hotkeys in my basic setup. As below, I want this because I use DuckDuckGo for 95+% of searches for much better anonymity, but there are a few times that I need/want to use Google / Google Maps so I want a quick way to get into them.

The Ctrl+Win+a / Ctrl+Win+p are so that I get a fast search into on programming (save me typing in "PowerShell" or "Autohotkey" every time I want to search!).

A problem with the below is that in the browser, I don't know how to detect when a task has finished (by which I mean "has the page fully loaded?") which is why I do the ^l twice.

Is there a way to do something like that? When interacting with Chrome, this would be massively useful so that instead of putting random length "Sleep" statements in, we could have a structured means of waiting until I know the thing has finished loaded before I continue - mostly Chrome responds really fast, fraction of a second usually, but sometimes it can take 3-8 seconds of seemingly doing nothing before it loads a page - obviously, if my approach here is all wrong, I'd appreciate pointer to use better functions that achieve the goal more efficiently! :).

Code: Select all

#IfWinActive ahk_exe chrome.exe
^g::   ; Quick search for Google, use this as I set default search engine to DuckDuckGo for anonymity, but sometimes need/want google.com
    WinActivate ahk_exe chrome.exe
    WinWaitActive ahk_exe chrome.exe   ; Activates the last found and open a new tab
    Send ^l       ; Ctrl-l to activate address bar    ; Send ^t   would do a 'Ctrl-t' to open a new tab, or     Run % "chrome.exe --new-tab" url
    Sleep 50
    Send ^l       ; Ctrl-l to activate address bar (do it a second time just in case)
    Sleep 50
    SendInput, www{.}google{.}com   ; when using more complex urls, replace characters by their SendInput equivalent: url := RegExReplace(url, "#", "{#}")
    Sleep 50
    SendInput, {Enter}    ; Default focus will jump to the Google search entry field so we don't need to do any more than this
    Sleep 50
    WinActivate ahk_exe chrome.exe
    WinWaitActive ahk_exe chrome.exe   ; Activates the last found
return
^m::   ; Quick search in Google Maps (the default DuckDuckGo maps are substandard so I need a quick way into them)
    Sleep 200
    WinActivate ahk_exe chrome.exe
    WinWaitActive ahk_exe chrome.exe   ; Activates the last found and open a new tab
    Send ^l       ; Ctrl-l to activate address bar
    Sleep 100
    ; Send ^t       ; Ctrl-t to open a new tab
    ; Sleep 100
    Send ^l       ; Ctrl-l to activate address bar (do it a second time just in case)
    Sleep 50
    SendInput, maps{.}google{.}com   ; Default focus jumps to Maps search, so don't need to do any more than this.
    Sleep 100
    SendInput, {Enter}
    Sleep 100
return
^#a::   ; AutoHotkey
    WinActivate ahk_exe chrome.exe
    WinWaitActive ahk_exe chrome.exe
    Sleep 50
    Send ^l
    Sleep 50
    Send ^a   ; select all to overwrite
    SendInput, AutoHotkey{Space}
    Sleep 50
return
^#p::   ; PowerShell
    WinActivate ahk_exe chrome.exe
    WinWaitActive ahk_exe chrome.exe
    Sleep 50
    Send ^l
    Sleep 50
    Send ^a   ; select all to overwrite
    SendInput, PowerShell{Space}
    Sleep 50
return
#IfWinActive

; Below might be useful as global searches outside of #IfWinActive.
; Note: do not make 4x char in a row "aaaa", as possibility of activating by accident by leaning on a key. Below are hard to do by accident.
; aaas "Autohotkey Search", ppps "PowerShell Search", etc

:x*:aaas::   ; Quick search for AutoHotkey, opens Chrome, then fills "AutoHotkey " ready to search for something
    WinActivate ahk_exe chrome.exe
    WinWaitActive ahk_exe chrome.exe
    Sleep 50
    Send AutoHotkey{Space}
return
:x*:ppps::   ; Quick search for AutoHotkey, opens Chrome, then fills "AutoHotkey " ready to search for something
    WinActivate ahk_exe chrome.exe
    WinWaitActive ahk_exe chrome.exe
    Sleep 50
    Send AutoHotkey{Space}
return
User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Wait for Chrome to finish loading  Topic is solved

13 Apr 2020, 04:14

Hello,

Normally there is a cycle icon next to the address bar.
(It's usually the button which you can click to reload the page)

An idea is to make a screenshot of this button and to use AHK's ImageSearch.
https://www.autohotkey.com/docs/commands/ImageSearch.htm
Probably you will have to loop until ErrorLevel is 0.
I don't thing you need an if statement for error level 0.
(It would only need a break, if needed)

Something like:
(I can't do tests right now)

Code: Select all

while ErrorLevel = 1
{
ImageSearch, ix, iy, 0, 0, 300, 300, %A_ScriptDir%\chromeLoaded.png
Sleep, 5
}
Once the image has been found (because the page is loaded), it should leave the loop and continue.
Important is that end coordinates of the image are not too low.
300, 300 was just a guess, the end coordinates might be different, you can increase those values.

Cheers!
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
roysubs
Posts: 426
Joined: 29 Sep 2018, 16:37

Re: Wait for Chrome to finish loading

13 Apr 2020, 05:38

wow, fascinating. Not used this functionality really (played with it but had dubious results). I think this is ingenious though, if I can get a function that can check that, all the 'sleep' statements can be binned and I'll use this for all Chrome automation. Great stuff, thanks! :)
strijder
Posts: 1
Joined: 25 Oct 2023, 13:09

Re: Wait for Chrome to finish loading

25 Oct 2023, 13:16

I know this is an old thread, but I am posting what works for me (both for my future reference and for other users who might not have been able to find a satisfactory answer).

Pre-requisites:
* A .pngfile of the Chrome loading icon (next to the Home button) saved as "chromeLoaded.png" in the same folder where your script is saved

Code:

Code: Select all

;Wait until page loads
Sleep, 100
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %A_ScriptDir%\chromeLoaded.png
if (ErrorLevel = 2)
    MsgBox Error - could not search for image
else if (ErrorLevel = 1)
    Loop {
Sleep, 100
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %A_ScriptDir%\chromeLoaded.png
if (ErrorLevel = 0)
break
}
[Mod edit: [code][/code] tags added. Please use them yourself when posting code!]

This usually works flawlessly for me. Sometimes I might have to save a new icon image file (tweaks to Chrome's layout? not sure why) but it works!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], roeleboele and 386 guests