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

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

问题描述

我正在研究在Google Chrome中执行重复性工作的AutoHotkey脚本。每次点击脚本中的链接时,我都必须告诉我的脚本在页面加载时进入睡眠状态。



我想知道AHK中有没有办法我告诉脚本等待浏览器完成加载页面,而不是休息一段时间。这可能吗?

解决方案

虽然 Karthik的回答比睡眠命令要好,并且它对许多网站来说功能相当好,我发现有一些问题可能会增加,特别是如果你经常使用它。



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

Control Freak Chrome扩展程序



加载完成后,请点击您现在拥有的用于扩展程序的齿轮按钮。点击弹出窗口顶部的相应上下文/空间,您需要进行页面加载检测。即全部



现在您可以点击Libs选项卡。向下滚动到jQuery。你可以从你喜欢的列表中选择任何版本,但是我选择了最新版本。请注意,CDN的网址添加在底部。现在,当您单击底部的 Save 按钮时,您将始终在所选的上下文中提供jQuery。



切换回JavaScript选项卡并添加以下代码并修改为您喜欢的方式:

  jQuery(function(){
prompt(Diagnostic:Page Status,loaded);
});

一旦你点击保存,这段代码就会执行jQuery Ready事件。现在你有一个可靠的方法来检测页面加载,AHK可以使用!



有了这个,你可以让AutoHotkey等待一个标题开始的窗口......的页面,无疑将成为我们的提示,预先填入我们的装载单词。你可以告诉AHK发送一个Control + c来复制它,然后检查它的值,或者只是假设你看到它,那么你可能会有你的预期。下面的AHK代码可以等待提示弹出窗口(这可以轻松地通过btw提示)。

  clipboard = 
WinWait,
的页面WinWaitActive
发送^ c
ClipWait
WinClose
;这里的其余部分完全是可选的工具提示代码,但可能很有用。
tooltip提供的诊断数据为:%clipboard%,0,0
sleep,1000
tooltip

所以让我们对它进行优化并将其放入一个可以反复调用的函数中:

函数定义:

  waitForPageLoad()
{
WinWait,
的页面WinWaitActive
WinClose
}

函数调用:

  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 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天全站免登陆