Re: Detecting Safari Web page loading
Re: Detecting Safari Web page loading
- Subject: Re: Detecting Safari Web page loading
- From: Takaaki Naganoya <email@hidden>
- Date: Tue, 20 Mar 2018 09:19:22 +0900
Thanks Jim.
Though it is not the solution I want (as you wrote), your suggestion is worthy
of study.
There is some way to detect Safari’s finish of url loading.
(1)Loop with short delay and check some page element (some target text)
(2)Loop with short delay and execute “document.readyState” JavaScript
Your suggestion is (3).
(3)Loop with short delay and evaluate the page’s Xpath structure
Very interesting.
--
Takaaki Naganoya
email@hidden
http://piyocast.com/as/
> 2018/03/20 5:46、Jim Underwood <email@hidden>のメール:
>
> Takaaki,
>
> I'm not sure that I understand your purpose/objective, so my response may or
> may not directly apply for your use case.
>
> But for a more general use case, my response speaks to the general question
> of knowing when a web page has completed loading so that further processing
> of that page by script can be done.
>
> I do a lot of web scraping, and I have found that using a JavaScript XPath to
> be a very reliable method to determine when a web page has completed loading
> sufficiently for my purpose. My purpose is usually to do another JavaScript
> in Browser to extract the info I want from the web page.
>
> I use the Chrome Dev Tools to view, inspect, develop, and test the XPath that
> I need.
>
> Here is an example AppleScript, which includes the JavaScript.
> The XPath in this example is looking for the "Create Topic" button on the
> Script Debugger forum ("http://forum.latenightsw.com/ ) site. Note that you
> must be logged in to the forum for this button to appear.
>
> To All: Please feel free to ask any questions about my method/script.
>
> Screenshot of Web Page Button of Interest
>
>
>
> use AppleScript version "2.5" -- El Capitan (10.11) or later
> use scripting additions
>
> (*
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> VER: 2.0 2018-03-19
>
> PURPOSE: Provide example of how to determine when Safari Page has completed
> loading
>
> METHOD:
> • Assume page has completed loaded for your purpose when a specific HTML
> element exists
> • Use XPath to specify the required HTML Element
> • Use JavaScript in Browser to check for existance of element
>
> AUTHOR: JMichaelTX
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> *)
>
> --- CHANGE THESE VARIABLES AS NEEDED ---
>
> set urlStr to "http://forum.latenightsw.com/"
> set xPathStr to "//button[@id=\"create-topic\"]" -- must be logged in to find
> this button
> set delayTime to 0.1
> set maxTime to 3
> set notifyTime to 0.2
>
> --- OPEN URL IN NEW SAFARI TAB ---
>
> tell application "Safari"
> activate
> tell window 1
> set current tab to (make new tab with properties {URL:urlStr})
> end tell
> end tell
>
> --- PAUSE UNTIL XPATH is FOUND, or TIME-OUT ---
> -- (script will CANCEL on time-out)
>
> set pageStatusStr to "[NOT-READY]"
> set elapsedTime to 0
>
> repeat while (pageStatusStr ≠ "[READY]")
> set pageStatusStr to getPageStatus(xPathStr)
> delay delayTime
> set elapsedTime to elapsedTime + delayTime
> if (elapsedTime > maxTime) then
> set msgStr to "Max Time of " & maxTime & " sec exceeded waiting for
> Safari page to load"
> display dialog msgStr with title (name of me) buttons {"Cancel"} default
> button "Cancel"
> end if
>
> if my isMod(elapsedTime, notifyTime) then
> set msgStr to "Elapsed Time: " & elapsedTime & " sec"
> set msgTitleStr to "Waiting for Safari Page to Load"
> display notification msgStr with title msgTitleStr sound name "Tink.aiff"
> end if
>
> end repeat
>
> set msgStr to "Page Loaded in " & elapsedTime & " sec"
> set msgTitleStr to "Safari Page Load Completed"
> display notification msgStr with title msgTitleStr sound name "Hero.aiff"
>
> return pageStatusStr
>
> --~~~~~~~~~~~~ END MAIN SCRIPT ~~~~~~~~~~~~~~~~
>
> on isMod(pNumber, pDiv)
> return ((((pNumber * 10) as integer) mod (pDiv * 10) as integer) = 0)
> end isMod
>
> on getPageStatus(pXPathStr)
> local jsStr
>
> set jsStr to "
> 'use strict';
> (function run() { // this will auto-run when script is executed
>
> var xPath = '" & pXPathStr & "'
> var nodes = document.evaluate(xPath, document, null, XPathResult.ANY_TYPE,
> null)
> var result = nodes.iterateNext();
>
> var statusStr = (result) ? \"[READY]\" : \"[NOT-READY]\"
>
> return statusStr;
>
> } // END of function run()
> )();
> "
>
> tell application "Safari"
> set jsScriptResults to do JavaScript jsStr in front document
> end tell
>
> return jsScriptResults
>
> end getPageStatus
>
>
>
>
>
> Best Regards,
>
> Jim Underwood
> aka JMichaelTX
>
>
> From: AppleScript-Users
> <applescript-users-bounces+jmichael=email@hidden> on behalf of
> Takaaki Naganoya <email@hidden>
> Date: Monday, March 19, 2018 at 10:28 AM
> To: "ASUL (AppleScript)" <email@hidden>
> Subject: Re: Detecting Safari Web page loading
>
> Thanks for your reply. But my intention is a little different.
>
> I wanted to know the new URL loading event by using Notification mechanism.
> Now, we can detect iTunes’ track changed event by using it without polling
> with short delay loop.
>
>
>
> <AppleScript>
> use AppleScript version "2.4"
> use scripting additions
> use framework "Foundation"
> use framework "AppKit"
>
> set aNC to current application's NSDistributedNotificationCenter's
> defaultCenter()
> aNC's addObserver:me selector:"updateNow:"
> |name|:"com.apple.iTunes.playerInfo" object:(missing value)
>
> on updateNow:aNotification
> set theNotif to aNotification's userInfo
> log theNotif
> end updateNow:
> </AppleScript>
>
> --
> Takaaki Naganoya
> email@hidden
> http://piyocast.com/as/
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> AppleScript-Users mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
> Archives: http://lists.apple.com/archives/applescript-users
>
> This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden