Last active
October 12, 2023 15:27
-
-
Save chk1/9b3cdbfa5008fd64654ec66137b36dd9 to your computer and use it in GitHub Desktop.
Play embedded Youtube videos for two seconds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There seems to be a bug in Greasemonkey for Firefox that prevents this script from working in an
<iframe>
, see greasemonkey/greasemonkey#2574Seems to work fine in Chrome with Tampermonkey though.