Skip to content

Instantly share code, notes, and snippets.

@resultakak
Forked from tarikguney/userscript.js
Created January 19, 2022 11:32
Show Gist options
  • Select an option

  • Save resultakak/33028bf7e2e1a7d1a7bb51e5fbc32664 to your computer and use it in GitHub Desktop.

Select an option

Save resultakak/33028bf7e2e1a7d1a7bb51e5fbc32664 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';
// Waits for this many seconds to try to find Netflix media player. Waiting for document ready is not enough.
// A better solution would check if the media player is ready in a loop rather than a fixed delay, but this is simpler for now.
var initializationDelay = 10000; // 10 seconds
var interruptionInterval = 40000; // 40 seconds
var pauseTime = 8000; // 8 seconds
if(document.readyState === 'ready' || document.readyState === 'complete') {
console.info("page is ready");
startInterruptingNetflix();
} else {
document.onreadystatechange = function () {
if (document.readyState == "complete") {
console.info("page is ready");
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});
function onUrlChange() {
console.info("Playing the next video");
startInterruptingNetflix();
}
})();
function startInterruptingNetflix(){
setTimeout(function(){
var mediaPlayer = document.getElementsByTagName("video")[0];
var mediaPlayerFound = mediaPlayer != null;
if(mediaPlayerFound){
console.info("the netflix media player is found");
setInterval(function(){
mediaPlayer.pause();
console.info("the media player is paused");
setTimeout(function(){
mediaPlayer.play();
console.info("the media player resumes");
},pauseTime);
},interruptionInterval);
} else {
console.error("the netflix media player could not be found. increase the intializationDelay perhaps.");
}
},initializationDelay);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment