Youtube Subtitle

打开中文字幕,无中文字幕则将第一个字幕自动翻译为简体中文,无自动翻译则使用第一个字幕

目前為 2020-05-14 提交的版本,檢視 最新版本

// ==UserScript==
// @name               Youtube Subtitle
// @namespace    https://gf.qytechs.cn
// @version      1.1.0
// @description  打开中文字幕,无中文字幕则将第一个字幕自动翻译为简体中文,无自动翻译则使用第一个字幕
// @author      [email protected]
// @source      https://github.com/szdailei/GM-scripts
// @match       https://www.youtube.com/watch?v=*
// @run-at       document-end
// ==/UserScript==

/**
require: none
ensure: 
  1. If there is Chinese subtitle, turn on it.
  2. If there is non-Chinese subtitle and auto-translation, turn on the first subtitle and translate to Simp Chinese.
  3. If there is non-Chinese subtitle without auto-translation, turn on the first subtitle.
  4. If there isn't subtitle, doesn't turn on subtitle.
*/
'use strict';
(function youtubeSubtitle() {
  var videoLoadCount, subtitleMenuLoadCount, translateToSimpChineseCount;
  videoLoadCount = subtitleMenuLoadCount = translateToSimpChineseCount = 0;
  const MAX_VIDEO_LOAD_COUNT = 10;
  const MAX_SUBTITLE_MENU_LOAD_COUNT = 10;
  const MAX_TRANS_TO_SIMP_CHINESE_COUNT = 5;
  const CHINESE_SUBTITLE = 'Chinese Subtitle';
  const NO_SUBTITLE = 'No Subtitle';
  const NON_CHINESE_SUBTITLE = 'Non Chinese Subtitle';
  const ON_SUBTITLE_MENU = 'On Subtitle Menu';

  function onDOMContentLoaded() {
    if (videoLoadCount === MAX_VIDEO_LOAD_COUNT) {
      return;
    }
    var videos = document.getElementsByTagName('video');
    if (videos !== null) {
      var video = videos.item(0);
      if (video !== null) {
        video.play();
        onVideoPlayed();
        return;
      }
    }
    videoLoadCount++;
    setTimeout(onDOMContentLoaded, 1000);
  }

  function onVideoPlayed() {
    if (subtitleMenuLoadCount === MAX_SUBTITLE_MENU_LOAD_COUNT) {
      return;
    }
    var SubtitlesButtons = document.getElementsByClassName('ytp-subtitles-button');
    if (SubtitlesButtons !== null) {
      var SubtitlesButton = SubtitlesButtons[0];
      if (
        SubtitlesButton !== undefined &&
        SubtitlesButton !== null &&
        SubtitlesButton.getAttribute('aria-pressed') !== null
      ) {
        if (SubtitlesButton.getAttribute('aria-pressed') === false) {
          SubtitlesButton.click();
        }
        onSubtitlesMenuLoaded();
        return;
      }
    }
    subtitleMenuLoadCount++;
    setTimeout(onVideoPlayed, 1000);
  }

  function onSubtitlesMenuLoaded() {
    var turnOnChineseSubtitleResult = turnOnChineseSubtitle();
    clickSettingsButtion();
    if (turnOnChineseSubtitleResult === NON_CHINESE_SUBTITLE) {
      translateToSimpChinese();
    }
  }

  function turnOnChineseSubtitle() {
    clickSettingsButtion();
    var enterSubtitleMenuResult = enterSubtitleMenu();
    if (enterSubtitleMenuResult === CHINESE_SUBTITLE) {
      return;
    }
    var menuItemRadios = document.querySelectorAll('[role="menuitemradio"]');
    var length = menuItemRadios.length;
    var firstSubtitleIndex = 0;
    for (var i = 0; i < length; i++) {
      var innerText = menuItemRadios[i].innerText;
      if (innerText.indexOf('添加字幕') !== -1 || innerText.indexOf('关闭') !== -1) {
        continue;
      }
      firstSubtitleIndex = i;
      if (innerText.indexOf('中文') !== -1) {
        menuItemRadios[i].click();
        return CHINESE_SUBTITLE;
      }
    }
    if (firstSubtitleIndex === 0) {
      return NO_SUBTITLE;
    } else {
      menuItemRadios[firstSubtitleIndex].click();
      return NON_CHINESE_SUBTITLE;
    }
  }

  function translateToSimpChinese() {
    if (subtitleMenuLoadCount === MAX_TRANS_TO_SIMP_CHINESE_COUNT) {
      return;
    }
    clickSettingsButtion();
    enterSubtitleMenu();
    var menuItemRadios = document.querySelectorAll('[role="menuitemradio"]');
    var length = menuItemRadios.length;
    if (menuItemRadios[length - 1].innerText === '自动翻译') {
      menuItemRadios[length - 1].click();
      menuItemRadios = document.querySelectorAll('[role="menuitemradio"]');
      length = menuItemRadios.length;
      for (var i = length - 1; i > -1; i--) {
        var innerText = menuItemRadios[i].innerText;
        if (innerText.indexOf('中文(简体)') !== -1) {
          menuItemRadios[i].click();
          return;
        }
      }
      return;
    }
    translateToSimpChineseCount++;
    setTimeout(translateToSimpChinese, 1000);
  }

  function clickSettingsButtion() {
    var settingsButtions = document.getElementsByClassName('ytp-settings-button');
    var settingsButtion = settingsButtions[0];
    settingsButtion.click();
  }

  function enterSubtitleMenu() {
    var menuItems = document.querySelectorAll('[role="menuitem"]');
    var length = menuItems.length;
    for (var i = 0; i < length; i++) {
      var innerText = menuItems[i].innerText;
      if (innerText.indexOf('字幕') !== -1) {
        if (innerText.indexOf('中文') !== -1) {
          return CHINESE_SUBTITLE;
        } else {
          menuItems[i].click();
          return ON_SUBTITLE_MENU;
        }
      }
    }
  }

  window.addEventListener('yt-navigate-finish', youtubeSubtitle); // reRun this script if click on youtube link
  onDOMContentLoaded();
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址