From 4a55234951a788f5a07be0e672be64f432f89a20 Mon Sep 17 00:00:00 2001 From: LNFWebsite Date: Thu, 28 Apr 2016 20:34:04 -0400 Subject: [PATCH] brand new failsafe error handling on the ajax calls --- index.js | 98 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 53 insertions(+), 45 deletions(-) diff --git a/index.js b/index.js index 095122b..fca6e7e 100644 --- a/index.js +++ b/index.js @@ -272,23 +272,21 @@ function getPlaylist() { } function getVideoData() { - var xhr = $.ajax({ + var loadingError = false; + $.ajax({ url: videoUrl, type: 'GET', success: function(res) { - var data = $(res.responseText); try { + var data = $(res.responseText); videoName = data.find("span#eow-title"); videoName = videoName[0].textContent; videoName = $("
").html(videoName).text(); videoName = videoName.trim(); + videoName = encodeURIComponent(videoName).replace(/%20/g, " "); } catch(err) { - setTimeout(function() { - getVideoData(); - xhr.abort(); - }, 3000); + loadingError = true; } - videoName = encodeURIComponent(videoName).replace(/%20/g, " "); try { videoTime = null; for (iteration in data) { @@ -301,21 +299,24 @@ function getVideoData() { videoTime = videoTime.replace(/,"length_seconds":"/g, "").replace(/",/g, ""); videoTime = +videoTime * 1000; } catch(err) { + loadingError = true; + } + }, + complete: function(jqXHR, textStatus) { + if (!loadingError) { + autoplayWorking = false; + $("#inputBox").val("").attr("placeholder", placeholder); + addVideo(); + } + else { setTimeout(function() { getVideoData(); - xhr.abort(); }, 3000); } }, - complete: function(jqXHR, textStatus) { - autoplayWorking = false; - $("#inputBox").val("").attr("placeholder", placeholder); - addVideo(); - }, error: function(jqXHR, textStatus, errorThrown) { setTimeout(function() { getVideoData(); - xhr.abort(); }, 3000); } }); @@ -323,42 +324,46 @@ function getVideoData() { function getAutoplayUrl() { highlight(videoIteration, "radio"); - var xhr = $.ajax({ - url: "https://www.youtube.com/watch?v=" + videos[videoIteration][2], - type: 'GET', - success: function(res) { + var loadingError = false; + $.ajax({ + url: "https://www.youtube.com/watch?v=" + videos[videoIteration][2], + type: 'GET', + success: function(res) { + try { var data = res["responseText"]; - try { - var regex = /
  • (?:.|\n)*?href=\"\/watch\?v=(.+?)\"/i; - autoplayMixUrl = data.match(regex); - autoplayMixUrl = $("
    ").html(autoplayMixUrl[1]).text(); - } catch(err) { - setTimeout(function() { - getAutoplayUrl(); - xhr.abort(); - }, 3000); - } - }, - complete: function(jqXHR, textStatus) { + var regex = /
  • (?:.|\n)*?href=\"\/watch\?v=(.+?)\"/i; + autoplayMixUrl = data.match(regex); + autoplayMixUrl = $("
    ").html(autoplayMixUrl[1]).text(); + } catch(err) { + loadingError = true; + } + }, + complete: function(jqXHR, textStatus) { + if (!loadingError) { saveAutoplay(); - return; - }, - error: function(jqXHR, textStatus, errorThrown) { + } + else { setTimeout(function() { getAutoplayUrl(); - xhr.abort(); }, 3000); } - }); + }, + error: function(jqXHR, textStatus, errorThrown) { + setTimeout(function() { + getAutoplayUrl(); + }, 3000); + } + }); } function saveAutoplay() { - var xhr = $.ajax({ + var loadingError = false; + $.ajax({ url: "https://www.youtube.com/watch?v=" + autoplayMixUrl, type: 'GET', success: function(res) { - var data = res["responseText"]; try { + var data = res["responseText"]; var regex = /