How to make AutoHotkey wait for a page to load?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
harmonysystems
Posts: 3
Joined: 26 Apr 2024, 17:36

How to make AutoHotkey wait for a page to load?

26 Apr 2024, 18:01

Over the years, many questions have been posted here asking how to make AutoHotkey wait for a page to load. The answers provided so far seem to be complicated and difficult to put into use. So here's a simple, easy solution. The basic idea:

1. Install a userscript manager browser extension.

2. Create a userscript that will signal when a webpage has completely loaded by temporarily modifying the title of the browser tab.

3. Use WinWaitActive in AHK to detect the "PageReady" signal set by the userscript.

The most popular userscript manager is called TamperMonkey, and that's what was used to develop this solution. You can also use another browser extension called ViolentMonkey. Both of these extensions can be installed on Chrome, Edge, Firefox and other browsers.

After installing one of the userscript manager browser extensions, click on the extension and select the option to "Create a new script". A userscript editor will be displayed along with a default userscript template. Replace the default userscript with the following:

Code: Select all

// ==UserScript==
// @name         PageWaitReady
// @version      2024-04-26
// @description  Wait for page to load
// @match        https://*/*
// ==/UserScript==

let PageReady = "(o_o) ";
window.addEventListener("load", () => {
    Sleep(1000).then(() => SetTitle());
    Sleep(2000).then(() => ResetTitle());
});
function SetTitle() {
    ResetTitle();
    document.title = PageReady + document.title;
};
function ResetTitle() {
    if (document.title.substring(0, PageReady.length) === PageReady) {
        document.title = document.title.substring(PageReady.length);
    };
};
function Sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
};
Click on Save and then Close, and the PageWaitReady userscript will be active. You can verify that the userscript is working by running the following script in AHK:

Code: Select all

Run https://autohotkey.com
WinWaitActive (o_o)
MsgBox The page is ready.
The PageWaitReady userscript relies on the "Window load" event to determine when to set the PageReady signal. The documentation for this event states:

"The load event is fired when the whole page has loaded, including all dependent resources such as stylesheets, scripts, iframes, and images."

That sounds like exactly what we want, and in fact, for most websites, it works very well. But there are exceptions. Some webpages will still be loading elements after the Window load event has fired.

In that case, you can modify the value of the first Sleep command in the userscript. The delay is set to 1 second (1000 milliseconds), but you can adjust the value higher if your website still hasn't completely loaded when the signal gets sent. The value for the second Sleep command needs to be set to at least 2 seconds to allow time for your AHK script to detect the momentary change in the title.

By default, the userscript will run on all webpages, and this is specified by "// @match https://*/*" in the metadata block at the top of the userscript. You can modify the @match command to limit the operation of the userscript to just one URL, and you can also have multiple @match commands in the metadata block.


[Mod edit: Changed “microseconds” to “milliseconds” at the poster’s request.]
User avatar
boiler
Posts: 17123
Joined: 21 Dec 2014, 02:44

Re: How to make AutoHotkey wait for a page to load?

26 Apr 2024, 21:23

Note that your short AHK script is v1 but you posted in the v2 section. I’ll move the thread if you’re looking to use v1.
harmonysystems
Posts: 3
Joined: 26 Apr 2024, 17:36

Re: How to make AutoHotkey wait for a page to load?

27 Apr 2024, 04:34

The PageWaitReady test script for both AHK versions:

Code: Select all

#Requires AutoHotkey v1
Run https://autohotkey.com
WinWaitActive (o_o)
MsgBox The page is ready.

Code: Select all

#Requires AutoHotkey v2
Run("https://autohotkey.com")
WinWaitActive("(o_o)")
MsgBox("The page is ready.")
harmonysystems
Posts: 3
Joined: 26 Apr 2024, 17:36

Error correction

28 Apr 2024, 12:26

@boiler, could you please correct an error in my original post? "microseconds" should be "milliseconds". Thanks

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: No registered users and 30 guests