首页
/ 【亲测免费】 Chrome.ahk 项目使用教程

【亲测免费】 Chrome.ahk 项目使用教程

2026-01-16 09:20:42作者:羿妍玫Ivan

1. 项目的目录结构及介绍

Chrome.ahk/
├── Examples/
│   ├── example1.ahk
│   ├── example2.ahk
│   └── ...
├── lib/
│   ├── lib1.ahk
│   ├── lib2.ahk
│   └── ...
├── .gitignore
├── .gitmodules
├── Chrome.ahk
├── LICENSE
└── README.md
  • Examples/: 包含多个示例脚本,展示了如何使用 Chrome.ahk 库来自动化 Google Chrome。
  • lib/: 包含项目依赖的库文件。
  • .gitignore: Git 忽略文件,指定哪些文件或目录不需要被 Git 跟踪。
  • .gitmodules: Git 子模块配置文件。
  • Chrome.ahk: 项目的主文件,包含了自动化 Google Chrome 的核心功能。
  • LICENSE: 项目的开源许可证,本项目使用 MIT 许可证。
  • README.md: 项目的说明文档,介绍了项目的基本信息和使用方法。

2. 项目的启动文件介绍

项目的启动文件是 Chrome.ahk。这个文件包含了自动化 Google Chrome 的核心功能。通过这个文件,你可以创建一个 Chrome 类的实例,并使用该实例来控制 Chrome 浏览器。

主要功能

  • 创建 Chrome 实例: 通过 new Chrome() 创建一个 Chrome 实例,可以指定用户配置文件路径、初始 URL、Chrome 可执行文件路径和调试端口。
  • 连接到页面: 使用 GetPage() 方法连接到新打开的 Chrome 页面。
  • 调用协议端点: 使用 Call() 方法调用 Chrome DevTools Protocol 的端点。
  • 执行 JavaScript: 使用 Evaluate() 方法在页面中执行 JavaScript 代码。

示例代码

#Include Chrome.ahk

; 创建一个 Chrome 实例,使用 ChromeProfile 文件夹存储用户配置
FileCreateDir, ChromeProfile
ChromeInst := new Chrome("ChromeProfile")

; 连接到新打开的标签页并导航到另一个网站
PageInst := ChromeInst.GetPage()
PageInst.Call("Page.navigate", {"url": "https://autohotkey.com/"})
PageInst.WaitForLoad()

; 执行一些 JavaScript
PageInst.Evaluate("alert('Hello World!');")

; 关闭浏览器
PageInst.Call("Browser.close")
PageInst.Disconnect()
ExitApp
return

3. 项目的配置文件介绍

项目中没有专门的配置文件,但可以通过以下方式进行配置:

1. 用户配置文件路径

在创建 Chrome 实例时,可以通过 ProfilePath 参数指定用户配置文件的路径。如果不指定,Chrome 将使用默认的用户配置文件。

ChromeInst := new Chrome("ChromeProfile")

2. 初始 URL

在创建 Chrome 实例时,可以通过 URLs 参数指定初始打开的页面。如果不指定,Chrome 将打开 about:blank

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

3. Chrome 可执行文件路径

在创建 Chrome 实例时,可以通过 ChromePath 参数指定 Chrome 可执行文件的路径。如果不指定,Chrome 将使用默认的安装路径。

ChromeInst := new Chrome("ChromeProfile", "", "C:\Program Files\Google\Chrome\Application\chrome.exe")

4. 调试端口

在创建 Chrome 实例时,可以通过 DebugPort 参数指定调试端口。如果不指定,默认使用端口 9222

ChromeInst := new Chrome("ChromeProfile", "", "", 9222)

通过这些配置,你可以灵活地控制 Chrome 的启动和行为。

登录后查看全文
热门项目推荐
相关项目推荐