Skip to content

Instantly share code, notes, and snippets.

@tarikguney
Last active December 8, 2023 15:07
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save tarikguney/68e3901b8e670ceb87006e5210a4e4c5 to your computer and use it in GitHub Desktop.
Save tarikguney/68e3901b8e670ceb87006e5210a4e4c5 to your computer and use it in GitHub Desktop.
Eat Your Food - Netflix Interruptor
// ==UserScript==
// @name Remember to eat your food while watching Netflix
// @namespace https://www.tarikguney.com
// @version 0.1
// @description Kids watching cartoons on Netflix often forget to eat and chew their food, which drives parents crazy. You need to sit down with them and pause the video and remind them to eat their food. This script will automate that.
// @author Tarik Guney
// @match https://www.netflix.com/watch/*
// @icon https://upload.wikimedia.org/wikipedia/commons/thumb/7/75/Netflix_icon.svg/1200px-Netflix_icon.svg.png
// @grant none
// ==/UserScript==
(function() {
'use strict';
var interruptionInterval = 40000; // 40 seconds
var pauseTime = 8000; // 8 seconds
var interruptionIntervalId;
if(document.readyState === 'ready' || document.readyState === 'complete') {
console.info("page is ready");
interruptionIntervalId = startInterruptingNetflix();
} else {
document.onreadystatechange = function () {
if (document.readyState == "complete") {
console.info("page is ready");
interruptionIntervalId = startInterruptingNetflix();
}
}
}
// If moving onto the next video in line, rebind the timer.
let lastUrl = location.href;
new MutationObserver(() => {
const url = location.href;
if (url !== lastUrl) {
lastUrl = url;
onUrlChange();
}
}).observe(document, {subtree: true, childList: true});
async function onUrlChange() {
console.info("Playing the next video");
clearInterval(interruptionIntervalId);
await new Promise(resolve => setTimeout(resolve, pauseTime));
interruptionIntervalId = startInterruptingNetflix();
}
async function startInterruptingNetflix(){
var mediaPlayer = document.getElementsByTagName("video")[0];
var maximumRetryBucket = 1000;
while(mediaPlayer == null && maximumRetryBucket >= 0){
await new Promise(resolve => setTimeout(resolve, 1000));
mediaPlayer = document.getElementsByTagName("video")[0];
maximumRetryBucket--;
}
if(mediaPlayer == null){
console.error("the netflix media player could not be found!");
}
console.info("the netflix media player is found");
return setInterval(function(){
if(mediaPlayer.readyState >= 2 && mediaPlayer.paused == false){
mediaPlayer.pause();
console.info("the media player is paused");
setTimeout(function(){
var actualPlayPromise = mediaPlayer.play();
actualPlayPromise.then(_ => {
console.info("the media player resumes");
});
},pauseTime);
}
},interruptionInterval);
}
})();
@tarikguney
Copy link
Author

tarikguney commented Jan 6, 2022

I've made some small changes:

  1. Defined initializationDelay, interruptionInterval, and pauseTime to better explain which number is what.
  2. Added known issues that I found out.
  3. Added comment for an area of improvement.
  4. Increased the pause to 8 seconds, which is a more sufficient amount of time for kids to take a bite of their food.
  5. Increased the initializationDelay variable to 10 seconds after having tested the script on a slower computer.

@gokaybiz
Copy link

gokaybiz commented Jan 9, 2022

@tarikguney
Copy link
Author

Thanks @gokaybiz!

Corrected:

  1. No more problem when automatically playing the next video in line.
  2. I removed the initializationDelay, now checking for the mediaPlayer in a loop.

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