自动英文字幕

show english subtitles automatically.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         自动英文字幕
// @namespace    http://elmagnifico.tech/
// @version      1.5
// @description  show english subtitles automatically.
// @author       elmagnifico
// @match        https://www.youtube.com/watch*
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function checkChinese(){
        var sub = $('[role="menuitem"]:contains("字幕")');
        if(!sub.length) return false;
        // show subtitles
        sub.click();
        console.log("打开设置");

        var subc = $('[role="menuitemradio"]:contains("中文")');
        if (subc.length) {
            console.log("原片是中文,退出");
            console.log("关闭字幕");
            var close_btn = $('[role="menuitemradio"]:contains("关闭")');
            if (!close_btn.length) return true;
            close_btn.click();
            return true;
        }else{
            console.log("没有中文");
        }
        return false;
    }

    function translateToEnglish(){
        var sub = $('[role="menuitem"]:contains("字幕")');
        if(!sub.length) return false;
        // show subtitles
        sub.click();
        console.log("打开设置");
        var success = false;

        var subc = $('[role="menuitemradio"]:contains("英语")');
        if (subc.length) {
            console.log("切换到英语(美国)字幕");
            subc.click();
            success = true;
        } else {
            console.log("关闭字幕1");
            var close_btn = $('[role="menuitemradio"]:contains("关闭")');
            if (!close_btn.length) return false;
            close_btn.click();
        }

        if(success == false)
        {
            subc = $('[role="menuitemradio"]:contains("英语 (自动生成)")');
            if (subc.length) {
                console.log("切换到英语(自动生成)字幕");
                subc.click();
                success = true;
            } else {
                console.log("关闭字幕2");
                close_btn = $('[role="menuitemradio"]:contains("关闭")');
                if (!close_btn.length) return false;
                close_btn.click();
            }
        }

        if(success == false)
        {
            subc = $('[role="menuitemradio"]:contains("英语")');
            if (subc.length) {
                console.log("切换到英语字幕");
                subc.click();
            } else {
                console.log("关闭字幕3");
                close_btn = $('[role="menuitemradio"]:contains("关闭")');
                if (!close_btn.length) return false;
                close_btn.click();
            }
        }
    }

    function onLoadStart(){
        $('.ytp-subtitles-button[aria-pressed="false"]').click();
        $('.ytp-settings-button').click();
        if(checkChinese() == false)
        {
            $('.ytp-settings-button').click();
            translateToEnglish();
            $('.ytp-settings-button').click();
        }
        console.log("关闭设置");
        $('.ytp-settings-button').click();
    }
    $('video').on('loadstart', onLoadStart).trigger('loadstart');
})();