在 Mac 上,如何在 Chrome 或 Firefox 中重新加载文档?

8

我该如何告诉Chrome或Firefox重新加载顶部窗口中的文档?这是我在Safari中使用的:

osascript -e '
    tell application "Safari"
      activate
      do JavaScript "history.go(0)" in document 1
    end tell
'
5个回答

40

这是Chrome的代码:

tell application "Google Chrome"
    tell the active tab of its first window
        reload
    end tell
end tell

更简洁地说:

tell application "Google Chrome" to tell the active tab of its first window
    reload
end tell

5
个人认为这个脚本值得更多的赞,因为它在重新加载之前不需要让浏览器聚焦 - 这很关键。 - bitwit
1
你也可以使用“第一个选项卡”代替“活动选项卡”。 - blockloop

11

我不认为Firefox或Chrome有特殊的AppleScript支持,但您可以发送按键(Cmd-R)来刷新页面:

tell application "Firefox"
    activate
    tell application "System Events" to keystroke "r" using command down
end tell

Firefox应该可以,但是显然在当前的3.5.x版本中出现了故障。 - Ned Deily
太棒了!这适用于所有三种浏览器,使用“Firefox”、“Google Chrome”和“Safari”作为应用程序字符串。 - Mark Harrison

5
这里有另一种在Safari中实现的方法,不需要使用JavaScript:
tell application "Safari"
    tell its first document
        set its URL to (get its URL)
    end tell
end tell

1

Automator

  • 打开 Automator 并选择新建文件
  • 选择服务

A screengrab showing the document type dialog in Automator

  • 服务接收设置为无输入
  • 从操作列表中选择运行AppleScript操作。
  • 将以下代码粘贴到脚本中:

 

tell application "Google Chrome" to tell the active tab of its first window
reload
end tell

A screengrab showing the automator window actions

  • 保存该服务,例如使用名称Chrome Refresh

系统偏好设置

  • 打开系统偏好设置 > 键盘
  • 快捷键选项卡中选择服务
  • 分配新的快捷键

A screengrab showing System Preferences keyboard shortcut tab


0
上述回答都很好,但使用它们会导致 DevTools 在最后一个焦点/活动的选项卡/窗口中刷新到新选项卡上。我不想让 DevTools 在新选项卡中刷新,我只想让第一个选项卡刷新,而不管最后一个焦点/活动的选项卡/窗口是哪个,这对我来说效果很好。留下来供其他有相同需求的人查找参考。
tell application "Google Chrome"
  activate
  tell application "System Events"
    tell process "Google Chrome"
      keystroke "r" using {command down, shift down}
    end tell
  end tell
end tell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接