【问题标题】:Get Source of Current Tab in Google Chrome via Applescript通过 Applescript 获取 Google Chrome 中当前选项卡的来源
【发布时间】:2023-04-06 22:17:01
【问题描述】:

在 Safari 中执行此操作很简单,它具有良好的 Applescript 支持。谷歌浏览器的 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 脚本抓取它,但这也很丑陋。此外,我还不断遇到使其无法正常工作的脚本错误。

任何帮助将不胜感激。

【问题讨论】:

  • 等等,Chrome 现在支持 AppleScript 了吗?什么时候发生的?太棒了!

标签: macos google-chrome applescript


【解决方案1】:

由于谷歌浏览器支持 Javascript

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

【讨论】:

  • 这给了我语法错误 预期的行尾,但找到了属性。
【解决方案2】:

Chrome 的 AppleScript 库可以execute JavaScript。

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

tell application "Google Chrome"
    set source to execute front window's active tab javascript "document.documentElement.outerHTML"
end tell

【讨论】:

    【解决方案3】:

    我很高兴得知 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 3s 来查看最有效的方法。请注意,first active tab of window 1 是任意的,其余部分明确引用源选项卡。 另外,显然没有办法从 Chrome 的脚本字典 (oy vey) 中关闭选项卡,所以我不得不改用 JavaScript。 另外,最后一个 delay 1 不应该 em> 是必要的,但如果它不存在,我的测试有时会返回错误的东西,即使我粘贴时剪贴板内容是正确的。我认为这是因为有足够的文本需要大量的是时候更新剪贴板了。

    编辑 1: 我按照建议将 execute the active tab of window 1 javascript "window.close()" 替换为 delete tab 行。不幸的是,delete tab active tab of window 1 不起作用,所以你需要这个稍微复杂一点的结构。

    【讨论】:

    • 您可以使用delete tab 3。问题在于多进程架构,HTML 在渲染器中,而 AppleScript 代码在浏览器进程中运行。
    • @user265260:哦,很好。谢谢!我会将其编辑到我的答案中。另外,出于好奇,为什么 Chrome 的该功能会导致问题? (如果你知道答案。)
    • tell tab (active tab index of window 1) of window 1 to delete 应该这样做。事情是活动选项卡返回一个选项卡类型的对象,选项卡不支持关闭,因为它无法知道它属于哪个窗口(这是选项卡可以更改其窗口的正确方法)。
    • 简短回答:正如我所说的 HTML 存在于渲染器中,一种可能的方法是使用 IPC 对字符串进行隧道传输,但因此会出现其他问题。
    【解决方案4】:
    -- 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:上面的脚本应该做你想要的

    【讨论】:

    • 谢谢,这很有帮助。仍然在编写一些繁重的 UI 脚本,但是因为有一些小错误(在 Google Chrome 中还不能工作的东西),所以这很难。我遇到的一个问题是,我需要打开一个选项卡,从主页获取一些数据(这是一个 bit.ly URL),然后关闭显示原始选项卡的选项卡。由于 UI 脚本的某些原因,它想返回一个奇怪的地方,我认为是选项卡的末尾。如何告诉窗口 1 的选项卡 154 变为活动状态?
    • tell application "Google Chrome" set active tab index of window 1 to /*whatever you want*/ end tell
    【解决方案5】:

    简单的答案是:

    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
    

    【讨论】:

      【解决方案6】:

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

      【讨论】:

      • (我有一个脚本会一次又一次地点击命令`直到有问题的选项卡循环,但它很慢而且非常不雅。)
      猜你喜欢
      • 2013-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-24
      • 2021-08-09
      • 1970-01-01
      相关资源
      最近更新 更多