为什么当在浏览器中输入相同的代码时,Applescript 中的“do javascript"调用不执行? [英] why doesn't 'do javascript' call in Applescript execute when the same code typed into browser does?

查看:23
本文介绍了为什么当在浏览器中输入相同的代码时,Applescript 中的“do javascript"调用不执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚为什么我的 Applescript 在输入到 Safari 位置栏中的相同 javascript 代码有效时什么也不做.

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/search?q=test .要获得正确的行为,请在地址栏中输入以下内容并按 Enter:

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?

推荐答案

问题在于 do 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

这篇关于为什么当在浏览器中输入相同的代码时,Applescript 中的“do javascript"调用不执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆