// ==UserScript== // @name Youtube 翻译中文字幕下载 v4 // @include https://*youtube.com/* // @author Cheng Zheng // @copyright 2018-2021 Cheng Zheng; // @license GNU GPL v3.0 or later. http://www.gnu.org/copyleft/gpl.html // @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js // @version 4 // @grant GM_xmlhttpRequest // @namespace https://greasyfork.org/users/5711 // @description Youtube 播放器右下角有个 Auto-tranlsate,可以把视频字幕翻成中文。这个脚本是下载这个中文字幕 // @downloadURL none // ==/UserScript== /* 作者 : 郑诚 新浪微博: 糖醋陈皮 https://weibo.com/u/2004104451/home?wvr=5 邮箱 : guokrfans@gmail.com Github: https://github.com/1c7/Youtube-Auto-Subtitle-Download 测试视频: https://www.youtube.com/watch?v=nGlQkaoIfBI 1门语言 https://www.youtube.com/watch?v=O5nskjZ_GoI 13门语言 https://www.youtube.com/watch?v=VfEz3DIbkvo */ // CONFIG var NO_SUBTITLE = '无字幕'; var HAVE_SUBTITLE = '下载翻译的中文字幕'; var first_load = true; // return true / false // Detect [new version UI(material design)] OR [old version UI] // I tested this, accurated. function new_material_design_version() { var old_title_element = document.getElementById('watch7-headline'); if (old_title_element) { return false; } else { return true; } } // trigger when first load (hit refresh button) $(document).ready(function () { // because document ready still not enough // it's still too early, we have to wait certain element exist, then execute function. if (new_material_design_version()) { var material_checkExist = setInterval(function () { if (document.querySelectorAll('.title.style-scope.ytd-video-primary-info-renderer').length) { init(); clearInterval(material_checkExist); } }, 330); } else { var checkExist = setInterval(function () { if ($('#watch7-headline').length) { init(); clearInterval(checkExist); } }, 330); } }); // trigger when loading new page (actually this would also trigger when first loading, that's not what we want, that's why we need to use firsr_load === false) // (new Material design version would trigger this "yt-navigate-finish" event. old version would not.) var body = document.getElementsByTagName("body")[0]; body.addEventListener("yt-navigate-finish", function (event) { if (first_load === false) { remove_subtitle_download_button(); init(); } }); // trigger when loading new page // (old version would trigger this "spfdone" event. new Material design version not sure yet.) window.addEventListener("spfdone", function (e) { if (current_page_is_video_page()) { remove_subtitle_download_button(); var checkExist = setInterval(function () { if ($('#watch7-headline').length) { init(); clearInterval(checkExist); } }, 330); } }); // return true / false function current_page_is_video_page() { return get_video_id() !== null; } // return string like "RW1ChiWyiZQ", from "https://www.youtube.com/watch?v=RW1ChiWyiZQ" // or null function get_video_id() { return getURLParameter('v'); } //https://stackoverflow.com/questions/11582512/how-to-get-url-parameters-with-javascript/11582513#11582513 function getURLParameter(name) { return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [null, ''])[1].replace(/\+/g, '%20')) || null; } function remove_subtitle_download_button() { $('#youtube-subtitle-downloader-by-1c7').remove(); } function init() { unsafeWindow.caption_array = []; inject_our_script(); first_load = false; } function inject_our_script() { var div = document.createElement('div'), select = document.createElement('select'), option = document.createElement('option'), controls = document.getElementById('watch7-headline'); // Youtube video title DIV if (new_material_design_version()) { div.setAttribute('style', `display: table; margin-top:4px; border: 1px solid rgb(0, 183, 90); cursor: pointer; color: rgb(255, 255, 255); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: #00B75A; padding: 4px; padding-right: 8px; `); } else { div.setAttribute('style', `display: table; margin-top:4px; border: 1px solid rgb(0, 183, 90); cursor: pointer; color: rgb(255, 255, 255); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: #00B75A; padding: 3px; padding-right: 8px; `); } div.id = 'youtube-subtitle-downloader-by-1c7'; select.id = 'captions_selector'; select.disabled = true; select.setAttribute('style', 'display:block; border: 1px solid rgb(0, 183, 90); cursor: pointer; color: rgb(255, 255, 255); background-color: #00B75A;'); option.textContent = '载入中...'; option.selected = true; select.appendChild(option); // 下拉菜单中选择后的事件侦听 select.addEventListener('change', function () { download_subtitle(this); }, false); div.appendChild(select); // put 里 function load_language_list(select) { // auto var auto_subtitle_exist = false; // closed var closed_subtitle_exist = false; var captions = null; // get auto subtitle var auto_subtitle_url = get_auto_subtitle_xml_url(); if (auto_subtitle_url != false) { auto_subtitle_exist = true; // console.log('自动字幕的确存在'); } else { // console.log('自动字幕不存在'); // console.log(auto_subtitle_url); } // get closed subtitle var list_url = 'https://video.google.com/timedtext?v=' + get_video_id() + '&type=list&hl=zh-CN'; // https://video.google.com/timedtext?v=if36bqHypqk&type=list&hl=en // 英文 // https://video.google.com/timedtext?v=n1zpnN-6pZQ&type=list&hl=zh-CN // 中文 GM_xmlhttpRequest({ method: 'GET', url: list_url, onload: function (xhr) { captions = new DOMParser().parseFromString(xhr.responseText, "text/xml").getElementsByTagName('track'); if (captions.length != 0) { closed_subtitle_exist = true; } // if no subtitle at all, just say no and stop if (auto_subtitle_exist == false && closed_subtitle_exist == false) { select.options[0].textContent = NO_SUBTITLE; disable_download_button(); return false; } // if at least one type of subtitle exist select.options[0].textContent = HAVE_SUBTITLE; select.disabled = false; // if at least one type of subtitle exist select.options[0].textContent = HAVE_SUBTITLE; select.disabled = false; var caption = null; // for inside loop var option = null; // for