Macos 通过Applescript获取Google Chrome中当前选项卡的来源

Macos 通过Applescript获取Google Chrome中当前选项卡的来源,macos,google-chrome,applescript,Macos,Google Chrome,Applescript,在Safari中这样做是儿戏,它有很好的Applescript支持。Google Chrome的AS支持刚刚到来,所以我对他们表示怀疑。我基本上是试图通过剪贴板抓取当前的HTML,这样我就可以获得信息。我们有一些漂亮的命令如下: tell application "Google Chrome" view source of active tab of window 1 save active tab of window 1 print active tab of window 1 rel

在Safari中这样做是儿戏,它有很好的Applescript支持。Google Chrome的AS支持刚刚到来,所以我对他们表示怀疑。我基本上是试图通过剪贴板抓取当前的HTML,这样我就可以获得信息。我们有一些漂亮的命令如下:

tell application "Google Chrome"
 view source of active tab of window 1
 save active tab of window 1
 print active tab of window 1
 reload active tab of window 1
 go back active tab of window 1
 go forward active tab of window 1
 copy selection of active tab of window 1
 paste selection active tab of window 1
end tell
但遗憾的是,您不能说“
将X设置为窗口1的活动选项卡的源”。有人对我有什么建议吗?我目前的想法是在Safari的后台加载我需要的代码(非常难看),或者尝试显示源代码并用UI脚本抓取它,但这也很难看。我还经常遇到脚本错误,使它无法工作


任何帮助都将不胜感激。

鉴于Chrome的支持“刚刚到来”,使用它肯定是“令人兴奋的”。在尝试字典中提供的一些命令时,看起来他们仍然有一些问题需要解决。除非Google在API中公开了一种更容易获取源代码的方法(和/或解决相关问题),否则您必须使用您在帖子中提到的替代方法之一

我很高兴得知Chrome现在支持AppleScript。不幸的是,到目前为止,它还很小,但我相信(我希望!)它会变得更好。由于无法直接获取源代码,我会选择以下黑客路线:

tell application "Google Chrome"
    view source of active tab of window 1 -- Or whichever tab you want
    delay 3
    repeat while loading of active tab of window 1
        delay 3
    end repeat
    select all of active tab of window 1 -- Must *always* be the active tab
    copy selection of active tab of window 1
    delete tab (active tab index of window 1) of window 1
end tell
delay 1
return the clipboard
是的,这有点骇人听闻,但考虑到脚本字典的当前状态,这是不可避免的。脚本应该很简单:打开源选项卡,等待加载,选择内容,复制,然后关闭选项卡。您可以使用
delay 3
s来查看最有效的方法。请注意,窗口1的第一个
活动选项卡是任意的,其余的显式引用源选项卡。而且,显然无法关闭Chrome脚本字典(oy-vey)中的选项卡,因此我不得不使用JavaScript。另外,最后一个
延迟1
应该是不必要的,但是如果它不在那里,我的测试有时会返回错误的内容,即使我粘贴它们时剪贴板内容是正确的。我认为这是因为有足够的文本,它花了相当长的时间来更新剪贴板

tell application "Google Chrome"
    set t to active tab index of window 1
    tell active tab of window 1 to view source
    set t to t + 1
    repeat while (loading of tab t of window 1)
    end repeat
    tell tab t of window 1 to select all
    tell tab t of window 1 to copy selection
    delete tab t of window 1
end tell
编辑1:我按照建议将
执行窗口1 javascript的活动选项卡“window.close()”
替换为
删除选项卡
行。不幸的是,
delete tab window 1的active tab
不起作用,所以您需要这种稍微复杂一些的构造

-- This script copies the HTML of a tab to a TextEdit document.
tell application "Chromium"
 tell tab 1 of window 1 to view source
 repeat while (loading of tab 2 of window 1)
 end repeat
 tell tab 2 of window 1 to select all
 tell tab 2 of window 1 to copy selection
end tell

tell application "TextEdit"
 set text of document 1 to the clipboard
end tell
说明:脚本被放入一个紧循环中,等待加载选项卡,然后将HTML复制到剪贴板

tell application "Google Chrome"
    set t to active tab index of window 1
    tell active tab of window 1 to view source
    set t to t + 1
    repeat while (loading of tab t of window 1)
    end repeat
    tell tab t of window 1 to select all
    tell tab t of window 1 to copy selection
    delete tab t of window 1
end tell

EDIT1:上面的脚本应该可以执行您想要的任务,因为google chrome支持Javascript

--Applescript code
tell active tab of window 1
    set sourcehtml to execute javascript
    document.getElementsByTagName('html')[0].innerHTML
end tell

Chrome的AppleScript库可以执行JavaScript

这将把页面的完整来源分配给
source
并返回它

tell application "Google Chrome"
    set source to execute front window's active tab javascript "document.documentElement.outerHTML"
end tell
答案很简单:

set sourcehtml to execute javascript "document.getElementsByTagName('html')[0].innerHTML"
看起来,其他职位非常接近,但仍然没有明确/有效的解决方案。对我有效的代码:

tell application "Google Chrome"
    activate
    tell active tab of window 1
        set sourcehtml to execute javascript "document.getElementsByTagName('html')[0].innerHTML"
        return sourcehtml
    end tell
end tell

等等,Chrome现在支持AppleScript了吗?那是什么时候发生的?令人惊叹的!您可以使用
删除选项卡3
。问题是多进程架构,HTML在呈现程序中,而AppleScript代码在浏览器进程中运行。@user265260:Ooh,很好。谢谢我会把它编辑成我的答案。另外,出于好奇,为什么Chrome的这个特性会引起问题?(如果您知道答案。)
告诉要删除的窗口1的选项卡(窗口1的活动选项卡索引)
应该这样做。事情是active tab返回类型为tab的对象,tab中不支持close,因为它无法知道它属于哪个窗口(这是选项卡可以更改其窗口的正确方式)。简短回答:正如我所说的HTML存在于渲染器中,一种可能的方法是使用IPC对字符串进行隧道传输,但由此会出现其他问题。谢谢,这很有帮助。仍然在做一些繁重的UI脚本编写,但是因为有一些小bug(在Google Chrome中还不起作用的东西),这很难。我遇到的一个问题是,我需要打开一个选项卡,从主页获取一些数据(它是一个bit.ly URL),然后关闭显示原始选项卡的选项卡。由于UI脚本编写的某些原因,它想回到一个陌生的地方,我认为是标签的末尾。我怎么能说告诉窗口1的tab 154变为活动状态呢?
告诉应用程序“Google Chrome”将窗口1的活动tab索引设置为/*你想要的任何内容*/end tell
(我有一个脚本,一次又一次地点击命令`直到有问题的tab循环出现,但速度很慢,非常不雅观。)这给了我一个语法错误,应该是行尾,但找到了属性。