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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
// ==UserScript==
// @name Auto-Live-TL YouTube Client
// @namespace https://example.com/
// @version 1.0
// @description Auto Translate Live Subtitles
// @author pinapelz
// @match https://www.youtube.com/*
// @grant none
// @run-at document-idle
// ==/UserScript==
(function () {
"use strict";
const PANEL_ID = "altl-subtitle-panel";
const SUBTITLE_TEXT_ID = "altl-subtitle-text";
const FOOTER_TEXT_ID = "altl-footer-text";
const EVENTS_URL = "http://127.0.0.1:5000/events";
let eventSource = null;
let isConnected = false;
let lastError = null;
let reconnectTimer = null;
const MAX_SUBTITLE_LINES = 3;
const recentLines = [];
function getPrimary() {
return document.querySelector("#columns #primary");
}
function getPlayerNode(primary) {
return (
primary?.querySelector("#player") ||
primary?.querySelector("ytd-player") ||
primary?.querySelector("#movie_player")?.closest("#player") ||
primary?.querySelector("#movie_player") ||
null
);
}
function getTitleAnchor(primary) {
return (
primary?.querySelector("ytd-watch-metadata") ||
primary?.querySelector("#title h1")?.closest("ytd-watch-metadata") ||
primary?.querySelector("#below ytd-watch-metadata") ||
primary?.querySelector("#below")?.firstElementChild ||
null
);
}
function ensurePanel() {
let panel = document.getElementById(PANEL_ID);
if (!panel) {
panel = document.createElement("div");
panel.id = PANEL_ID;
const subtitleText = document.createElement("div");
subtitleText.id = SUBTITLE_TEXT_ID;
subtitleText.textContent = "This is a sample subtitle.";
const footerText = document.createElement("div");
footerText.id = FOOTER_TEXT_ID;
footerText.textContent = "Machine Translated - no translation should be taken as authoritative or quoted verbatim";
footerText.style.marginTop = "8px";
footerText.style.fontSize = "12px";
footerText.style.opacity = "0.7";
footerText.style.textAlign = "center";
panel.appendChild(subtitleText);
panel.appendChild(footerText);
panel.style.display = "block";
panel.style.boxSizing = "border-box";
panel.style.width = "100%";
panel.style.margin = "8px 0 12px";
panel.style.padding = "10px 14px";
panel.style.borderRadius = "10px";
panel.style.background = "rgba(255,255,255,0.04)";
panel.style.border = "1px solid rgba(255,255,255,0.12)";
panel.style.color = "var(--yt-spec-text-primary, #f1f1f1)";
panel.style.fontSize = "16px";
panel.style.lineHeight = "1.45";
panel.style.fontWeight = "500";
panel.style.textAlign = "center";
panel.style.whiteSpace = "pre-wrap";
panel.style.wordBreak = "break-word";
}
return panel;
}
function getSubtitleNode(panel) {
return panel?.querySelector(`#${SUBTITLE_TEXT_ID}`) || null;
}
function setSubtitleText(panel, text) {
const node = getSubtitleNode(panel);
if (node) node.textContent = text;
}
function renderSubtitleHistory(panel) {
const node = getSubtitleNode(panel);
if (!node) return;
node.textContent = "";
recentLines.forEach((line, index) => {
const row = document.createElement("div");
row.textContent = line;
const opacity = Math.max(0.35, 1 - index * 0.15);
row.style.opacity = String(opacity);
if (index > 0) row.style.filter = "grayscale(1)";
node.appendChild(row);
});
}
function updateSubtitleHistory(panel, text) {
if (!text) return;
if (recentLines.length > 0 && recentLines[0] === text) {
renderSubtitleHistory(panel);
return;
}
recentLines.unshift(text);
if (recentLines.length > MAX_SUBTITLE_LINES) {
recentLines.splice(MAX_SUBTITLE_LINES);
}
renderSubtitleHistory(panel);
}
function safePlaceBefore(targetNode, nodeToPlace) {
if (!targetNode || !targetNode.parentNode || !targetNode.isConnected) return false;
const parent = targetNode.parentNode;
if (nodeToPlace.parentNode !== parent || nodeToPlace.nextSibling !== targetNode) {
parent.insertBefore(nodeToPlace, targetNode);
}
return true;
}
function safePlaceAfter(targetNode, nodeToPlace) {
if (!targetNode || !targetNode.parentNode || !targetNode.isConnected) return false;
const parent = targetNode.parentNode;
const desiredNext = targetNode.nextSibling;
if (nodeToPlace.parentNode !== parent || nodeToPlace.previousSibling !== targetNode) {
parent.insertBefore(nodeToPlace, desiredNext);
}
return true;
}
function connectEventSource(panel) {
if (eventSource) {
eventSource.close();
eventSource = null;
}
setSubtitleText(panel, "Connecting to local subtitle server...");
const source = new EventSource(EVENTS_URL);
eventSource = source;
source.addEventListener("subtitle", (event) => {
try {
const data = JSON.parse(event.data || "{}");
updateSubtitleHistory(panel, data.text || "");
} catch (err) {
lastError = err;
console.warn("Auto-Live-TL: failed to parse subtitle event", err);
}
});
source.onopen = () => {
isConnected = true;
if (recentLines.length > 0) {
renderSubtitleHistory(panel);
} else {
setSubtitleText(panel, "Connected. Waiting for subtitles...");
}
};
source.onerror = () => {
isConnected = false;
setSubtitleText(panel, "Connection lost. Reconnecting...");
if (eventSource) {
eventSource.close();
eventSource = null;
}
if (!reconnectTimer) {
reconnectTimer = setTimeout(() => {
reconnectTimer = null;
connectEventSource(panel);
}, 2000);
}
};
}
function startListeningIfNeeded(panel) {
if (isConnected || eventSource) return;
if (!location.pathname.startsWith("/watch")) return;
connectEventSource(panel);
}
function injectSubtitlePanel() {
if (!location.pathname.startsWith("/watch")) return false;
const primary = getPrimary();
if (!primary) return false;
const panel = ensurePanel();
startListeningIfNeeded(panel);
const titleAnchor = getTitleAnchor(primary);
if (safePlaceBefore(titleAnchor, panel)) return true;
const playerNode = getPlayerNode(primary);
if (safePlaceAfter(playerNode, panel)) return true;
return false;
}
injectSubtitlePanel();
const observer = new MutationObserver(() => {
injectSubtitlePanel();
});
observer.observe(document.documentElement, {
childList: true,
subtree: true,
});
window.addEventListener("yt-navigate-finish", injectSubtitlePanel);
})();
|