Forked from qwertyuiop6/TranslateToChineseOnYouTube.user.js
Created
October 20, 2020 12:42
-
-
Save lucifer88285/b6cffd1d505cfee3cda0465154898a15 to your computer and use it in GitHub Desktop.
Translate to Chinese automatically. youtube自动翻译中文简体
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ==UserScript== | |
| // @name YTB字幕自动翻译->中文简体 | |
| // @namespace http://tampermonkey.net/ | |
| // @version 0.2 | |
| // @description translate to Chinese automatically. 自动点击翻译字幕到中文简体 | |
| // @author qwertyuiop6 | |
| // @match https://www.youtube.com/watch* | |
| // @grant none | |
| // ==/UserScript== | |
| (function(){ | |
| const $=document.querySelector.bind(document); | |
| const $$=document.querySelectorAll.bind(document); | |
| let video=$('video'); | |
| let subtitlesButton=$('.ytp-subtitles-button[aria-pressed="false"]') | |
| let settingsButton=$('.ytp-settings-button'); | |
| function checkAndClick(){ | |
| let sub=findElem($$('[role="menuitem"]'),"字幕"); | |
| if (!sub) return false; | |
| sub.click(); | |
| let subc = findElem($$('[role="menuitemradio"]'),"中文"); | |
| if (subc) { | |
| subc.click(); | |
| settingsButton.click(); | |
| } else { | |
| let autoTrans = findElem($$('[role="menuitemradio"]'),"自动翻译"); | |
| if (!autoTrans) return false; | |
| autoTrans.click(); | |
| let autoTransC = findElem($$('[role="menuitemradio"]'),"中文(简体)"); | |
| if (!autoTransC) return false; | |
| autoTransC.click(); | |
| } | |
| return true; | |
| } | |
| function findElem(elems,text) { | |
| for (let node of elems) { | |
| if (node.textContent.startsWith(text)) { | |
| return node | |
| } | |
| } | |
| return null | |
| } | |
| function clickToTranslate(){ | |
| if (subtitlesButton) subtitlesButton.click(); | |
| settingsButton.click(); | |
| if(!checkAndClick()) settingsButton.click(); | |
| } | |
| video.addEventListener('loadstart',clickToTranslate) | |
| window.addEventListener('load', ()=>{ | |
| video.dispatchEvent(new Event('loadstart')) | |
| }) | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment