通过Applescript获取Google Chrome中当前标签的来源

时间:2010-12-14 16:24:36

标签: macos google-chrome applescript

在Safari中这样做是孩子的游戏,它有很好的Applescript支持。谷歌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

但是你不能说“set X to source of active tab of window 1”。有人对我有什么建议吗?我目前的想法是在Safari中加载我需要的代码(非常难看)或尝试显示源代码并使用UI脚本抓取它,但这也很难看。此外,我一直遇到脚本错误,使其无法正常工作。

任何帮助都将不胜感激。

6 个答案:

答案 0 :(得分:5)

由于Google Chrome支持Javascript

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

答案 1 :(得分:2)

我很高兴得知Chrome现在支持AppleScript。不幸的是,它还很小,但我确信(我希望!)它会变得更好。由于无法直接获取源,我会选择以下hackish路径:

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

是的,它是hackish,但鉴于脚本字典的当前状态,这是不可避免的。该脚本应该是直截了当的:打开源选项卡,等待它加载,选择内容,复制它,然后关闭选项卡。您可以使用delay 3来查看效果最佳的内容。请注意, first active tab of window 1是任意的,其余的显式引用了source选项卡。 另外,显然无法从Chrome的脚本词典(oy vey)中关闭标签,因此我不得不使用JavaScript。此外,最后delay 1 不应该是必要的,但是如果它不存在,我的测试有时会返回错误的东西,即使我粘贴它们时剪贴板内容是正确的。我认为这是因为有足够的文本它显得很明显更新剪贴板的时间。

编辑1:我将execute the active tab of window 1 javascript "window.close()"替换为delete tab行,正如我所建议的那样。遗憾的是,delete tab active tab of window 1不起作用,因此您需要这种稍微复杂的构造。

答案 2 :(得分:2)

-- 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:上面的脚本应该做你想要的事情

答案 3 :(得分:2)

Chrome的AppleScript库可以select * from [*YourDatabaseName*].[dbo].[sysssislog] JavaScript。

这会将页面的完整来源分配到execute并将其返回

source

答案 4 :(得分:0)

简单的答案是:

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

答案 5 :(得分:-1)

看到Chrome的AS支持“刚刚到来”,它一定会“令人兴奋”使用。在尝试他们在字典中可用的一些命令时,看起来它们仍然有一些问题需要解决。在Google公开API中的一种方式以更轻松地获取源代码(和/或解决相关的问题)之前,您必须使用您在帖子中提到的替代方法之一。