【浏览器操控示例】示例演示的是,用网页按钮来控制AHK
以下示例需要Chrome.ahk库支持才能工作
; By 空 #NoEnv SetBatchLines, -1 #Include <Chrome> TestPages := 3 ; --- Define a data URL for the test page --- ; https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs DataURL = ( Comments <!DOCTYPE html> <html> <head> ; Use {} to allow text insertion using Format() later <title>Test Page {}</title> </head> <body> <button class="someclass">Click Me!</button> </body> </html> ) ; --- Define some JavaScript to be injected into each page --- JS = ( Comments ; Using a self-invoking anonymous function for scope management ; https://blog.mgechev.com/2012/08/29/self-invoking-functions-in-javascript-or-immediately-invoked-function-expression/ (function(){ var clickCount = 0; ; Whenever the button tag with class someclass is clicked document.querySelector("button.someclass").onclick = function() { clickCount++; ; Prefix the message with AHK: so it can be ; filtered out in the AHK-based callback function console.log("AHK:" + clickCount); }; })(); ) ; --- Create a new Chrome instance --- ; Define an array of pages to open DataURLs := [] Loop, %TestPages% { File := Format("{}\{}.html", A_Temp, A_Index) FileDelete, % File FileAppend, % Format(DataURL, A_Index), % File DataURLs.Push(File) } ; Open Chrome with those pages ChromeInst := new Chrome("User_Data", DataURLs) ; --- Connect to the pages --- PageInstances := [] Loop, %TestPages% { ; 不加延时容易报错 Sleep, 2000 ; Bind the page number to the function for extra information in the callback BoundCallback := Func("Callback").Bind(A_Index) ; Get an instance of the page, passing in the callback function if !(PageInst := ChromeInst.GetPageByTitle(A_Index, "contains",, BoundCallback)) { MsgBox, Could not retrieve page %A_Index%! ChromeInst.Kill() ExitApp } PageInstances.Push(PageInst) ; Enable console events and inject the JS payload PageInst.WaitForLoad() PageInst.Call("Console.enable") PageInst.Evaluate(JS) } MsgBox, Running... Click OK to exit ; --- Close the Chrome instance --- try PageInstances[1].Call("Browser.close") ; Fails when running headless catch ChromeInst.Kill() for Index, PageInst in PageInstances PageInst.Disconnect() ExitApp return Callback(PageNum, Event) { ; Filter for console messages starting with "AHK:" if (Event.Method == "Console.messageAdded" && InStr(Event.params.message.text, "AHK:") == 1) { ; Strip out the leading AHK: Text := SubStr(Event.params.message.text, 5) ToolTip, Clicked %Text% times on page %PageNum% } }
声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。
评论(0)