修复获取Brave浏览器中YouTube视频信息的AppleScript语法错误
修复Brave浏览器YouTube视频标题获取的AppleScript语法错误
你的代码出现Expected “,” but found identifier.错误,核心原因是Brave的AppleScript接口不支持do JavaScript ... in t的调用格式,需要通过tell块针对目标标签页执行JavaScript命令。
修复后的完整代码
tell application "Brave Browser" if (count of windows) = 0 then return "No windows open in Brave Browser" end if set videoTitle to "" repeat with w in every window repeat with t in every tab of w if URL of t contains "youtube.com/watch" then tell t set videoTitle to do JavaScript "document.title" end tell exit repeat end if end repeat if videoTitle is not "" then exit repeat end repeat end tell return videoTitle
额外说明
- 权限配置:必须先在Brave中启用AppleScript控制:打开「设置」→ 搜索「AppleScript」→ 勾选「允许AppleScript控制浏览器」。
- 纯视频标题优化:如果不需要标题里的“YouTube”后缀,可以把JavaScript代码替换为:
document.querySelector('h1.title yt-formatted-string').textContent
内容的提问来源于stack exchange,提问作者Gokul Kannan
相关产品推荐
相关产品推荐

