Forked from qwertyuiop6/TranslateToChineseOnYouTube.user.js
Created
April 16, 2021 14:42
-
-
Save myesn/1dfea512cbe9327470e4d50760506482 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 YouTube字幕自动翻译->中文简体 | |
// @namespace http://tampermonkey.net/ | |
// @version 1.0 | |
// @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); | |
const video=$('video'); | |
function checkAndClick(){ | |
let sub=findElem($$('[role="menuitem"]'),"字幕"); | |
if (!sub) return false; | |
sub.click(); | |
let subc = findElem($$('[role="menuitemradio"]'),"中文"); | |
if (subc) { | |
subc.click(); | |
video.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(){ | |
$('.ytp-subtitles-button[aria-pressed="false"]')?.click(); | |
const settingsButton=$('.ytp-settings-button'); | |
settingsButton.click(); | |
if(!checkAndClick()) settingsButton.click(); | |
} | |
video.addEventListener('loadstart',clickToTranslate) | |
window.addEventListener('load', ()=>{ | |
$('.ytp-ad-skip-button-icon')?.click(); | |
video.dispatchEvent(new Event('loadstart')) | |
}) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment