Skip to content

Instantly share code, notes, and snippets.

@RohanAwhad
Created March 31, 2024 19:45
Show Gist options
  • Save RohanAwhad/61f486ab91499dc56400b6973514b5b5 to your computer and use it in GitHub Desktop.
Save RohanAwhad/61f486ab91499dc56400b6973514b5b5 to your computer and use it in GitHub Desktop.
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function clickShowTranscriptButton() {
// Selecting the button based on its class and aria-label
var buttons = document.querySelectorAll(
"button.yt-spec-button-shape-next.yt-spec-button-shape-next--outline.yt-spec-button-shape-next--call-to-action.yt-spec-button-shape-next--size-m"
);
// Filtering buttons to find the one with the "Show transcript" aria-label
var showTranscriptButton = Array.from(buttons).find(
(button) => button.getAttribute("aria-label") === "Show transcript"
);
if (showTranscriptButton) {
showTranscriptButton.click();
} else {
console.log("Show transcript button not found");
}
}
// JavaScript code to get the video title without using the id attribute
function getVideoTitle() {
// Selects the <yt-formatted-string> element that is a child of an <h1> tag with class "style-scope ytd-watch-metadata"
const titleElement = document.querySelector(
"h1.style-scope.ytd-watch-metadata > yt-formatted-string.style-scope.ytd-watch-metadata"
);
// Returns the text content of the title element, if found
return titleElement ? titleElement.textContent : "Title not found";
}
// Function to download the text content of transcript segments as a file
function downloadTranscriptSegments() {
const transcriptCollection = document.querySelectorAll(
"ytd-transcript-renderer ytd-transcript-segment-list-renderer yt-formatted-string.segment-text"
);
// Concatenate all segments into a single string
let transcriptText = "";
transcriptCollection.forEach((segment) => {
transcriptText += segment.innerText + "\n\n";
});
// Create a blob with the transcript text
const transcriptBlob = new Blob([transcriptText], { type: "text/plain" });
// Create an anchor element for the download
const downloadLink = document.createElement("a");
downloadLink.href = URL.createObjectURL(transcriptBlob);
downloadLink.download = getVideoTitle() + ".txt"; // Name of the file to be downloaded
// Append the anchor to the body, trigger the download, and then remove the anchor
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
const main = async function () {
document
.getElementById("description-inner")
.getElementsByTagName("tp-yt-paper-button")[2]
.click();
await sleep(1000);
clickShowTranscriptButton();
await sleep(1000);
downloadTranscriptSegments();
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment