3

When viewing a video, and the video's up-loader added/uploaded it as part of a playlist. From the video page, how can I easily reach the entire parent playlist for said video?

Some Notes

The Next Up video does show the next video in the playlist correctly, but I want see the entire "list" of videos in the playlist.

When navigating to a video from a playlist, it retains the playlist for easy navigation.

I can navigate to the up-loader's page, then click on Playlists, then search for the playlist, but this is cumbersome.

2
  • In case this feature exists in YouTube it would probably need to display a list of all playlists including the video. A single video could belong to several playlists. I'm not sure YouTube would consider any one of those playlists to be more important than the others.
    – cyberixae
    Commented Sep 10, 2023 at 12:19
  • 1
    I understand that, that's why I specifically mentioned up-loader's playlist.
    – mxmissile
    Commented Sep 11, 2023 at 13:55

1 Answer 1

1

I wanted to do this myself. The following Javascript code, when run in the browser console of a Youtube video page, grabs the title and channel name, submits a search to Youtube with those elements, and parses out the top resulting playlist ID. A more robust solution would use the Youtube API but I didn't want to bother with setting and storing API keys here.

(Caveat: Seems to have trouble with excessively complex video titles)

function captureYouTubeData() {
    let videoTitleElement = document.querySelector('h1.style-scope.ytd-watch-metadata yt-formatted-string.style-scope.ytd-watch-metadata');
    let videoTitle = videoTitleElement ? videoTitleElement.innerText : null;

    let channelNameElement = document.querySelector('yt-formatted-string.ytd-channel-name');
    let channelName = channelNameElement ? channelNameElement.innerText : null;

    let data = {
        "videoTitle": videoTitle,
        "channelName": channelName
    };

    return data;
}

async function findFirstPlaylistId(videoTitle, channelOwner) {
    // Constructing the search URL
    const baseUrl = 'https://www.youtube.com/results';
    const query = encodeURIComponent(`"${channelOwner}" "${videoTitle}"`);
    const searchUrl = `${baseUrl}?search_query=${query}`;

    // Fetch the content
    const response = await fetch(searchUrl);
    const htmlText = await response.text();

    // Extract ytInitialData from the HTML
    const ytInitialDataRegex = /var ytInitialData = (.*?);\s*<\/script>/s;
    const match = ytInitialDataRegex.exec(htmlText);
    
    if (!match) {
        console.error("Failed to extract ytInitialData");
        return null;
    }

    const ytInitialData = JSON.parse(match[1]);

    // Recursive function to extract the playlistId
    function findPlaylistId(data) {
        if (data && data.playlistRenderer && data.playlistRenderer.playlistId && data.playlistRenderer.playlistId.length > 2) {
            return data.playlistRenderer.playlistId;
        }

        if (typeof data === 'object') {
            for (let key in data) {
                const result = findPlaylistId(data[key]);
                if (result) return result;
            }
        }

        return null;
    }

    return findPlaylistId(ytInitialData);
}

let videoData = captureYouTubeData();
findFirstPlaylistId(videoData.videoTitle, videoData.channelName).then(id => console.log('Playlist: ', id));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.