File size: 13,528 Bytes
8671ce8 | 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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 | // youtube-transcript page logic
// 1. ask this site's own endpoint for the caption tracks YouTube issued for the video
// 2. YouTube only hands the caption text to a browser that can produce a
// proof-of-origin token, so the visitor's own browser mints one and fetches
// the caption directly from YouTube - no file, no account, no third party.
const API = "/api/youtube-transcript";
const REQUEST_KEY = "O43z0dpjhgX20SCx4KAo";
const CVER = "2.20260916.01.00";
const $ = (id) => document.getElementById(id);
const els = {
form: $("ytForm"),
input: $("urlInput"),
button: $("getBtn"),
hint: $("panelHint"),
status: $("statusBox"),
statusText: $("statusText"),
error: $("errorBox"),
section: $("transcriptSection"),
output: $("transcriptOutput"),
meta: $("videoMeta"),
trackRow: $("trackRow"),
copy: $("copyBtn"),
txt: $("downloadTxtBtn"),
srt: $("downloadSrtBtn"),
tsToggle: $("tsToggle"),
plainToggle: $("plainToggle"),
};
let state = {
segments: [],
text: "",
title: "",
videoId: "",
mode: "timestamps",
};
function show(el) {
el.classList.remove("hidden");
}
function hide(el) {
el.classList.add("hidden");
}
function setStatus(msg) {
els.statusText.textContent = msg;
show(els.status);
}
function fail(msg) {
els.error.textContent = msg;
show(els.error);
hide(els.status);
}
function formatTime(seconds) {
const total = Math.max(0, Math.floor(seconds));
const h = Math.floor(total / 3600);
const m = Math.floor((total % 3600) / 60);
const s = total % 60;
const pad = (n) => String(n).padStart(2, "0");
return h > 0 ? `${h}:${pad(m)}:${pad(s)}` : `${pad(m)}:${pad(s)}`;
}
function render() {
if (!state.segments.length) return;
if (state.mode === "timestamps") {
els.output.innerHTML = "";
for (const seg of state.segments) {
const p = document.createElement("p");
const stamp = document.createElement("span");
stamp.className = "timestamp";
stamp.textContent = `[${formatTime(seg.start)}] `;
p.appendChild(stamp);
p.appendChild(document.createTextNode(seg.text));
els.output.appendChild(p);
}
} else {
els.output.innerHTML = "";
const p = document.createElement("p");
p.textContent = state.segments.map((s) => s.text).join(" ");
els.output.appendChild(p);
}
show(els.section);
}
function toSrt(segments) {
const pad = (n) => String(n).padStart(2, "0");
const stamp = (t) => {
const ms = Math.max(0, Math.round(t * 1000));
const h = Math.floor(ms / 3600000);
const m = Math.floor((ms % 3600000) / 60000);
const s = Math.floor((ms % 60000) / 1000);
return `${pad(h)}:${pad(m)}:${pad(s)},${String(ms % 1000).padStart(3, "0")}`;
};
return segments
.map((seg, i) => {
const start = seg.start;
const end = seg.start + (seg.duration || 2);
return `${i + 1}\n${stamp(start)} --> ${stamp(end)}\n${seg.text}\n`;
})
.join("\n");
}
function download(filename, content) {
const blob = new Blob([content], { type: "text/plain;charset=utf-8" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
a.remove();
setTimeout(() => URL.revokeObjectURL(url), 1000);
}
function baseName() {
const clean = (state.title || state.videoId || "youtube-transcript")
.replace(/[^\w\s-]/g, "")
.trim()
.replace(/\s+/g, "-")
.slice(0, 60);
return clean || "youtube-transcript";
}
/* ---------- proof-of-origin token (minted in this browser) ---------- */
let potLoader = null;
function loadPotLibrary() {
if (!potLoader) {
potLoader = new Promise((resolve, reject) => {
const s = document.createElement("script");
s.src = "/js/yt-pot.js";
s.onload = () => (window.YTPOT ? resolve(window.YTPOT) : reject(new Error("library did not load")));
s.onerror = () => reject(new Error("could not load the token helper"));
document.head.appendChild(s);
});
}
return potLoader;
}
async function mintPot(videoId) {
const Y = await loadPotLibrary();
const fetchFn = window.fetch.bind(window);
const challenge = await Y.getChallenge({
requestKey: REQUEST_KEY,
fetchFunction: fetchFn,
useYouTubeAPI: false,
});
const ij = challenge.interpreterJavascript;
if (ij && ij.privateDoNotAccessOrElseSafeScriptWrappedValue) {
new Function(ij.privateDoNotAccessOrElseSafeScriptWrappedValue)();
} else if (challenge.interpreterUrl && challenge.interpreterUrl.privateDoNotAccessOrElseTrustedResourceUrlWrappedValue) {
await new Promise((resolve, reject) => {
const s = document.createElement("script");
s.src = challenge.interpreterUrl.privateDoNotAccessOrElseTrustedResourceUrlWrappedValue;
s.onload = resolve;
s.onerror = () => reject(new Error("could not load YouTube's checker"));
document.head.appendChild(s);
});
}
const client = await Y.BotGuardClient.create({
program: challenge.program,
globalName: challenge.globalName,
globalObject: window,
});
const webPoSignalOutput = [];
const snapshot = await client.snapshot({ webPoSignalOutput });
const itResponse = await fetchFn(Y.buildURL("GenerateIT", false), {
method: "POST",
headers: Y.getHeaders(),
body: JSON.stringify([REQUEST_KEY, snapshot]),
});
if (!itResponse.ok) throw new Error("YouTube refused the browser check");
const itJson = await itResponse.json();
const integrityToken = Array.isArray(itJson) ? itJson[0] : null;
if (!integrityToken) throw new Error("no integrity token");
const minter = await Y.WebPoMinter.create({ integrityToken }, webPoSignalOutput);
const pot = await minter.mintAsWebsafeString(videoId);
if (!pot) throw new Error("no token minted");
return pot;
}
function withTimeout(promise, ms, message) {
return Promise.race([
promise,
new Promise((_, reject) => setTimeout(() => reject(new Error(message)), ms)),
]);
}
/* ---------- caption fetch ---------- */
function clientParams() {
const ua = navigator.userAgent;
const isMobile = /Android|iPhone|iPad|iPod/i.test(ua);
let browser = "Chrome";
let version = "152";
if (/Edg\//.test(ua)) browser = "Edge";
else if (/OPR\//.test(ua)) browser = "Opera";
else if (/Firefox\//.test(ua)) browser = "Firefox";
else if (/Chrome\//.test(ua)) browser = "Chrome";
else if (/Safari\//.test(ua)) browser = "Safari";
const m = ua.match(/(?:Chrome|Edg|OPR|Firefox|Version)\/(\d+)/);
if (m) version = m[1];
let os = "Windows";
let osVersion = "10.0";
if (/Android (\d+(\.\d+)*)/.test(ua)) {
os = "Android";
osVersion = ua.match(/Android (\d+(\.\d+)*)/)[1];
} else if (/iPhone|iPad|iPod/.test(ua)) {
os = "iOS";
const im = ua.match(/OS (\d+)_/);
osVersion = im ? im[1] + ".0" : "17.0";
} else if (/Mac OS X (\d+[._]\d+)/.test(ua)) {
os = "Macintosh";
osVersion = ua.match(/Mac OS X (\d+[._]\d+)/)[1].replace("_", ".");
} else if (/Linux/.test(ua)) {
os = "Linux";
}
const p = new URLSearchParams();
p.set("potc", "1");
p.set("c", "WEB");
p.set("cver", CVER);
p.set("cbr", browser);
p.set("cbrver", version + ".0.0.0");
p.set("cplayer", "UNIPLAYER");
p.set("cos", os);
p.set("cosver", osVersion);
p.set("cplatform", isMobile ? "MOBILE" : "DESKTOP");
p.set("xorb", "2");
p.set("xobt", "3");
p.set("xovt", "3");
return p.toString();
}
function decodeEntities(s) {
return s
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, '"')
.replace(/�?39;/g, "'")
.replace(/&#(\d+);/g, (_, d) => String.fromCodePoint(Number(d)));
}
function parseJson3(raw) {
const data = JSON.parse(raw);
const out = [];
for (const ev of data.events || []) {
if (!ev.segs) continue;
const text = decodeEntities(
ev.segs
.map((s) => (s && s.utf8 ? s.utf8 : ""))
.join("")
.replace(/\n/g, " ")
.trim()
);
if (!text) continue;
out.push({ start: (ev.tStartMs || 0) / 1000, duration: (ev.dDurationMs || 0) / 1000, text });
}
return out;
}
function pickTrack(tracks) {
if (!tracks || !tracks.length) return null;
const select = $("langSelect");
const wanted = select && select.value;
if (wanted) {
const hit = tracks.find((t) => t.languageCode === wanted);
if (hit) return hit;
}
return tracks[0];
}
async function run(event) {
event.preventDefault();
hide(els.error);
hide(els.section);
els.button.disabled = true;
const url = els.input.value.trim();
if (!url) {
els.button.disabled = false;
fail("Paste a YouTube link first.");
return;
}
setStatus("Looking up the caption tracks for this video...");
let meta;
try {
const response = await fetch(API + "?v=" + encodeURIComponent(url));
meta = await response.json();
} catch (err) {
els.button.disabled = false;
fail("Could not reach this site's caption service. Check your connection and try again.");
return;
}
if (!meta.ok) {
els.button.disabled = false;
fail(meta.message || "This video has no caption track we can read.");
return;
}
state.videoId = meta.videoId;
state.title = meta.title || "";
const select = $("langSelect");
if (select && meta.tracks && meta.tracks.length > 1) {
// Rebuilding the option list wipes the current value, so remember the
// language the visitor already picked and put it back. Without this the
// second lookup quietly fell back to the first track - usually English.
const previous = select.value;
select.innerHTML = meta.tracks
.map(
(t) =>
`<option value="${t.languageCode}">${t.languageName}${t.isGenerated ? " (auto)" : ""}</option>`
)
.join("");
if (previous && Array.from(select.options).some((o) => o.value === previous)) {
select.value = previous;
}
show(els.trackRow);
} else if (select) {
hide(els.trackRow);
}
if (meta.segments && meta.segments.length) {
state.segments = meta.segments;
finish(meta);
return;
}
const track = pickTrack(meta.tracks);
if (!track) {
els.button.disabled = false;
fail("This video has no caption track we can read.");
return;
}
setStatus("Asking your browser to pass YouTube's bot check...");
let pot;
try {
pot = await withTimeout(mintPot(meta.videoId), 20000, "the browser check took too long");
} catch (err) {
els.button.disabled = false;
fail(
"YouTube asked this browser for a proof-of-origin token and it could not be produced (" +
err.message +
"). A strict privacy setting or an extension that blocks Google scripts usually causes this. Try another browser or turn the blocker off for this page."
);
return;
}
setStatus("Reading the caption track...");
let raw = "";
try {
const target = track.url + "&" + clientParams() + "&pot=" + encodeURIComponent(pot);
const response = await fetch(target);
raw = await response.text();
} catch (err) {
els.button.disabled = false;
fail("YouTube refused the caption request from this page. Try again in a moment.");
return;
}
let segments = [];
try {
segments = parseJson3(raw);
} catch (err) {
segments = [];
}
if (!segments.length) {
els.button.disabled = false;
fail(
"This video has a caption track, but YouTube sent back nothing for it. That happens when the captions were switched off after publishing, when auto captions have not been generated yet, or when the track is empty."
);
return;
}
state.segments = segments;
finish(meta, track);
}
function finish(meta, track) {
els.button.disabled = false;
hide(els.status);
const bits = [];
if (meta.title) bits.push(meta.title);
if (meta.author) bits.push(meta.author);
if (meta.lengthSeconds) bits.push(formatTime(meta.lengthSeconds));
bits.push(`${state.segments.length} lines`);
if (track) bits.push(track.languageName + (track.isGenerated ? " (auto-generated)" : ""));
els.meta.textContent = bits.join(" · ");
show(els.meta);
render();
}
els.form.addEventListener("submit", run);
els.tsToggle.addEventListener("click", () => {
state.mode = "timestamps";
els.tsToggle.classList.add("active");
els.plainToggle.classList.remove("active");
render();
});
els.plainToggle.addEventListener("click", () => {
state.mode = "plain";
els.plainToggle.classList.add("active");
els.tsToggle.classList.remove("active");
render();
});
els.copy.addEventListener("click", async () => {
const text =
state.mode === "timestamps"
? state.segments.map((s) => `[${formatTime(s.start)}] ${s.text}`).join("\n")
: state.segments.map((s) => s.text).join(" ");
try {
await navigator.clipboard.writeText(text);
els.copy.classList.add("copied");
els.copy.textContent = "Copied";
setTimeout(() => {
els.copy.classList.remove("copied");
els.copy.innerHTML = "Copy";
}, 1500);
} catch (err) {
fail("Your browser blocked the clipboard. Select the text and copy it manually.");
}
});
els.txt.addEventListener("click", () => {
const text =
state.mode === "timestamps"
? state.segments.map((s) => `[${formatTime(s.start)}] ${s.text}`).join("\n")
: state.segments.map((s) => s.text).join(" ");
download(baseName() + ".txt", text);
});
els.srt.addEventListener("click", () => {
download(baseName() + ".srt", toSrt(state.segments));
});
|