Youtube: disable suggested autoplay

Disable and hide suggested video autoplay on YouTube

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name          Youtube: disable suggested autoplay
// @include       https://www.youtube.com/*
// @description   Disable and hide suggested video autoplay on YouTube
// @version       1.0
// @author        wOxxOm
// @namespace     wOxxOm.scripts
// @license       MIT License
// @run-at        document-start
// ==/UserScript==

/* jshint lastsemic:true, multistr:true, laxbreak:true, -W030, -W041, -W084 */

let next, cancel;

document.addEventListener('DOMContentLoaded', processPage);
window.addEventListener('yt-navigate-finish', processPage);
window.addEventListener('spfdone', processPage);

function processPage(e) {
  next = document.querySelector('.ytp-upnext');
  cancel = document.querySelector('.ytp-upnext-cancel-button');
  if (!next)
    return;
  if (getComputedStyle(next).display != 'none')
    cancelNext();
  else
    new MutationObserver((_, ob) => { cancelNext(); ob.disconnect(); })
      .observe(next, {attributes: true, attributeFilter: ['style']});
}

function cancelNext() {
  cancel && cancel.click();
  next.remove();
  console.log('Canceled suggested video autoplay');
}