AppleScript 可以访问浏览器选项卡并在其中执行 javascript 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5135609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can AppleScript access browser tabs and execute javascript in them?
提问by Allan
I'm curious if AppleScript can access each specific tab in a browser and execute some javascript in them.
我很好奇 AppleScript 是否可以访问浏览器中的每个特定选项卡并在其中执行一些 javascript。
Anyone have ideas?
有人有想法吗?
回答by Fábio Perez
For Google Chrome, use executefrom AppleScript Chromium Suite:
对于Google Chrome,execute从 AppleScript Chromium Suite 使用:
execute?v : Execute a piece of javascript.
executespecifier: The tab to execute the command in.
javascripttext: The javascript code to execute.
execute?v : 执行一段javascript。
执行说明符:在其中执行命令的选项卡。
javascript text: 要执行的 javascript 代码。
Example:
例子:
tell application "Google Chrome"
execute front window's active tab javascript "alert('example');"
end tell
Can be done in Safarieither:
可以在Safari中完成:
tell application "Safari" to do JavaScript "alert('example')" in document 1
For other browsers, check the functions at File -> Open Dictionaryon AppleScript editor.
对于其他浏览器,请检查File -> Open DictionaryAppleScript 编辑器上的功能。
回答by Evan Lovely
Recently Google Chrome disabled the ability to execute JavaScript via AppleScript. I'm having trouble finding a reference link, but in Chrome Canary v59 it doesn't work and gives a message saying it's disabled, and I'm seeing it still work in Chrome v57 so it's got to be very new.
最近 Google Chrome 禁用了通过 AppleScript 执行 JavaScript 的能力。我找不到参考链接,但在 Chrome Canary v59 中它不起作用并给出一条消息说它已被禁用,我看到它在 Chrome v57 中仍然有效,所以它必须是非常新的。
回答by nisetama
The function below runs JavaScript in each tab of the frontmost window in Chrome and concatenates the output. To run JavaScript in in all windows, replace window 1with windows.
下面的函数在 Chrome 最前面窗口的每个选项卡中运行 JavaScript 并连接输出。要在所有窗口中运行 JavaScript,请替换window 1为windows.
xjss(){ osascript -e'on run {a}
set o to ""
tell app "Google Chrome" to repeat with t in (get tabs of window 1)
tell t to set o to o & (execute JavaScript a) & linefeed
end
end' -- ""; }
This runs JavaScript in only the frontmost tab:
这仅在最前面的选项卡中运行 JavaScript:
xjs(){ osascript -e'on run {a}
tell app "Google Chrome" to tell active tab of window 1 to execute JavaScript a
end' -- ""; }
Here are similar functions for Safari:
以下是 Safari 的类似功能:
sjss(){ osascript -e'on run {a}
set o to ""
tell app "Safari" to repeat with t in (get tabs of window 1)
tell t to set o to o & (do JavaScript a) & linefeed
end
end' -- ""; }
sjs(){ osascript -e'on run {a}
tell app "Safari" to tell document 1 to do JavaScript a
end' -- ""; }
Since some version of Chrome released in 2018, an error like this is shown by default when running an execute JavaScriptcommand:
由于某些版本的 Chrome 于 2018 年发布,因此在运行execute JavaScript命令时默认会显示这样的错误:
78:98: execution error: Google Chrome got an error: Executing JavaScript through AppleScript is turned off. To turn it on, from the menu bar, go to View > Developer > Allow JavaScript from Apple Events. For more information: https://support.google.com/chrome/?p=applescript(12)
78:98:执行错误:Google Chrome 出现错误:通过 AppleScript 执行 JavaScript 已关闭。要打开它,请从菜单栏中转到“查看”>“开发人员”>“允许来自 Apple 事件的 JavaScript”。更多信息:https: //support.google.com/chrome/?p=applescript(12)
Safari has a similar preference in "Develop > Allow JavaScript from Apple Events" which was turned off by default in macOS version 10.11.5 (released in 2016-05-16).
Safari 在“开发 > 允许来自 Apple 事件的 JavaScript”中有类似的偏好,在 macOS 版本 10.11.5(2016 年 5 月 16 日发布)中默认关闭。

