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));
};
Code: Select all
Run https://autohotkey.com
WinWaitActive (o_o)
MsgBox The page is ready.
"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.]