Skip to content

Instantly share code, notes, and snippets.

@mvaneijgen
Created January 30, 2020 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvaneijgen/3c44c733f2a462dae7ca372f1884f8b3 to your computer and use it in GitHub Desktop.
Save mvaneijgen/3c44c733f2a462dae7ca372f1884f8b3 to your computer and use it in GitHub Desktop.
to clickClassName2(theClassName, elementnum)
if application "Safari" is running then
try
tell application "Safari"
repeat with t in tabs of windows
if URL of t contains "youtube.com/watch" then
tell t
do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();"
end tell
end if
end repeat
end tell
end try
else
return ""
end if
end clickClassName2
clickClassName2("ytp-play-button ytp-button", 0)
set str to "" as string
if ((str is equal to str) and (application "Safari" is running)) then
tell application "Safari"
repeat with t in tabs of windows
tell t
if URL starts with "https://www.youtube.com/watch" then
set state to do JavaScript [¬
"document", ¬
".querySelectorAll('div[class*=\"-mode\"]')[0]", ¬
".className", ¬
".match(/(playing|paused|ended)-mode/)[1]"] ¬
as text
if state = "playing" then
set vidName to (do JavaScript " document.getElementsByTagName('title')[0].innerHTML" as text)
return text 1 thru -11 of vidName
else if state = "paused" then
return "(Paused)"
else if state = "ended" then
return "(Video Ended)"
end if
end if
end tell
end repeat
end tell
else
return ""
end if
return str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment