Script or addon to disable YouTube's "up next" / AutoPlay feature

Is there any way to disable the YouTube "up next" or auto play feature completely using a script or add on for Firefox? Note that the name "auto play" is obfuscating. I am specifically referring to the feature that will play a "suggested" video after the current video finishes playing. The feature often gets stuck in loops repeating the same two videos ad infinitum. Additionally, it often plays the next video when I am in the middle of writing a comment below the video. The recent UI change has broken the add on that I had been using to accomplish this task. Enabling cookies is not an option for me, because then I have to choose between these two unpleasant alternatives:

  1. Keep cookies on and deal with intrusive "recommended" videos that pollute the links to the side of the current video.

  2. Deal with this terribly implemented feature that seems specifically designed to extract ad revenue from the user.

I do not believe that this thread is a duplicate, because I am not aware of any existing scripts that disable the auto play feature after the recent UI changes (after extensive searching on GitHub, the Firefox about:addons and Google). It would be extremely helpful if the script met two conditions.

  1. It did not rely on a trivial solution such as pausing the video 1 second before it ends.
  2. It will still be functional after YouTube changes its UI once again.

Below is a userscript that is similar to the one I was using before the update but is now broken.

 (function () {
'use strict';
function removeAPUN() {
    var autoplaybar = document.getElementsByClassName('autoplay-bar')[0];
    if (autoplaybar) {
        autoplaybar.removeAttribute('class');
        document.getElementsByClassName('checkbox-on-off')[0].remove();
    }
}
window.addEventListener('readystatechange', removeAPUN, true);
window.addEventListener('spfdone', removeAPUN);

}());

Thanks in advance!


You say disable the feature completely, so I'm not sure if the following is 100% what you need, but maybe it helps.

The following keeps looking for the "disable autoplay" button until it exists and then clicks it, if it is currently set to active:

setTimeout(disable_autoplay, 1000);

function disable_autoplay(){
    // Try to get the toggle button
    var toggle_button = document.getElementById("toggle");
    if (toggle_button){
        if (toggle_button.hasAttribute("checked")){
            // If it is checked, click it to disable autoplay
            toggle_button.click();
            // Retry one more time to make sure it stays disabled
            setTimeout(disable_autoplay, 5000);
        } 
    } else {
        // Not found, retry
        console.log("Retrying in 1s");
        setTimeout(disable_autoplay, 1000);
    }
}

After the button is clicked it retries one more time, I am not certain that is required but when I manually disable the autoplay it sometimes just switches back on a moment later, so this is really just to make sure it stays off.