Skip to content

Instantly share code, notes, and snippets.

@resultakak
Forked from tarikguney/userscript.js
Created January 19, 2022 11:32
Show Gist options
  • Save resultakak/33028bf7e2e1a7d1a7bb51e5fbc32664 to your computer and use it in GitHub Desktop.
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';
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);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment