脚本编辑器:等待Safari完成加载,我的JavaScript方法不工作

jchrr9hc  于 2022-12-17  发布在  Java
关注(0)|答案(2)|浏览(303)

我在执行一个简单的命令:加载URL1,等待Safari完成加载URL1,然后在同一标签中加载URL2。由于某种原因,下面的代码加载URL1,然后停止。任何帮助将不胜感激。谢谢。

tell application "Safari"
    activate
    tell window 1
        tell current tab
            set URL to "https://google.com"
        end tell
    end tell
end tell

tell application "Safari"
    tell window 1 to repeat
        do JavaScript "document.readyState"
        if the result = "complete" then exit repeat
        delay 0.5
    end repeat
end tell

tell application "Safari"
    activate
    tell window 1
        tell current tab
            set URL to "https://apple.com"
        end tell
    end tell
end tell
pnwntuvh

pnwntuvh1#

我在过去为这个任务写了下面的脚本。它是为源代码末尾有**/html标签的网页设计的。在许多网页上,/html标签是不是一个连续的单词并且跨越多个段落。我的处理程序也使用它们,不像网上提供的许多。另外,我更喜欢使用open location**命令来加载网站。如果需要,你可以替换它:

tell application "Safari"
    activate -- optional
    try
        close (current tab of window 1) -- optional, if you won't grow additional tabs
    end try
    open location "https://google.com"
    my waitSafariWebPageLoading(10) -- maximum seconds to wait = 10 seconds
    if result is true then set googleHTML to (source of document 1)
    close (current tab of window 1) -- optional, if you won't grow additional tabs
    open location "https://apple.com"
    my waitSafariWebPageLoading(10) -- maximum seconds to wait = 10 seconds
    if result is true then set appleHTML to (source of document 1)
end tell
return {googleHTML, appleHTML}

-- Safari.app: Pause a Script until a Web Page has Loaded
on waitSafariWebPageLoading(loadingWaitMaximumSeconds as integer)
    set lineChangingChars to {linefeed, return, character id 11, character id 12, character id 133, character id 8232, character id 8233}
    set {ATID, htmlEnding} to {AppleScript's text item delimiters, ""}
    tell application "Safari"
        repeat 100 * loadingWaitMaximumSeconds times -- wait seconds
            delay 0.1
            set AppleScript's text item delimiters to {"<", ">"}
            try
                copy text item -2 of (get source of front document) to htmlEnding
            end try
            set AppleScript's text item delimiters to lineChangingChars
            set htmlEnding to text items of htmlEnding
            set AppleScript's text item delimiters to ""
            set htmlEnding to "<" & htmlEnding & ">"
            if htmlEnding is "</html>" then exit repeat
        end repeat
    end tell
    set AppleScript's text item delimiters to ATID
    if htmlEnding is "</html>" then return true
    display notification "The webpage loading failed"
    return false
end waitSafariWebPageLoading

我写的最稳定的处理程序使用GUI脚本。它适用于任何网页,而且也更简单:

tell application "Safari"
    activate
    make new document with properties {URL:"https://www.macscripter.net/viewtopic.php?id=19262"}
    my waitSafariWebPageLoading()
    display dialog "The webpage is fully loaded now."
end tell

on waitSafariWebPageLoading()
    tell application "System Events" to tell application process "Safari"
        repeat until (UI element "Reload this page" of group 3 of toolbar 1 of window 1 exists) or (UI element "Reload this page" of group 2 of toolbar 1 of window 1 exists)
            delay 0.1
        end repeat
    end tell
end waitSafariWebPageLoading
flseospp

flseospp2#

您需要允许浏览器允许来自Apple活动的Javascript。首先启用在菜单栏中显示“开发”菜单,然后在浏览器的“开发”菜单中单击**允许来自Apple活动的Javascript
第一节第一节第一节第一节第一次
更新的版本(我添加了一些日志语句,这样你就可以在第二个页面打开之前看到页面被加载):

set URL1 to "https://google.com"
set URL2 to "https://apple.com"
set done to false

tell application "Safari"
    activate
    
    set currentTab to current tab of window 1
    set URL of currentTab to URL1
    
    repeat until (done is true)
        log done
        if (do JavaScript "document.readyState" in currentTab) is "complete" then
            log (do JavaScript "document.readyState" in currentTab)
            set done to true
        end if
    end repeat
   
    log done

    if done then
        tell currentTab
            set URL to URL2
        end tell
    end if
end tell

相关问题