Advertisement
renix1

choque de cultura

Oct 15th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Example of usage
  2. https://www.googleapis.com/youtube/v3/search?part=snippet&q=Choque+de+Cultura&type=video&key={YOUR_KEY}
  3. */
  4. const apiKey = "YOUR KEY"; // Youtube Data Search v3 api key
  5. var listItems = document.querySelectorAll('.lolomoRow_title_card');
  6.  
  7. for (let i=0; i<listItems.length; i++) {
  8.     let items = [].slice.call(listItems[i].querySelectorAll('.slider-item'));
  9.     items = items.slice(0, 5);
  10.     items.forEach((item) => {
  11.     try {
  12.         // Request to put button
  13.         let titleCard = item.querySelector('.title-card').querySelector('a');
  14.         let term = titleCard.getAttribute('aria-label') + " choque de cultura";
  15.         let url = encodeURI(`https://www.googleapis.com/youtube/v3/search?part=snippet&q=${term}&type=video&key=${apiKey}`);
  16.         let xhr = new XMLHttpRequest();
  17.         xhr.open("GET", url);
  18.         xhr.responseType = "json";
  19.         xhr.onload = function() {
  20.             if (xhr.readyState == XMLHttpRequest.DONE) {
  21.                 let jsonResponse = xhr.response;
  22.                 let itemsYoutube = jsonResponse.items;
  23.                 for (let i=0; i<itemsYoutube.length; i++) {
  24.                     if ((itemsYoutube[i].snippet.channelTitle == "omeleteve") || (itemsYoutube[i].snippet.channelTitle == "Globo") || (itemsYoutube[i].snippet.channelTitle == "TV Quase")) {
  25.                         let videoId = itemsYoutube[i].id.videoId;
  26.                         choqueUrl = `https://www.youtube.com/watch?v=${videoId}`;
  27.                         console.log(choqueUrl);
  28.                         // Create a button
  29.                         /*
  30.                             SOLVE:
  31.                                 When search for movie/serie no delete the button element
  32.                         */
  33.                         let btn = document.createElement('btn');
  34.                         let link = document.createElement('a');
  35.                         let textNode = document.createTextNode(itemsYoutube[i].snippet.title);
  36.                         link.appendChild(textNode);
  37.                         link.href = choqueUrl;
  38.                         link.title = itemsYoutube[i].snippet.title;
  39.                         btn.appendChild(link);
  40.                         item.querySelector('.title-card').appendChild(btn);
  41.                         break;
  42.                     }
  43.                 }
  44.             }
  45.         }
  46.         xhr.send();
  47.     } catch (e) {
  48.         if (e instanceof TypeError) {}
  49.         else { throw new Error(e); }
  50.     }
  51. });
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement