Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Example of usage
- https://www.googleapis.com/youtube/v3/search?part=snippet&q=Choque+de+Cultura&type=video&key={YOUR_KEY}
- */
- const apiKey = "YOUR KEY"; // Youtube Data Search v3 api key
- var listItems = document.querySelectorAll('.lolomoRow_title_card');
- for (let i=0; i<listItems.length; i++) {
- let items = [].slice.call(listItems[i].querySelectorAll('.slider-item'));
- items = items.slice(0, 5);
- items.forEach((item) => {
- try {
- // Request to put button
- let titleCard = item.querySelector('.title-card').querySelector('a');
- let term = titleCard.getAttribute('aria-label') + " choque de cultura";
- let url = encodeURI(`https://www.googleapis.com/youtube/v3/search?part=snippet&q=${term}&type=video&key=${apiKey}`);
- let xhr = new XMLHttpRequest();
- xhr.open("GET", url);
- xhr.responseType = "json";
- xhr.onload = function() {
- if (xhr.readyState == XMLHttpRequest.DONE) {
- let jsonResponse = xhr.response;
- let itemsYoutube = jsonResponse.items;
- for (let i=0; i<itemsYoutube.length; i++) {
- if ((itemsYoutube[i].snippet.channelTitle == "omeleteve") || (itemsYoutube[i].snippet.channelTitle == "Globo") || (itemsYoutube[i].snippet.channelTitle == "TV Quase")) {
- let videoId = itemsYoutube[i].id.videoId;
- choqueUrl = `https://www.youtube.com/watch?v=${videoId}`;
- console.log(choqueUrl);
- // Create a button
- /*
- SOLVE:
- When search for movie/serie no delete the button element
- */
- let btn = document.createElement('btn');
- let link = document.createElement('a');
- let textNode = document.createTextNode(itemsYoutube[i].snippet.title);
- link.appendChild(textNode);
- link.href = choqueUrl;
- link.title = itemsYoutube[i].snippet.title;
- btn.appendChild(link);
- item.querySelector('.title-card').appendChild(btn);
- break;
- }
- }
- }
- }
- xhr.send();
- } catch (e) {
- if (e instanceof TypeError) {}
- else { throw new Error(e); }
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement