【问题标题】:Applescript - reload webpage till HTML contains specific textApplescript - 重新加载网页,直到 HTML 包含特定文本
【发布时间】:2017-07-01 11:31:24
【问题描述】:

我想创建一个applescript。

我需要在 Chrome(或 Safari)中自动重新加载当前网页,直到 HTML 中出现一些特定的关键字。找到此关键字后,停止重新加载。

该网页有一个带有广告的区域。只要我能看到一些特定的广告,我就需要重新加载页面。这意味着关键字adPraha 在HTML 中。

你能帮帮我吗?

set keyword to "adPraha"
tell application "Safari"   
    -- get the current window
    set myWindow to (get current tab of window 1)

    repeat
        do JavaScript "window.location.reload()" in myWindow


        -- wait for the page to load        
        repeat until ((do JavaScript "document.readyState;" in myWindow) is equal to "complete")
            delay 0.5
        end repeat

        set pageContent to text of myWindow

        if pageContent contains keyword then

        else

        end if

        delay 2 -- wait a bit before running again

    end repeat
end tell

【问题讨论】:

  • 这个脚本到底有什么问题?对我来说,它似乎有效。我在if/elseelse/end if 之间添加了一个显示对话框,当pageContent 包含keyword 时脚本成功检测到。您在寻找exit repeat 命令吗?把它放在else/endif之间或将“包含关键字”替换为“不包含关键字”,它会在关键字丢失时退出重复。

标签: html search applescript reload


【解决方案1】:

此脚本将通过对话框通知您该页面已检测到关键字。它仅适用于 Safari。谷歌浏览器执行 JavaScript 有点不同:

tell application "Safari"

    set keyword to "adPraha"
    repeat
        set myWindow to current tab of first window
        activate

        do JavaScript "window.location.reload()" in myWindow

        repeat while (do JavaScript "document.readyState" in document 1) is not "complete"
            delay 0.5
        end repeat

        set pageContent to do JavaScript ("window.document.documentElement.outerHTML") in myWindow

        if pageContent contains keyword then
            display dialog "found it"
            exit repeat
        end if
        delay 2 -- wait a bit before running again
    end repeat
end tell

这对你有用吗?

【讨论】:

  • 非常感谢!这适用于网络中的单词。但我想在 HTML 源代码中搜索关键字。网页中 HTML 的一些标签或一些值。是否可以搜索它?我找到了一些用于解析 HTML 的脚本,但它做不到。
猜你喜欢
  • 2021-10-11
  • 2013-10-15
  • 2015-01-06
  • 2013-09-11
  • 2017-06-23
  • 1970-01-01
  • 1970-01-01
  • 2014-04-08
  • 2010-11-16
相关资源
最近更新 更多