Youtube: disable suggested autoplay

Disable and hide suggested video autoplay on YouTube

  1. // ==UserScript==
  2. // @name Youtube: disable suggested autoplay
  3. // @include https://www.youtube.com/*
  4. // @description Disable and hide suggested video autoplay on YouTube
  5. // @version 1.0
  6. // @author wOxxOm
  7. // @namespace wOxxOm.scripts
  8. // @license MIT License
  9. // @run-at document-start
  10. // ==/UserScript==
  11.  
  12. /* jshint lastsemic:true, multistr:true, laxbreak:true, -W030, -W041, -W084 */
  13.  
  14. let next, cancel;
  15.  
  16. document.addEventListener('DOMContentLoaded', processPage);
  17. window.addEventListener('yt-navigate-finish', processPage);
  18. window.addEventListener('spfdone', processPage);
  19.  
  20. function processPage(e) {
  21. next = document.querySelector('.ytp-upnext');
  22. cancel = document.querySelector('.ytp-upnext-cancel-button');
  23. if (!next)
  24. return;
  25. if (getComputedStyle(next).display != 'none')
  26. cancelNext();
  27. else
  28. new MutationObserver((_, ob) => { cancelNext(); ob.disconnect(); })
  29. .observe(next, {attributes: true, attributeFilter: ['style']});
  30. }
  31.  
  32. function cancelNext() {
  33. cancel && cancel.click();
  34. next.remove();
  35. console.log('Canceled suggested video autoplay');
  36. }