-2

Is there a way to determine from the terminal if a youtube video is finished playing in my browser?

matt
  • 43
  • 6

1 Answers1

1

Since your question has an tag, I'll illustrate one method that can determine the playing/paused/ended status of a YouTube video (such as the linked video demonstrating how to turn a sphere inside out).

You didn't mention what browser you use. I use Safari, so I'm going to use this in my AppleScript code, however, it would be just as achievable in Google Chrome. This method will not work in Firefox.

The browser settings must be such that JavaScript from Apple Events is allowed, and Remote Automation is allowed. In Safari, these are available in the Develop menu.

When inspecting the HTML DOM of a YouTube video page, there appears to be a <div> element that serves as a container for the HTML5-rendered video object whose class name is dynamically updated to include a label that signifies whether the video is playing, paused, or ended, and, moreover, that outside of this, no other elements contain these labels in their class names.

The AppleScript below simply runs a JavaScript command in the YouTube tab of the web browser that determines whether the <div> element in question is of class playing-mode, paused-mode, or ended-mode:

    tell application "Safari" to tell front window to tell ¬
        (first tab whose URL contains "youtube.com/watch?v=") to ¬
        do JavaScript [¬
            "document", ¬
            ".querySelectorAll('div[class*=\"-mode\"]')[0]", ¬
            ".className", ¬
            ".match(/(playing|paused|ended)-mode/)[1]"] ¬
            as text

It returns either playing, paused or ended to denote the status of the video, or current application if there was a JavaScript error (e.g. no matching elements or class name labels).

You can run this script in Script Editor, or from Terminal by using the osascript command:

    osascript -e 'tell application "Safari" to tell front window to tell ¬' \
              -e '(first tab whose URL contains "youtube.com/watch?v=") to ¬' \
              -e '    do JavaScript ["document", ¬' \
              -e '        ".querySelectorAll(\"div[class*=-mode]\")[0]", ¬' \
              -e '        ".className", ¬' \
              -e '        ".match(/(playing|paused|ended)-mode/)[1]"] ¬' \
              -e '    as text'

This will, likewise, return either playing, paused, or ended; nothing in the event of a JavaScript error; and an error message in the event of an AppleScript error (e.g. no YouTube tab open in Safari).

CJK
  • 5,732
  • 1
  • 8
  • 26
  • When clicking **Allow JavaScript from Apple Events** from the **Develop** menu in **Safari** and receiving this message "**Are you sure you want to allow JavaScript from Apple Events?** Malicious programs can use this feature to harm your computer or steal your personal or financial information, like passwords, photos, or credit cards.", it really makes one think twice before clicking the **Allow** button! :) +1 – user3439894 Mar 13 '18 at 13:36
  • Thanks for the warning. If I really need to use this, how can I avoid having malicious programs use this feature? – matt Mar 23 '18 at 15:58
  • By not downloading to running scripts on your computer that haven’t come from trustworthy sources and/or you don’t know what they do. Also, safeguard your computer’s physical security, by setting a password to wake it back up from sleep when you’re absent, and don’t allow others to use your computer without your being there (in case they decided to plant/write a script that ran in the background). There’s another option allowing **Javascript from the Smart Search Field**; **DON’T** allow this, or immediately deactivate it after use, otherwise websites can execute code on your machine. – CJK Mar 23 '18 at 16:04
  • You could also probably run a periodic scan for malicious software (*malware*) using a program like *ClamXav*, or another one you favour. These can detect general security threats, including malicious scripts operating in the background. I’m sure there are other bits of advice that others can give. I’m personally not to worried about that particular setting myself, but everyone has different security needs and operate their machine in a different environment, so there are degrees of necessity dependent upon your own habits. – CJK Mar 23 '18 at 16:08