Switched all xmlhttp requests to use new dedicated function.

This commit is contained in:
Ajay Ramachandran
2019-07-20 15:33:52 -04:00
parent 4cc44258e8
commit 91f0b65d06
2 changed files with 46 additions and 31 deletions

View File

@@ -87,7 +87,7 @@ function addSponsorTime(time) {
function submitVote(type, UUID, callback) { function submitVote(type, UUID, callback) {
getUserID(function(userID) { getUserID(function(userID) {
//publish this vote //publish this vote
sendRequestToUser('GET', "/api/voteOnSponsorTime?UUID=" + UUID + "&userID=" + userID + "&type=" + type, function(xmlhttp, error) { sendRequestToServer('GET', "/api/voteOnSponsorTime?UUID=" + UUID + "&userID=" + userID + "&type=" + type, function(xmlhttp, error) {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
callback({ callback({
successType: 1 successType: 1
@@ -116,13 +116,10 @@ function submitTimes(videoID) {
if (sponsorTimes != undefined && sponsorTimes.length > 0) { if (sponsorTimes != undefined && sponsorTimes.length > 0) {
//submit these times //submit these times
for (let i = 0; i < sponsorTimes.length; i++) { for (let i = 0; i < sponsorTimes.length; i++) {
let xmlhttp = new XMLHttpRequest(); getUserID(function(userIDStorage) {
let userIDStorage = getUserID(function(userIDStorage) {
//submit the sponsorTime //submit the sponsorTime
xmlhttp.open('GET', serverAddress + "/api/postVideoSponsorTimes?videoID=" + videoID + "&startTime=" + sponsorTimes[i][0] + "&endTime=" + sponsorTimes[i][1] sendRequestToServer('GET', "/api/postVideoSponsorTimes?videoID=" + videoID + "&startTime=" + sponsorTimes[i][0] + "&endTime=" + sponsorTimes[i][1]
+ "&userID=" + userIDStorage, true); + "&userID=" + userIDStorage);
xmlhttp.send();
}); });
} }
} }
@@ -179,11 +176,12 @@ function getUserID(callback) {
}); });
} }
function sendRequestToUser(type, address, callback) { function sendRequestToServer(type, address, callback) {
let xmlhttp = new XMLHttpRequest(); let xmlhttp = new XMLHttpRequest();
xmlhttp.open(type, serverAddress + address, true); xmlhttp.open(type, serverAddress + address, true);
if (callback != undefined) {
xmlhttp.onreadystatechange = function () { xmlhttp.onreadystatechange = function () {
callback(xmlhttp, false); callback(xmlhttp, false);
}; };
@@ -191,6 +189,7 @@ function sendRequestToUser(type, address, callback) {
xmlhttp.onerror = function(ev) { xmlhttp.onerror = function(ev) {
callback(xmlhttp, true); callback(xmlhttp, true);
}; };
}
//submit this request //submit this request
xmlhttp.send(); xmlhttp.send();

View File

@@ -118,9 +118,7 @@ function sponsorsLookup(id) {
let xmlhttp = new XMLHttpRequest(); let xmlhttp = new XMLHttpRequest();
//check database for sponsor times //check database for sponsor times
xmlhttp.open('GET', serverAddress + "/api/getVideoSponsorTimes?videoID=" + id, true); sendRequestToServer('GET', "/api/getVideoSponsorTimes?videoID=" + id, function(xmlhttp) {
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
sponsorDataFound = true; sponsorDataFound = true;
@@ -134,8 +132,7 @@ function sponsorsLookup(id) {
} else { } else {
sponsorDataFound = false; sponsorDataFound = false;
} }
}; });
xmlhttp.send(null);
} }
function sponsorCheck(sponsorTimes) { // Video skipping function sponsorCheck(sponsorTimes) { // Video skipping
@@ -463,6 +460,25 @@ function sponsorMessageStarted() {
toggleStartSponsorButton(); toggleStartSponsorButton();
} }
function sendRequestToServer(type, address, callback) {
let xmlhttp = new XMLHttpRequest();
xmlhttp.open(type, serverAddress + address, true);
if (callback != undefined) {
xmlhttp.onreadystatechange = function () {
callback(xmlhttp, false);
};
xmlhttp.onerror = function(ev) {
callback(xmlhttp, true);
};
}
//submit this request
xmlhttp.send();
}
function getYouTubeVideoID(url) { // Returns with video id else returns false function getYouTubeVideoID(url) { // Returns with video id else returns false
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/; var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp); var match = url.match(regExp);