Skip to content

Instantly share code, notes, and snippets.

@chk1
Last active October 12, 2023 15:27
Show Gist options
  • Save chk1/9b3cdbfa5008fd64654ec66137b36dd9 to your computer and use it in GitHub Desktop.
Save chk1/9b3cdbfa5008fd64654ec66137b36dd9 to your computer and use it in GitHub Desktop.
Play embedded Youtube videos for two seconds
// ==UserScript==
// @name Play embedded Youtube videos for two seconds
// @version 1.0
// @grant none
// @include https://www.youtube.com/embed/*
// @include https://www.youtube-nocookie.com/embed/*
// ==/UserScript==
// I chose two seconds because that's how long the bottom bar with play/pause/time stays visible
// Once that bar disappears, the time won't update, and the script can't react to
// the correct time code. Maybe there's some other way.
var playVideoUntil = "0:02";
//wait for the time elapsed timer to count to desired time
var config = {
childList: true,
attributes: true,
subtree: true,
attributeOldValue: true,
characterData: true
};
var timeElapsedObserver = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
console.log(mutation);
if(mutation.addedNodes.length > 0
&& mutation.target.innerHTML == playVideoUntil){
playPauseBtn.click();
timeElapsedObserver.disconnect();
}
});
});
// big play button in center
//var playBtn = document.querySelector("button.ytp-large-play-button.ytp-button");
// small play/pause button bottom left
var playPauseBtn = document.querySelector("button.ytp-play-button.ytp-button");
if(playPauseBtn.getAttribute("aria-label") == "Play"){
playPauseBtn.click();
}
var spanTimeElapsed = document.querySelector("span.ytp-time-current")
timeElapsedObserver.observe(spanTimeElapsed, config);
@chk1
Copy link
Author

chk1 commented Mar 1, 2018

There seems to be a bug in Greasemonkey for Firefox that prevents this script from working in an <iframe>, see greasemonkey/greasemonkey#2574

Seems to work fine in Chrome with Tampermonkey though.

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