Disable YouTube Autoplay

Keeps checking if autoplay is enabled and disables it

当前为 2024-01-21 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            Disable YouTube Autoplay
// @description     Keeps checking if autoplay is enabled and disables it
// @namespace       https://github.com/tadwohlrapp
// @author          Tad Wohlrapp
// @version         0.0.3
// @license         MIT
// @homepageURL     https://github.com/tadwohlrapp/disable-youtube-autoplay
// @supportURL      https://github.com/tadwohlrapp/disable-youtube-autoplay/issues
// @icon            https://github.com/tadwohlrapp/disable-youtube-autoplay/raw/main/icon.png
// @match           https://www.youtube.com/*
// @grant           none
// ==/UserScript==

let counter
const isVideo = location.pathname.startsWith("/watch")

function resetCount() {
  if (isVideo) counter = 0
}

(function () {
  'use strict';

  window.addEventListener("yt-navigate-finish", resetCount, true)
  resetCount()

  setInterval(() => {
    if (!isVideo) return

    const autoplayButton = document.querySelector('button[data-tooltip-target-id="ytp-autonav-toggle-button"]')
    const autoplayBadge = document.querySelector('.ytp-autonav-toggle-button')
    autoplayBadge.dataset.counter = counter > 0 ? counter : '⏸'

    if (autoplayBadge.getAttribute('aria-checked') === 'true') {
      autoplayButton.click()
      counter++
      autoplayBadge.dataset.counter = counter
      console.log(`[Disable YouTube Autoplay]: Autoplay deactivation counter: ${counter} (${new Date().toLocaleTimeString()})`)
    }
  }, 1000)

  function addGlobalStyle(css) {
    const head = document.getElementsByTagName('head')[0]
    if (!head) return
    const style = document.createElement('style')
    style.innerHTML = css
    head.appendChild(style)
  }

  addGlobalStyle(`
    .ytp-autonav-toggle-button[aria-checked=false]:after {
      content: attr(data-counter);
      line-height: 20.4px;
      text-align: center;
      font-size: 12px;
      font-weight:700;
      background-color: #666;
      background-image: none;
    }
    .ytp-autonav-toggle-button[aria-checked=true]:after {
      content: "";
    }
    .ytp-big-mode .ytp-autonav-toggle-button[aria-checked=false]:after {
      line-height: 25.5px;
      font-size: 14px;
    }
  `)

})();