Skip to content

Instantly share code, notes, and snippets.

@fent
Last active May 13, 2022 00:49
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fent/7763775 to your computer and use it in GitHub Desktop.
Save fent/7763775 to your computer and use it in GitHub Desktop.
Scripts for controlling youtube videos. Now maintained at https://github.com/fent/dotfiles/tree/master/scripts
# If in a playlist, play the next video.
tell application "Google Chrome"
repeat with t in tabs of windows
tell t
if URL starts with "http://www.youtube.com/watch" or URL starts with "https://www.youtube.com/watch" then
execute javascript "
var player =
document.getElementById('movie_player') ||
document.getElementsByTagName('embed')[0];
var next = document.getElementsByClassName('yt-uix-button-icon-playlist-bar-next')[0];
if (player && next) {
next.click();
}
"
exit repeat
end if
end tell
end repeat
end tell
# Pause all youtube videos in all youtube tabs.
tell application "Google Chrome"
repeat with t in tabs of windows
tell t
if URL starts with "http://www.youtube.com/watch" or URL starts with "https://www.youtube.com/watch" then
execute javascript "
var player =
document.getElementById('movie_player') ||
document.getElementsByTagName('embed')[0];
if (player) {
player.pauseVideo();
}
"
end if
end tell
end repeat
end tell
# Play or pause a video.
tell application "Google Chrome"
repeat with t in tabs of windows
tell t
if URL starts with "http://www.youtube.com/watch" or URL starts with "https://www.youtube.com/watch" then
execute javascript "
var player =
document.getElementById('movie_player') ||
document.getElementsByTagName('embed')[0];
if (player) {
if (player.getPlayerState() === 1) {
player.pauseVideo();
} else {
player.playVideo();
}
}
"
exit repeat
end if
end tell
end repeat
end tell
# If in a playlist and the current video time is <= 5 seconds,
# play the previous video,
# otherwise, rewind to the beginning of the current video and play.
tell application "Google Chrome"
repeat with t in tabs of windows
tell t
if URL starts with "http://www.youtube.com/watch" or URL starts with "https://www.youtube.com/watch" then
execute javascript "
var player =
document.getElementById('movie_player') ||
document.getElementsByTagName('embed')[0];
if (player) {
var prev = document.getElementsByClassName('yt-uix-button-icon-playlist-bar-prev')[0];
if (player.getCurrentTime() <= 5 && prev) {
prev.click();
} else {
player.seekTo(0, true);
if (player.getPlayerState() !== 1) {
player.playVideo();
}
}
}
"
exit repeat
end if
end tell
end repeat
end tell
@nkgm
Copy link

nkgm commented Oct 15, 2015

Does this still work for you with the latest Chrome version? I had a bunch of utility scripts which are all now failing as the js invocation appears to be sandboxed to the vanilla dom api's (no access to page scripts or variables).

@zachloubier
Copy link

These will not work with the latest chrome version anymore. I have created some updated scripts to use with the new chrome version which can be found here https://gist.github.com/zachloubier/9b07fd21292a7dfb92d9.

I used these scripts as inspiration and just replaced the youtube api with vanilla js.

@thienha1
Copy link

Can you make a script that set a timer to automatically pause on YouTube? On Firefox?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment