<input type="text" id="id" value="PL07D6B14E58F50900" /> <input type="submit" id="submit" value="Load" /> <h1>Failed</h1> <div id="failed"></div> <h1>Success</h1> <div id="success"></div> <h1>Matches</h1> <div id="content"></div>
var start = 1, index = 0, matches = [], pageToken = '', success = document.getElementById('success'), failed = document.getElementById('failed'), content = document.getElementById('content'); function load(url, callback) { var xhr = new XMLHttpRequest(); xhr.open('GET', url, true); xhr.onreadystatechange = function () { if (xhr.readyState === 4) { if (xhr.status === 200) { callback(JSON.parse(xhr.responseText)); } else { callback([{}]); } } }; xhr.send(); } function loadPage(id) { if (!id) { id = document.getElementById('id').value; } load('https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&key=AIzaSyBxGbqNZL1EXK7aaZWGC4pohdsMcYliy_s&playlistId=' + id + '&pageToken=' + pageToken + '&max-results=50', function (e) { console.log('loadPage', e); if (e.items) { searchSpotify(e.items); } if (e.nextPageToken) { pageToken = e.nextPageToken; } }); } function searchSpotify(items) { var name = items[index].snippet.title || []; name = name.match(/[\w']+/g) .join(' ') .replace(/official/ig, '') .replace(/featuring/ig, '') .replace(/feat/ig, '') .replace(/video/ig, '') .replace(/1080p/ig, '') .replace(/music/ig, ''); load('http://ws.spotify.com/search/1/track.json?q=' + encodeURIComponent(name), function (e) { if (e.tracks && e.tracks[0]) { console.log('success', name, e); // success matches.push(e.tracks[0]); success.innerHTML += name + '<br/>'; content.innerHTML += e.tracks[0].href + '<br/>'; } else { console.log('fail', name, e); // fail failed.innerHTML += name + '<br/>'; } if (index < items.length - 1) { index += 1; searchSpotify(items); } else { console.log(matches); start += 50; index = 0; loadPage(); } }); } document.getElementById('submit').addEventListener('click', function () { index = 0; matches = []; loadPage(); });