【浏览器操控示例】此示例演示了在当前网页注入JS代码,执行自己需要的代码功能。

AHK操控浏览器入门教程

以下示例需要Chrome.ahk库支持才能工作

; By 空
#NoEnv
SetBatchLines, -1

#Include <Chrome>


; --- Create a new Chrome instance ---

ChromeInst := new Chrome("User_Data", "https://autohotkey.com/")


; --- Connect to the page ---

if !(PageInst := ChromeInst.GetPage())
{
  MsgBox, Could not retrieve page!
  ChromeInst.Kill()
}
else
{
  ; --- Perform JavaScript injection ---
  
  Loop
  {
    InputBox, JS,,
    ( LTrim
    Enter some JavaScript to be run on the page, or leave blank to exit. For example:
    
    alert('hi');
    window.location = "https://ahk66.com/";
    ), , , , , , Locale, , alert('hi');
    
    if (JS == "" || ErrorLevel)
      break
    
    try
      Result := PageInst.Evaluate(JS)
    catch e
    {
      MsgBox, % "Exception encountered in " e.What ":`n`n"
      . e.Message "`n`n"
      . "Specifically:`n`n"
      . Chrome.Jxon_Dump(Chrome.Jxon_Load(e.Extra), "`t")
      
      continue
    }
    
    MsgBox, % "Result:`n" Chrome.Jxon_Dump(Result, "`t")
  }
  
  
  ; --- Close the Chrome instance ---
  
  try
    PageInst.Call("Browser.close") ; Fails when running headless
  catch
    ChromeInst.Kill()
  PageInst.Disconnect()
}

ExitApp
return

 

声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。