Skip to content

Instantly share code, notes, and snippets.

@LasseWolter
Last active August 29, 2025 14:28
Show Gist options
  • Select an option

  • Save LasseWolter/66153133403d522cb9c980850428ea68 to your computer and use it in GitHub Desktop.

Select an option

Save LasseWolter/66153133403d522cb9c980850428ea68 to your computer and use it in GitHub Desktop.
Simple js script to fetch the auto-generated youtube captions for the video currently open in your browser window
function decodeUnicodeEscapeSequence(str) {
return str
.replace(/\\u/g, "%u")
.replace(/(%u)([a-fA-F\d]{4})/gi, function(_, _, hex) {
return String.fromCharCode(parseInt(hex, 16));
});
}
function parseXML(xmlStr) {
// Create a new DOMParser object
var parser = new DOMParser();
// Parse the XML string into a DOM Document
var doc = parser.parseFromString(xmlStr, "application/xml");
// Find all <text> elements in the document
var texts = Array.from(doc.querySelectorAll("text"));
// Extract the text content of each <text> element and store it in an array
var textContents = texts.map((textElement) => textElement.textContent.trim());
return textContents;
}
// Fetch url for auto-generated captions from youtube
async function getCaptionUrl() {
const response = await fetch(window.location);
rawHtml = await response.text();
let matches = rawHtml.match(
new RegExp('(?<=captionTracks.*baseUrl":")[^"]+"', "g"),
);
if (!matches) {
return;
}
var decodedUrl = decodeUnicodeEscapeSequence(matches[0].replace('"', ""));
return decodedUrl;
}
async function fetchCaptions() {
const url = await getCaptionUrl();
console.log(url);
const response = await fetch(url);
rawXml = await response.text();
var captions = parseXML(rawXml);
console.log(`No of Captions: ${captions.length}`);
return captions;
}
var captions = await fetchCaptions();
@LasseWolter
Copy link
Author

This one also seems to work at the moment:
https://github.com/ericmmartin/youtube-transcript-plus

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