Skip to content

Instantly share code, notes, and snippets.

@jeffmbellucci
Last active January 31, 2024 11:07
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save jeffmbellucci/8db3a8b27664dac6b8f9cf10b416b433 to your computer and use it in GitHub Desktop.
Save jeffmbellucci/8db3a8b27664dac6b8f9cf10b416b433 to your computer and use it in GitHub Desktop.
Turn off/disable YouTube autoplay feature
// To run, install GreaseMonkey or TamperMonkey extension in your browser
// Copy this code into new user script, and enable
// ==UserScript==
// @name Disable Youtube autoplay
// @version 1.0
// @description This script turns off Youtube's newest autoplay feature after the page loads
// @author Jeff Bellucci
// @match *://www.youtube.com/*
// @run-at document-start
// @grant none
// ==/UserScript==
(function() {
'use strict';
function uncheck(toggle) {
if (toggle.hasAttribute('checked')) {
toggle.click();
}
}
function disableAfterLoad() {
var autoplayToggle = document.getElementById('toggle');
if (autoplayToggle) {
uncheck(autoplayToggle);
} else {
setTimeout(disableAfterLoad, 500);
}
}
disableAfterLoad();
})();
@jeffmbellucci
Copy link
Author

You're welcome, I'm glad it is helpful.

@jeffmbellucci
Copy link
Author

This gist has been added to the website Greasy Fork for more exposure and easy installation, and can be found at the link below:

https://greasyfork.org/en/scripts/34651-disable-youtube-autoplay

@LESHIY-ODESSA
Copy link

This does not work for old "Classic Design". ID = autoplay-checkbox

Perhaps you seem to be right to add icons.

// @ICON https://raw.github.com/YePpHa/YouTubeCenter/master/assets/icon48.png
// @icon64 https://raw.github.com/YePpHa/YouTubeCenter/master/assets/icon64.png

Copy link

ghost commented Jan 3, 2018

Thanks a lot, I pput my foot down and searched for +30 mins and finally found a working solution for the modern GUI ... had to deal with this for like a year if not more.

@jeffmbellucci
Copy link
Author

Leshiy, I'm not exactly sure what you mean, but if you are running a different version of youtube somehow, if you know the "id" attribute of the toggle, you may be able to get this to work by just changing the line of the script that finds the page element. Lemme know if I can help.

@LaaZa
Copy link

LaaZa commented Jan 20, 2018

Does not work on Firefox 58 with Greasemonkey 4.2, because toggle.checked is undefined. Works with Violentmonkey. As a workaround that should work on every addon toggle.checked can be changed to toggle.hasAttribute("checked").

@jeffmbellucci
Copy link
Author

OK, thanks, I will update it.

@agrn
Copy link

agrn commented May 29, 2018

Hi,
the ID of the toggle widget is not "toggle" anymore, but "improved-toggle".

@jeffmbellucci
Copy link
Author

@agrn, thanks for noticing the change, I updated, so should work again.

@downloaderfan
Copy link

It seems the ID is back to "toggle". I made that change & it started working again.

@jeffmbellucci
Copy link
Author

@downloaderfan, thanks for the heads up. I updated the gist to reflect that change, and it is working again for me as well.

@Jakobimatrix
Copy link

I got annoyed with the whole back and forth of youtube changeing the stupid ID. I forked you and did some minor improvements you might wanna try too. (Unfortunately gist doesnt support pull requests)

@jeffmbellucci
Copy link
Author

@Jakobimatrix I understand what you are trying to do, and it makes sense. However, it makes your code much more complicated, and if they ever change it to an option you don't have listed, then it will stop working. I'm not saying its a bad approach, it is definitely effective if you have the valid id in you your list of guesses.

My whole approach was to keep the code as short and elegant as possible, if the id changes, there is just as good of a chance they will change it to something completely new as they will change it to something in your list. I think a better solution would be to have the code notify you if the id has changed from what you have currently set, than to guess.

I will update my script with what I mean.

@S3NTYN3L
Copy link

S3NTYN3L commented Mar 6, 2021

No longer working as of March 2021.
Please update.

@jeffmbellucci
Copy link
Author

@S3NTYN3L, I just tested it with Chrome from a laptop, and it is still working for me. What browser/platform are you using?

@downloaderfan
Copy link

Same here, no longer working. Using Chrome v88.0.4324.190 (Official Build) (64-bit) on Windows 10 Pro v2004 build 19041.804 with dark mode. Tried it in incognito.

Copy link

ghost commented Mar 8, 2021

Tried on Firefox Dev Edition 87.0b1, it doesn't work.

I tried .click() the parent node "ytp-button" of class "ytp-autonav-toggle-button-container" but click event isn't working either:

var node = document.getElementsByClassName( "ytp-autonav-toggle-button-container" );
if ( node & node.length )
    node[ 0 ].parentNode.click( );
}

All of the customizations I did were simply altering the internal properties such as e.loop = true. While some were through its api such as e.setPlaybackQuality( q ) and e.setPlaybackRate( r ). I try not to rely on storage, click, or altering cookies/reloading pages.

I wonder if autoplay next property or a set function exists. I'm not advanced at this.

@S3NTYN3L
Copy link

S3NTYN3L commented Mar 9, 2021

@jeffmbellucci
Linux Mint and Arch
Waterfox Current and Classic
All up to date.

@jeffmbellucci
Copy link
Author

@S3NTYN3L, I am using Mac OS Mojave and Chrome. For whatever reason, I can't reproduce the failure. So far on my machine, the script still works. I wonder if you are getting served a different version of the Youtube front-end for some reason, like maybe they are A/B testing something new, and I am not in the test group? I dunno why else it might still works for me and not others.

Copy link

ghost commented Mar 9, 2021

@jeffmbellucci, that certainly might be possible. A few months ago, there was no Autoplay Next button on the player controls. But then it suddenly appeared. It became apparent because I hid other controls to prevent mis-clicking. I hid this thing right away after it first appared, of course.
But that Auto Play button for some reason only visible to my account. If I log out, it's not there. It's also already existed in Youtube android during that time of me discovering it.

For example now it's always there now even if I logged:
2021-03-09_14 07 39_Tuesday

Copy link

ghost commented Mar 9, 2021

Also, my bad.
I just realized it's actually still works for me. I forgot I hid (deleted) element id "secondary" before your script, yeeting the entire next-suggested playlist from existence to prevent binge watching. It works once I commented out that part.

So for your script to work, I have to use yours to disable autoplay next before removing the next-suggested playlist. Thank you for sharing the script!

@konomikitten
Copy link

I've updated one of my scripts in my userscripts repo to work with the new YouTube, it's pretty hacky though issue reports welcome.

@S3NTYN3L
Copy link

@01yakiek
This is what I see.
Doesn't matter if I'm logged in or not.

Copy link

ghost commented Mar 17, 2021

@S3NTYN3L
It finally stopped working for me as well. @konomikitten's script works wonderfully.
But the mutationObserver freaks out on my side, so I replaced it with setInterval every 1 second instead and stops the script once it detects that the autoplay next has been disabled.

Here's a shortened working script that only disables autoplay next with setInterval, nothing else.

var var_interval_id = window.setInterval( function( window ) {
    let a = window.document.querySelector( "button.ytp-button[data-tooltip-target-id='ytp-autonav-toggle-button']" ); // get the button
    if ( a.getAttribute( "aria-label" ) == "Autoplay is on" ) {
        a.click( ); // disable autoplay next if enabled
    }
    if ( a.getAttribute( "aria-label" ) == "Autoplay is off" ) {
        window.clearInterval( var_interval_id ); // end script once done
    }
}, 1024, window );

@S3NTYN3L
Copy link

@01yakiek
That script seems to be working.
Thanks for sharing it.

@Synetech
Copy link

The problem is that Google's horrible naming abilities have caused confusion over what is what. This, and @konomikitten's scripts only affect the button on videos as seen in @01yakiek's screenshot which is incorrectly labeled "Autoplay". In reality, that button should be labeled "Auto Advance" (which is why konomikitten calls it "Next Up"). All it does is to determine if YouTube automatically plays the next video in a playlist (or ostensibly if it moves on to the next suggested/related/recommended video). It doesn't actually control or block auto-PLAY.

If you toggle that switch, videos will still play automatically when you navigate to a video page (e.g., if you go to a playlist's page then click on one of the videos). It also doesn't stop videos from automatically playing when you view a channel page or the YouTube homepage.

The worst part is that even browsers' own autoplay controls can't block it. The Firefox devs have indicated that because of the way Google has redesigned YouTube to be "dynamic", you're not really navigating to "new" pages anymore, you're still on the same page which is just being modified with new content, therefore Firefox doesn't ask you if you want to let media play again, so if you click to allow a video to play once in a session, it's considered allowed indefinitely throughout that session. 🤦

I'm not sure anybody will be able to prevent Google from forcibly autoplaying videos and wasting CPU cycles and bandwidth and mobile data and jump-scaring people with audio and other stuff in their selfish endeavor to convince advertisers that people are "choosing" to watch the videos and ads. 😠 This requires legislation.

@konomikitten
Copy link

@Synetech it used to be called up next, they've renamed it a few times and it's extremely frustrating. As is dealing with them changing it constantly.

@JohnyP36
Copy link

JohnyP36 commented Apr 28, 2021

@S3NTYN3L
It finally stopped working for me as well. @konomikitten's script works wonderfully.
But the mutationObserver freaks out on my side, so I replaced it with setInterval every 1 second instead and stops the script once it detects that the autoplay next has been disabled.

Here's a shortened working script that only disables autoplay next with setInterval, nothing else.

var var_interval_id = window.setInterval( function( window ) {
    let a = window.document.querySelector( "button.ytp-button[data-tooltip-target-id='ytp-autonav-toggle-button']" ); // get the button
    if ( a.getAttribute( "aria-label" ) == "Autoplay is on" ) {
        a.click( ); // disable autoplay next if enabled
    }
    if ( a.getAttribute( "aria-label" ) == "Autoplay is off" ) {
        window.clearInterval( var_interval_id ); // end script once done
    }
}, 1024, window );

Thanks for the code.
But how can I make a web extension in which I can toggle the autoplay button on and off?

I have the old background.js and the script.js
I also have a "jquery-3.2.1.min.js" but I don't know what this script does.

Please help!

@jeffmbellucci
Copy link
Author

@JohnyP36 You need to use a script running extension. Personally, I prefer Tampermonkey, but there are a number of options out there that let you save different scripts and run them as you see fit, on a single website, on all websites, on document ready, etc, etc. They are quite powerful.

@bitelaserkhalif
Copy link

bitelaserkhalif commented Mar 5, 2022

I have forked and modded this userscript due to youtube breaking stuff (You refresh the page, autoplay goes back on even if you turn it off). The autoplay got moved into html5 player, and the only way I can do it is with JQuery. My version would not allow autoplay to be turned on, it'll turn off again because of youtube bug above.

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