Auto-Select YouTube Subtitles by Sapioit

Automatically selects the YouTube subtitles with the text "English (auto-generated)"

Verze ze dne 06. 07. 2023. Zobrazit nejnovější verzi.

// ==UserScript==
// @name         Auto-Select YouTube Subtitles by Sapioit
// @namespace    Sapioit
// @copyright    Sapioit, 2020 - Present
// @author       sapioitgmail.com
// @license      GPL-2.0-only; http://www.gnu.org/licenses/gpl-2.0.txt
// @version      2.9
// @description  Automatically selects the YouTube subtitles with the text "English (auto-generated)"
// @match        https://*.youtube.com/*
// @match        https://youtube.com/*
// @match        https://youtu.be/*
// @icon         https://youtube.com/favicon.ico
// @grant        none
// ==/UserScript==

let repeatInterval = 10000; // 1000 milliseconds = 1 second; 10000 milliseconds = 10 second;
let waitInterval = 5; // Adjust the delay (in milliseconds) if needed. 1000 = 1 second.

function checkSubtitles() {
  console.log("Sapioit: Checking subtitles.");
  // Check if the value of ytp-menuitem-content is 'English (auto-generated)'
  const menuItems = document.querySelectorAll('.ytp-menuitem');
  let autoGeneratedMenuItem;

  for (const menuItem of menuItems) {
    const menuLabel = menuItem.querySelector('.ytp-menuitem-label span:first-child');
    const menuContent = menuItem.querySelector('.ytp-menuitem-content');

    if (menuLabel && menuLabel.textContent.trim() === 'Subtitles/CC' && menuContent && menuContent.textContent.trim() !== 'English (auto-generated)') {
      autoGeneratedMenuItem = menuItem;
      break;
    }
  }

  // Run the code if the condition is not met
  if (autoGeneratedMenuItem) {
      // Change the subtitles
      ChangeSubtitles();
  } else {
      console.log("Sapioit: Subtitles did not need to be updated.");
  }
}

// Schedule to run the code every so often
setInterval(checkSubtitles, repeatInterval); // 1000 milliseconds = 1 second; 10000 milliseconds = 10 second;

window.addEventListener('load', ChangeSubtitles() );

function ChangeSubtitles() {
    'use strict';

    // Delay function to add a short delay between clicks
    function delay(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }

    // Find the gear icon
    const gearIcon = document.querySelector('.ytp-settings-button');

    // Click the gear icon to open the menu
    gearIcon.click();

    // Wait for the menu to open
    setTimeout(() => {
        // Find the third menu item
        const menuItems = document.querySelectorAll('.ytp-menuitem-label span');
        let thirdMenuItem;

        for (let j = 0; j < menuItems.length; j++) {
            const menuItem = menuItems[j].closest('.ytp-menuitem');
            if (menuItem && menuItem.querySelector('.ytp-menuitem-label span:first-child').textContent.trim() === 'Subtitles/CC') {
                thirdMenuItem = menuItem;
                break;
            }
        }

        // Click the third menu item
        thirdMenuItem.click();

        // Wait for the "Subtitles/CC" menu to open
        setTimeout(() => {
            // Find the "English (auto-generated)" menu item
            const menuLabels = document.querySelectorAll('.ytp-menuitem-label');
            let autoGeneratedMenuItem;

            for (let i = 0; i < menuLabels.length; i++) {
                if (menuLabels[i].textContent.trim() === 'English (auto-generated)') {
                    autoGeneratedMenuItem = menuLabels[i];
                    break;
                }
            }

            // Click the "English (auto-generated)" menu item if found
            if (autoGeneratedMenuItem) {
                autoGeneratedMenuItem.click();
            }
            // Wait for the "English (auto-generated)" option to be selected
            setTimeout(() => {
                const element = document.querySelector('.ytp-settings-menu');
                const computedStyle = getComputedStyle(element);
                if (computedStyle.display !== 'none') {
                    // Click the gear icon to open the menu
                    gearIcon.click();
                }
            }, waitInterval); // Adjust the delay (in milliseconds) if needed
        }, waitInterval); // Adjust the delay (in milliseconds) if needed
    }, waitInterval); // Adjust the delay (in milliseconds) if needed
    console.log("Sapioit: Subtitles changed.");
}