在继续使用 AutoHotkey 之前,如何等待 Google Chrome 加载网页? [英] How do I wait for Google Chrome to load a webpage before continuing in AutoHotkey?

查看:65
本文介绍了在继续使用 AutoHotkey 之前,如何等待 Google Chrome 加载网页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 AutoHotkey 脚本,该脚本在 Google Chrome 中执行重复性工作.每次我点击脚本中的链接时,我都必须让我的脚本在页面加载时休眠.

我想知道在 AHK 中是否有一种方法可以让我告诉脚本等待浏览器完成页面加载而不是休眠一段时间.这可能吗?

解决方案

虽然 Karthik 的回答比 sleep 命令更好,并且它可以在许多网站上运行良好,但我发现存在一些问题,尤其是在您经常使用它的情况下.

我自己需要一个可靠的解决方案,我相信我终于找到了.转到 Chrome 网上应用店并添加名为 Control Freak 的 Google Chrome 扩展程序.这让您可以轻松地设置可以在单个页面、整个域或任何站点/页面上触发的代码块.

Control Freak Chrome 扩展程序

加载完成后,单击扩展程序现在拥有的齿轮按钮.在您需要进行页面加载检测的适当上下文/空间中,单击弹出窗口顶部的 .即 All

现在您可以单击库"选项卡.向下滚动到 jQuery.您可以从您喜欢的列表中选择任何版本,但我选择了最新版本.请注意,CDN 的 url 添加在底部.现在,在您单击底部的 Save 按钮后,您将始终可以在选择的任何上下文中使用 jQuery.

切换回 JavaScript 选项卡并添加此代码,然后修改为您喜欢的方式:

jQuery(function() {prompt("诊断:页面状态", "已加载");});

单击保存"后,此代码将在 jQuery Ready 事件上执行.现在您有一种可靠的方法来检测页面加载,AHK 可以使用它!

有了这个,你可以让 AutoHotkey 等待一个标题以The page at"开头的窗口,这无疑是我们的提示,预填充了我们的单词loaded".你可以告诉 AHK 发送一个 Control+c 来复制它,然后检查它的值,或者只是假设既然你看到了它,那么你可能得到了你所期望的.下面的 AHK 代码可以等待提示弹出窗口(顺便说一句,这可以很容易地完成).

剪贴板=WinWait,页面位于WinWaitActive发送 ^c剪辑等待关闭;这里的其余部分完全是可选的工具提示代码,但可能很有用.工具提示,提供的诊断数据为:%clipboard%,0,0睡眠,1000工具提示

那么让我们优化一下,放到一个可以反复调用的函数中:

功能定义:

waitForPageLoad(){WinWait,页面位于WinWaitActive关闭}

函数调用:

waitForPageLoad()

其他想法..

现在我注意到,即使在页面并未真正更改或重新加载时,此代码也会触发,例如页面 URL 可能更改时,但他们专门对其进行编码以不离开页面,通常您在那里看到新内容.这对我来说是完美的,但可以通过设置某种变量并检查它是否已经设置(在你在 Control Freak 中添加的 JavaScript 代码中)来过滤掉.

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?

解决方案

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

这篇关于在继续使用 AutoHotkey 之前,如何等待 Google Chrome 加载网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆