// ==UserScript== // @name Youtube 下载自动字幕 (字词级) v6 // @include https://*youtube.com/* // @author Cheng Zheng // @require https://code.jquery.com/jquery-1.12.4.min.js // @version 6 // @grant GM_xmlhttpRequest // @namespace https://greasyfork.org/users/5711 // @description (下载 .json 文件)字词级字幕仅适用于自动字幕(也就是机器用语音转文字识别出来的字幕)(完整字幕没有字词级的)下载字词级的意义是方便分句。可下载两种格式:原版 (&fmt=json3 从 Youtube 获取的原样返回) 和简化版 {startTime: "开始时间(毫秒)", endTime: "结束时间(毫秒)", text: "文字"}。 json 格式不可配合视频直接播放,需要其他软件进行进一步处理(把词拼成句子,转成 srt 格式) // @license MIT // @downloadURL https://update.greasyfork.icu/scripts/422083/Youtube%20%E4%B8%8B%E8%BD%BD%E8%87%AA%E5%8A%A8%E5%AD%97%E5%B9%95%20%28%E5%AD%97%E8%AF%8D%E7%BA%A7%29%20v6.user.js // @updateURL https://update.greasyfork.icu/scripts/422083/Youtube%20%E4%B8%8B%E8%BD%BD%E8%87%AA%E5%8A%A8%E5%AD%97%E5%B9%95%20%28%E5%AD%97%E8%AF%8D%E7%BA%A7%29%20v6.meta.js // ==/UserScript== (function () { // 可配置项 var NO_SUBTITLE = '无自动字幕'; var HAVE_SUBTITLE = '下载自动字幕 (字词级)'; var TEXT_LOADING = '加载中...'; const BUTTON_ID = 'youtube-download-word-level-subtitle-last-update-2021-2-21' // 可配置项 var HASH_BUTTON_ID = `#${BUTTON_ID}` // 初始化 var first_load = true; // indicate if first load this webpage or not var youtube_playerResponse_1c7 = null; // for auto subtitle unsafeWindow.caption_array = []; // store all subtitle $(document).ready(function () { make_sure_it_load_properly_before_continue(); }); async function wait_until_element_exists(element_identifier) { var retry_count = 0; var RETRY_LIMIT = 30; return new Promise(function (resolve, reject) { var intervalID = setInterval(function () { try { var element = document.querySelector(element_identifier); if (element != null) { resolve(true); } else { retry_count = retry_count + 1; // console.log(`重试次数 ${retry_count}`); if (retry_count > RETRY_LIMIT) { clearInterval(intervalID); reject(false); } } } catch (error) { reject(false); } }, 330); }); } async function make_sure_it_load_properly_before_continue() { var id = new_Youtube_2022_UI_element_identifier(); var result = await wait_until_element_exists(id); if (result) { init_UI(); } } // 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 (current_page_is_video_page() === false) { return; } youtube_playerResponse_1c7 = event.detail.response.playerResponse; // for auto subtitle unsafeWindow.caption_array = []; // clean up (important, otherwise would have more and more item and cause error) // if use click to another page, init again to get correct subtitle if (first_load === false) { remove_subtitle_download_button(); init_UI(); } }); // trigger when loading new page // (old version would trigger "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_UI(); clearInterval(checkExist); } }, 330); } }); // 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; } } // return true / false function current_page_is_video_page() { return get_url_video_id() !== null; } // return string like "RW1ChiWyiZQ", from "https://www.youtube.com/watch?v=RW1ChiWyiZQ" // or null function get_url_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() { $(HASH_BUTTON_ID).remove(); } // 初始化 function init_UI() { var html_element = get_main_UI_element(); var old_anchor_element = document.getElementById("watch7-headline"); if (old_anchor_element != null) { old_anchor_element.appendChild(html_element); } var anchor = document.querySelector('#above-the-fold') if (anchor) { anchor.appendChild(html_element); } first_load = false; } function get_main_UI_element() { var div = document.createElement('div'), select = document.createElement('select'), option = document.createElement('option'); var css_div = `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; `; div.setAttribute('style', css_div); div.id = BUTTON_ID; select.id = 'captions_selector'; select.disabled = true; let css_select = `display:block; border: 1px solid rgb(0, 183, 90); cursor: pointer; color: rgb(255, 255, 255); background-color: #00B75A; padding: 4px; `; select.setAttribute('style', css_select); option.textContent = TEXT_LOADING; option.selected = true; select.appendChild(option); // 下拉菜单里,选择一项后触发下载 select.addEventListener('change', function () { download_subtitle(this); }, false); div.appendChild(select); // put function load_language_list(select) { var auto_subtitle_exist = false; // get auto subtitle var auto_subtitle_url = get_auto_subtitle_xml_url(); if (auto_subtitle_url != false) { auto_subtitle_exist = true; } // if no subtitle at all, just say no and stop if (auto_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; var option = null; // for