Skip to content

Instantly share code, notes, and snippets.

@qwertyuiop6
Last active March 10, 2024 14:44
Show Gist options
  • Save qwertyuiop6/8499fe086491da90617fe17c32716d4e to your computer and use it in GitHub Desktop.
Save qwertyuiop6/8499fe086491da90617fe17c32716d4e to your computer and use it in GitHub Desktop.
🌍 🪄 🀄 Translate to Chinese automatically. Youtube自动点击翻译字幕到中文简体
// ==UserScript==
// @name YouTube字幕自动选择翻译->中文简体
// @namespace http://tampermonkey.net/
// @version 1.2
// @description translate to Chinese automatically. 自动点击字幕翻译到中文简体
// @author qwertyuiop6
// @match https://www.youtube.com/*
// @grant none
// ==/UserScript==
(function(){
const $=document.querySelector.bind(document);
const $$=document.getElementsByClassName.bind(document);
let video;
if(!location.href.includes('watch')){
document.addEventListener('click',e=>{
console.log(e.target)
if(e.target.closest('.ytd-rich-grid-media')){
setTimeout(listenVideo,300)
}
},true)
}else{
listenVideo()
}
function listenVideo(){
video=$('video');
video.addEventListener('canplay', ()=>{
$('.ytp-ad-skip-button-icon')?.click();
})
video.addEventListener('loadeddata',clickToTranslate)
}
function checkAndClick(){
const findElem=findElemByText.bind($$("ytp-menuitem"))
const sub=findElem("字幕");
if (!sub) return false;
sub.click();
const subc = findElem("中文");
if (subc) {
subc.click();
video.click();
} else {
const autoTrans = findElem("自动翻译");
if (!autoTrans) return false;
autoTrans.click();
const autoTransC = findElem("中文(简体)");
if (!autoTransC) return false;
autoTransC.click();
}
return true;
}
function findElemByText(text) {
for (const elem of this) {
if (elem.innerText.includes(text)) {
return elem
}
}
return null
}
function clickToTranslate(){
$('.ytp-subtitles-button[aria-pressed="false"]')?.click();
const settingsButton=$('.ytp-settings-button');
settingsButton.click();
if(!checkAndClick()) settingsButton.click();
}
})();
@huagegeya
Copy link

i

@huagegeya
Copy link

@everlee78
Copy link

不能用

@qwertyuiop6
Copy link
Author

emm 可能是google jq源加载的问题 我来换个国内的

@yezihack
Copy link

非常好用

@Clark-jhx
Copy link

好用

@qianxin12568
Copy link

qianxin12568 commented Jan 4, 2020

...有时候选择器能选到,有时候同一个页面却不行...

@jason1004
Copy link

什么原理,能解释一下吗?

@zermatt-luo
Copy link

顶顶顶

@CatCars
Copy link

CatCars commented May 12, 2020

2020.5.12 测试可用

@qwertyuiop6
Copy link
Author

什么原理,能解释一下吗?

没什么复杂的,就是选择到按钮元素,然后模拟触发点击事件

@dreambgm
Copy link

如果在手机上用x浏览器添加脚本,不切换到桌面模式,也能做到自动翻译吗

@qwertyuiop6
Copy link
Author

如果在手机上用x浏览器添加脚本,不切换到桌面模式,也能做到自动翻译吗

不太清楚,如果移动端youtube 的视频组件dom的css tagname 不变的话 理论上是能用的

@xb315554674
Copy link

额,麻烦问一下,这玩意下载下来怎么用呀。。。。

@wangyuan233
Copy link

好神奇,真的可以自动出中文字幕,

@hoshugon
Copy link

hoshugon commented Mar 19, 2021

很方便,不過可再改進,
目前好像自動開啟字幕,如果可以判斷依使用者是否有開啟字幕,會更方便些,
必竟不是所有的都需要開字幕,很多影片會內嵌字幕,自動打開字幕反而是麻煩,

@Liwenhao-Coder
Copy link

怎么安装呢

@Sucgz
Copy link

Sucgz commented Aug 11, 2021

感谢感谢

@Ayx03
Copy link

Ayx03 commented Aug 18, 2021

2021/8/18 亲测可用

@wangsir5
Copy link

用户脚本无效什么鬼

@ruanbaojun1105
Copy link

没用啊

@CoisiniAKAM
Copy link

2022/2/21,亲测可用!!感谢

@starplatinum3
Copy link

如果没有成功有可能是你的youtube设置是英文的 需要把你的界面设置为中文,因为代码的逻辑是通过找到“字幕” 这样的按钮去点击,如果你的界面设置是英文的话 就找不到这个按钮。我个人是这个原因
设置中文界面的方法 https://acc15t4bm5.feishu.cn/docs/doccn66lNAP0P0zZrSK0YLQ5HWh

@starplatinum3
Copy link

安装方法 知乎评论有说。 youtube ,如何自动加载,翻译中文字幕,插件? - Butterfly的回答 - 知乎
https://www.zhihu.com/question/37871481/answer/492288392 。。 如果你已经安装了油猴插件,那么点击gist 右上角的 raw ,插件会自动加载【1】,然后你只要在那个跳转的界面点击安装就可以了。然后在youtube页面刷新之后就可以有了,他是在window 加载的时候启动的,也就是说是界面刷新之后会去找中文字幕。 【1】原因貌似是油猴插件会自动加载raw形式展示在网页上的js脚本,也就是说你直接通过网页打开一个本地的js文件,可能也是会被油猴检测到的

@starplatinum3
Copy link

好吧 刚才测试了一下 直接打开本地js 文件,貌似不会被油猴监听到。。

@Nicecsh
Copy link

Nicecsh commented May 27, 2022

太好用了,这下在油管上简体中文自由了!!

@fon11731
Copy link

有一些有繁体中文的视频拿不上

@Gilgamesh-lzq
Copy link

如果没有成功有可能是你的youtube设置是英文的 需要把你的界面设置为中文,因为代码的逻辑是通过找到“字幕” 这样的按钮去点击,如果你的界面设置是英文的话 就找不到这个按钮。我个人是这个原因
设置中文界面的方法 https://acc15t4bm5.feishu.cn/docs/doccn66lNAP0P0zZrSK0YLQ5HWh

设置中文界面会被推很多政治方面恶心人的视频,切英文界面再也没出现过。改代码大概也可以吧,无非是把按钮改成英文

@Kira0v0
Copy link

Kira0v0 commented May 25, 2023

油管没有设置默认字幕就离谱,只能找脚本用了,这个真好用

@Gilgamesh-lzq
Copy link

Gilgamesh-lzq commented May 25, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment