JS-151-API
  • JavaScript Library
  • 02-DOM-Manipulation
    • Overview
    • DOM Explained
    • Example Setup
    • Selecting Elements
    • Events
    • Complete Example
  • 03-API Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-Intro-To-APIs
      • Intro
      • Client
      • Requests
      • JSON
      • API Endpoints
      • Server
      • Response
      • Statelessness
      • REST
    • 2-Asynchronous-Programming
      • Intro
      • Callbacks
      • Promises
      • Promises Continued
      • Chaining Promises
    • 3-Fetch
      • Fetch Intro
      • Star Wars
        • Star Wars API
        • Star Wars Setup
        • Star Wars JS
      • Random Photo
        • Unsplash
        • Unsplash Setup
        • Unsplash JS
    • 4-Apps
      • 01-New York Times
        • 00-App Intro
        • 01-HTML/CSS/API Key
        • 02-Variables
        • 03-Event Listeners
        • 04-fetchResults
          • 01-fetchResults()
          • 02-preventDefault()
          • 03-fetch() method
          • 04-Dates
        • 05-displayResults
          • 01-Logging JSON
          • 02-Working with JSON
          • 03-Link Display
          • 04-Results Navigation
          • 05-Results Update
          • 06-Keywords
          • 07-Images
        • 06-Pagination
          • 01-Pagination Intro
          • 02-Pagination Functions
        • 07-Next Steps
      • 02-YouTube
        • html
        • youtube.css
        • youtube.js
      • 03-GoogleMaps
        • Setup
        • HTML and CSS files
        • API Key
        • JS Setup
        • Adding Your Location
        • Listeners
        • Custom Options
Powered by GitBook
On this page
  1. 03-API Fundamentals
  2. 4-Apps
  3. 02-YouTube

youtube.js

const searchTerm = document.querySelector('.search');
const searchForm = document.querySelector('form');
const submitBtn = document.querySelector('.submit');

const section = document.querySelector('section');

window.onload = onClientLoad;

function onClientLoad() {
    gapi.client.load('youtube', 'v3', onYouTubeApiLoad)
}

function onYouTubeApiLoad() {
    gapi.client.setApiKey('AIzaSyDjtFukI0pv7QB2lyz98HbRIeDJK7OeRHI');
    searchForm.addEventListener('submit', search);
}

function search(e) {
    e.preventDefault();

    let request = gapi.client.youtube.search.list({
        part: 'snippet',
        maxResults: 10,
        q: searchTerm.value
    });

    request.execute(onSearchResponse);
}

function onSearchResponse(response) {
    while (section.firstChild) {
        section.removeChild(section.firstChild);
    }

    let results = response.items;

    for(let i = 0; i < results.length; i++) {
        displayVideo(results[i], i);
    }
}

function displayVideo(result, i) {
    let vid = document.createElement('div');
    vidId = 'vid' + i;
    vid.id = vidId;
    section.appendChild(vid);

    let player = new YT.Player(vidId, {
        height: '360',
        width: '480',
        videoId: result.id.videoId,
        events: {
            'onReady': onPlayerReady
        }
    });

    function onPlayerReady(e) {
        let myId = e.target.a.id;
        let duration = e.target.getDuration();
        if(duration === 0) {
            console.log('Video ' + myId + ' cannot be played, so it was deleted.');
            section.removeChild(e.target.a);
        } else {
            console.log('Video ' + myId + ' ready to play.');
        }
    }
}
Previousyoutube.cssNext03-GoogleMaps

Last updated 7 years ago