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.

Revisions

  1. @tarikguney tarikguney revised this gist Jan 9, 2022. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion userscript.js
    Original file line number Diff line number Diff line change
    @@ -72,5 +72,4 @@

    },interruptionInterval);
    }

    })();
  2. @tarikguney tarikguney revised this gist Jan 9, 2022. 1 changed file with 13 additions and 23 deletions.
    36 changes: 13 additions & 23 deletions userscript.js
    Original file line number Diff line number Diff line change
    @@ -11,10 +11,6 @@

    (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

    @@ -40,15 +36,14 @@
    }
    }).observe(document, {subtree: true, childList: true});

    function onUrlChange() {
    async function onUrlChange() {
    console.info("Playing the next video");
    clearInterval(interruptionIntervalId);
    await new Promise(resolve => setTimeout(resolve, pauseTime));
    interruptionIntervalId = startInterruptingNetflix();
    }

    async function startInterruptingNetflix(){
    var interruptionIntervalId;

    var mediaPlayer = document.getElementsByTagName("video")[0];
    var maximumRetryBucket = 1000;
    while(mediaPlayer == null && maximumRetryBucket >= 0){
    @@ -57,30 +52,25 @@
    maximumRetryBucket--;
    }

    var mediaPlayerFound = mediaPlayer != null;

    if(!mediaPlayerFound){
    if(mediaPlayer == null){
    console.error("the netflix media player could not be found!");
    }

    console.info("the netflix media player is found");
    interruptionIntervalId = setInterval(function(){
    // Don't delete this, otherwise: https://developers.google.com/web/updates/2017/06/play-request-was-interrupted
    var playPromise = mediaPlayer.play();
    if (playPromise !== undefined) {
    playPromise.then(_ => {
    mediaPlayer.pause();
    console.info("the media player is paused");
    setTimeout(function(){
    mediaPlayer.play();
    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);
    });
    });

    },pauseTime);
    }

    },interruptionInterval);

    return interruptionIntervalId;
    }

    })();
  3. @tarikguney tarikguney revised this gist Jan 9, 2022. 1 changed file with 50 additions and 32 deletions.
    82 changes: 50 additions & 32 deletions userscript.js
    Original file line number Diff line number Diff line change
    @@ -17,52 +17,70 @@

    var interruptionInterval = 40000; // 40 seconds
    var pauseTime = 8000; // 8 seconds


    var interruptionIntervalId;
    if(document.readyState === 'ready' || document.readyState === 'complete') {
    console.info("page is ready");
    startInterruptingNetflix();
    interruptionIntervalId = startInterruptingNetflix();
    } else {
    document.onreadystatechange = function () {
    if (document.readyState == "complete") {
    console.info("page is ready");
    startInterruptingNetflix();
    interruptionIntervalId = startInterruptingNetflix();
    }
    }
    }
    // If moving onto the next video in line, rebind the timer.
    let lastUrl = location.href;
    let lastUrl = location.href;
    new MutationObserver(() => {
    const url = location.href;
    if (url !== lastUrl) {
    lastUrl = url;
    onUrlChange();
    }
    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 onUrlChange() {
    console.info("Playing the next video");
    clearInterval(interruptionIntervalId);
    interruptionIntervalId = startInterruptingNetflix();
    }

    })();

    function startInterruptingNetflix(){
    setTimeout(function(){
    async function startInterruptingNetflix(){
    var interruptionIntervalId;

    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--;
    }

    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.");

    if(!mediaPlayerFound){
    console.error("the netflix media player could not be found!");
    }
    },initializationDelay);
    }

    console.info("the netflix media player is found");
    interruptionIntervalId = setInterval(function(){
    // Don't delete this, otherwise: https://developers.google.com/web/updates/2017/06/play-request-was-interrupted
    var playPromise = mediaPlayer.play();
    if (playPromise !== undefined) {
    playPromise.then(_ => {
    mediaPlayer.pause();
    console.info("the media player is paused");
    setTimeout(function(){
    mediaPlayer.play();
    console.info("the media player resumes");
    },pauseTime);
    });
    }

    },interruptionInterval);

    return interruptionIntervalId;
    }

    })();
  4. @tarikguney tarikguney revised this gist Jan 9, 2022. 1 changed file with 15 additions and 5 deletions.
    20 changes: 15 additions & 5 deletions userscript.js
    Original file line number Diff line number Diff line change
    @@ -9,11 +9,6 @@
    // @grant none
    // ==/UserScript==

    /*
    Known problems:
    1. When Netflix video jumps to the next in line, the script loses its effect. Looks like the jump happens w/o page refresh.
    */

    (function() {
    'use strict';
    // Waits for this many seconds to try to find Netflix media player. Waiting for document ready is not enough.
    @@ -34,6 +29,21 @@ Known problems:
    }
    }
    }
    // 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(){
  5. @tarikguney tarikguney revised this gist Jan 6, 2022. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions userscript.js
    Original file line number Diff line number Diff line change
    @@ -24,12 +24,12 @@ Known problems:
    var pauseTime = 8000; // 8 seconds

    if(document.readyState === 'ready' || document.readyState === 'complete') {
    console.log("page is ready");
    console.info("page is ready");
    startInterruptingNetflix();
    } else {
    document.onreadystatechange = function () {
    if (document.readyState == "complete") {
    console.log("page is ready");
    console.info("page is ready");
    startInterruptingNetflix();
    }
    }
    @@ -42,13 +42,13 @@ function startInterruptingNetflix(){
    var mediaPlayerFound = mediaPlayer != null;

    if(mediaPlayerFound){
    console.log("the netflix media player is found");
    console.info("the netflix media player is found");
    setInterval(function(){
    mediaPlayer.pause();
    console.log("the media player is paused");
    console.info("the media player is paused");
    setTimeout(function(){
    mediaPlayer.play();
    console.log("teh media player resumes");
    console.info("the media player resumes");
    },pauseTime);
    },interruptionInterval);
    } else {
  6. @tarikguney tarikguney revised this gist Jan 6, 2022. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions userscript.js
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,11 @@
    // @grant none
    // ==/UserScript==

    /*
    Known problems:
    1. When Netflix video jumps to the next in line, the script loses its effect. Looks like the jump happens w/o page refresh.
    */

    (function() {
    'use strict';
    // Waits for this many seconds to try to find Netflix media player. Waiting for document ready is not enough.
  7. @tarikguney tarikguney revised this gist Jan 6, 2022. 1 changed file with 16 additions and 6 deletions.
    22 changes: 16 additions & 6 deletions userscript.js
    Original file line number Diff line number Diff line change
    @@ -11,6 +11,13 @@

    (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.log("page is ready");
    startInterruptingNetflix();
    @@ -27,17 +34,20 @@
    function startInterruptingNetflix(){
    setTimeout(function(){
    var mediaPlayer = document.getElementsByTagName("video")[0];
    console.log(mediaPlayer);
    if(mediaPlayer != null){
    console.log("the media player is found");
    var mediaPlayerFound = mediaPlayer != null;

    if(mediaPlayerFound){
    console.log("the netflix media player is found");
    setInterval(function(){
    mediaPlayer.pause();
    console.log("the media player is paused");
    setTimeout(function(){
    mediaPlayer.play();
    console.log("teh media player resumes");
    },4000);
    },50000);
    },pauseTime);
    },interruptionInterval);
    } else {
    console.error("the netflix media player could not be found. increase the intializationDelay perhaps.");
    }
    },2000);
    },initializationDelay);
    }
  8. @tarikguney tarikguney renamed this gist Jan 6, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. @tarikguney tarikguney created this gist Jan 6, 2022.
    43 changes: 43 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    // ==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';
    if(document.readyState === 'ready' || document.readyState === 'complete') {
    console.log("page is ready");
    startInterruptingNetflix();
    } else {
    document.onreadystatechange = function () {
    if (document.readyState == "complete") {
    console.log("page is ready");
    startInterruptingNetflix();
    }
    }
    }
    })();

    function startInterruptingNetflix(){
    setTimeout(function(){
    var mediaPlayer = document.getElementsByTagName("video")[0];
    console.log(mediaPlayer);
    if(mediaPlayer != null){
    console.log("the media player is found");
    setInterval(function(){
    mediaPlayer.pause();
    console.log("the media player is paused");
    setTimeout(function(){
    mediaPlayer.play();
    console.log("teh media player resumes");
    },4000);
    },50000);
    }
    },2000);
    }