Last active
September 4, 2024 18:25
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
captions
contains a list of all captions for the YouTube video currently open in your browser window