You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写脚本模拟点击Chrome历史记录页面的第一条地址链接

可行解决方案

方案1:直接读取Chrome本地历史数据库(最稳定,无权限限制)

Chrome的历史记录默认存储在本地SQLite数据库文件中,无需打开浏览器页面即可直接读取,完全避开页面操作的权限限制。

  • 数据库默认路径:~/Library/Application Support/Google/Chrome/Default/History
  • 操作逻辑:先复制数据库文件到临时目录(规避Chrome运行时对原文件的锁定),再执行SQL查询拿到最近访问的第一条URL,直接调用Chrome打开即可。
  • 完整AppleScript示例:
set originHistoryPath to POSIX path of (path to library folder from user domain) & "Application Support/Google/Chrome/Default/History"
set tmpHistoryPath to "/tmp/chrome_history_temp"
-- 复制历史文件到临时目录规避锁定
do shell script "cp " & quoted form of originHistoryPath & " " & quoted form of tmpHistoryPath
-- 查询最近访问的第一条URL
set firstHistoryUrl to do shell script "sqlite3 " & quoted form of tmpHistoryPath & " \"SELECT url FROM urls ORDER BY last_visit_time DESC LIMIT 1;\""
-- 调用Chrome打开链接
tell application "Google Chrome"
    open location firstHistoryUrl
end tell

方案2:修复历史页面操作失效问题

你之前操作失败大概率不是权限限制,而是Chrome的chrome://history页面所有元素都封装在Shadow DOM中,直接通过普通选择器无法定位元素,同时需要先配置系统自动化权限:

  • 前置配置:打开「系统设置→隐私与安全性→自动化」,找到你运行脚本的工具(脚本编辑器/终端/第三方脚本工具等),勾选Google Chrome的访问权限。
  • 调整后的页面操作AppleScript示例:
tell application "Google Chrome"
    activate
    set historyTab to make new tab with properties {URL:"chrome://history"}
    delay 1.5 -- 等待页面完全加载,可根据设备性能调整时长
    tell active tab of front window
        -- 逐层穿透Shadow DOM定位第一条历史链接并点击
        execute javascript "document.querySelector('history-app').shadowRoot.querySelector('history-list').shadowRoot.querySelector('history-item').shadowRoot.querySelector('#url').click();"
    end tell
end tell

内容的提问来源于stack exchange,提问作者Gin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 21:57:04