-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyoutube-cc-lang.user.js
105 lines (93 loc) · 3.07 KB
/
youtube-cc-lang.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// ==UserScript==
// @name YouTube subtitles lang attribute
// @namespace https://github.com/lilydjwg/userscripts
// @description set YouTube subtitles lang attribute
// @match https://www.youtube.com/*
// @version 0.4.1
// @grant window.onurlchange
// ==/UserScript==
(function() {
'use strict'
const ZH_TW_CHANNELS = ['PanSci 泛科学', '舞秋風', '歷史衛視 History Channel']
let observer
const run = function(el) {
if(observer) {
observer.disconnect()
}
observer = new MutationObserver((mutationList, observer) => {
mutationList.forEach((mutation) => {
const name = mutation.target.textContent
const cc = document.getElementById('ytp-caption-window-container')
if(!cc) {
return;
}
console.log('ytcc: 字幕', mutation.target.textContent)
if(name.includes('台湾') || name.includes('繁体')) {
cc.lang = 'zh-TW'
}else if(name.includes('日语')) {
cc.lang = 'ja'
}else if(name.includes('韩语')) {
cc.lang = 'ko'
}else{
const channel = document.getElementById('text-container').textContent.trim()
console.log('ytcc: channel', channel)
if(ZH_TW_CHANNELS.includes(channel)) {
cc.lang = 'zh-TW'
}else{
cc.removeAttribute('lang')
}
}
})
})
console.log('ytcc: observing')
observer.observe(el, { childList: true })
}
let btn_clicked = false
let retry_times = 3
const start = function() {
console.log('ytcc: start')
if(location.href.indexOf('https://www.youtube.com/watch?') != 0) {
console.log('ytcc: not watching, returning')
return;
}
const button = document.querySelector('.ytp-popup.ytp-settings-menu')
if(!button) {
console.log('ytcc: no button, retry after 1000')
setTimeout(start, 1000)
}
// const el = document.querySelector('.ytp-popup.ytp-settings-menu .ytp-menuitem:nth-child(2) .ytp-menuitem-content')
const r = document.evaluate('//div[@class="ytp-popup ytp-settings-menu"]//span[text()="字幕"]/parent::div/parent::div/parent::div/div[@class="ytp-menuitem-content"]', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null)
if(r.snapshotLength == 0) {
if(!btn_clicked) {
const settings_btn = document.querySelector('.ytp-settings-button')
if(settings_btn) {
settings_btn.click()
btn_clicked = true
}
}
if(retry_times > 0) {
console.log('ytcc: no subtitles button, retry after 500')
retry_times -= 1
setTimeout(start, 500)
} else {
console.log('ytcc: no subtitles, giving up.')
}
} else {
document.querySelector('.ytp-settings-button').click()
run(r.snapshotItem(0))
}
}
start()
// https://www.tampermonkey.net/documentation.php#api:window.onurlchange
if(window.onurlchange === null) {
// feature is supported
window.addEventListener('urlchange', (info) => {
console.log('ytcc: restart on urlchange', info)
if(info.url.indexOf('https://www.youtube.com/watch?') == 0) {
btn_clicked = false
retry_times = 3
start()
}
})
}
})()