// ==UserScript== // @name Youtube Subtitle Downloader v15 // @include https://*youtube.com/* // @author Cheng Zheng // @copyright 2009 Tim Smart; 2011 gw111zz; 2013~2018 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 15 // @grant GM_xmlhttpRequest // @namespace https://greasyfork.org/users/5711 // @description download youtube COMPLETE subtitle (v15 fix "underfined" in file name) // @downloadURL none // ==/UserScript== /* Sometime it may not work(rarely), TRY Refresh. if problem still exist. Email me at guokrfans@gmail.com Author : Cheng Zheng Email : guokrfans@gmail.com Github : https://github.com/1c7/Youtube-Auto-Subtitle-Download Some code comments are in Chinese. */ // CONFIG var NO_SUBTITLE = 'No captions.'; var HAVE_SUBTITLE = 'Download captions.'; 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 = 'Loading...'; option.selected = true; select.appendChild(option); select.addEventListener('change', function() { download_subtitle(this); }, false); div.appendChild(select); // put 选回第一个元素. 也就是 Download captions. } // Return something like: "(English)How Did Python Become A Data Science Powerhouse?.srt" function get_file_name(language_name){ return '(' + language_name + ')' + unsafeWindow.ytplayer.config.args.title + '.srt'; } // 载入有多少种语言, 然后加到