为什么不'做的javascript“在AppleScript的调用执行时相同的code输入到浏览器呢? [英] why doesn't 'do javascript' call in Applescript execute when the same code typed into browser does?
问题描述
我试图找出为什么我的AppleScript什么都不做的时候相同的JavaScript code键入到Safari浏览器地址栏中的作品。
I'm trying to figure out why my Applescript does nothing when the same javascript code typed into a Safari location bar works.
去一个搜索结果页面,如: http://www.google.com/搜索q =测试。对于正确的行为,输入这个到地址栏并回车:
Go to a search results page, such as: http://www.google.com/search?q=test . For the correct behavior, type this into the location bar and hit enter:
javascript: document.getElementsByClassName('vspib')[0].click();
您会看到它选择的第一个搜索结果的放大镜。
You'll see that it selects the magnifier for the first search result.
这是我想要通过JavaScript发生什么。所以,我输入了以下内容:
This is what I want to make happen via javascript. So I typed up the following:
tell application "Safari"
activate
do JavaScript "document.getElementsByClassName('vspib')[0].click();" in document 1
end tell
但是,它什么都不做。任何想法?
However, it does nothing. Any ideas?
推荐答案
的问题是,做的JavaScript
有正确解决在Safari浏览器窗口的一个标签。
The problem is that do JavaScript
has to correctly address a tab in a Safari window.
下面的脚本作品对我来说,如果搜索结果页面在最前面的Safari窗口当前标签:
The following script works for me, if the search results page is the current tab in the frontmost Safari window:
tell application "Safari"
activate
set theScript to "document.getElementsByClassName('vspib')[0].click();"
do JavaScript theScript in current tab of first window
end tell
这篇关于为什么不'做的javascript“在AppleScript的调用执行时相同的code输入到浏览器呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!