skip to Main Content

I’m trying to extract youtube video’s titles but nothing works. I’m working on a chrome extension and i need to export the video’s title.

Here’s my code so far:

// Function to extract the song information from the YouTube page
function extractSongInfo() {
    // Extract the video title from the YouTube page's DOM
    const titleElement = document.querySelector('.title.ytd-video-primary-info-renderer');
    const videoTitle = titleElement ? titleElement.innerText : '';

    // Extract the artist information from the YouTube page's DOM
    const artistElement = document.querySelector('.ytp-subtitles-button');
    const artist = artistElement ? artistElement.getAttribute('aria-label') : '';

    // Send the song information to the background script
    chrome.runtime.sendMessage({ type: 'SONG_INFO', title: videoTitle, artist });
}

// Call the function to extract song information
extractSongInfo();

I tried the code below

2

Answers


  1. iniside youtube video page :

    this is the video description:
    document.querySelector('#description-inline-expander').innerText

    and the title is :
    document.querySelector('#below #title h1').innerText

    Login or Signup to reply.
  2. You can also get it as follows:

    document.title
    

    And replace "- YouTube" from it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search