Skip to content

Instantly share code, notes, and snippets.

@LasseWolter
Last active September 4, 2024 18:25
Show Gist options
  • Save LasseWolter/66153133403d522cb9c980850428ea68 to your computer and use it in GitHub Desktop.
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

LasseWolter commented Sep 4, 2024

Warning

Do not paste code into your dev console if you don't trust the author/don't understand the code. This can be a security risk.

How to use

  1. Copy the above code
  2. Go to the tab which has a YouTube video open
  3. Open the dev console (F12)
  4. Paste the code you copied in step 1 and hit enter
  5. The variable captions contains a list of all captions for the YouTube video currently open in your browser window

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