diff --git a/background.js b/background.js index 4feb48bd..c7bf572b 100644 --- a/background.js +++ b/background.js @@ -87,7 +87,7 @@ function addSponsorTime(time) { function submitVote(type, UUID, callback) { getUserID(function(userID) { //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) { callback({ successType: 1 @@ -116,13 +116,10 @@ function submitTimes(videoID) { if (sponsorTimes != undefined && sponsorTimes.length > 0) { //submit these times for (let i = 0; i < sponsorTimes.length; i++) { - let xmlhttp = new XMLHttpRequest(); - - let userIDStorage = getUserID(function(userIDStorage) { + getUserID(function(userIDStorage) { //submit the sponsorTime - xmlhttp.open('GET', serverAddress + "/api/postVideoSponsorTimes?videoID=" + videoID + "&startTime=" + sponsorTimes[i][0] + "&endTime=" + sponsorTimes[i][1] - + "&userID=" + userIDStorage, true); - xmlhttp.send(); + sendRequestToServer('GET', "/api/postVideoSponsorTimes?videoID=" + videoID + "&startTime=" + sponsorTimes[i][0] + "&endTime=" + sponsorTimes[i][1] + + "&userID=" + userIDStorage); }); } } @@ -179,18 +176,20 @@ function getUserID(callback) { }); } -function sendRequestToUser(type, address, callback) { +function sendRequestToServer(type, address, callback) { let xmlhttp = new XMLHttpRequest(); xmlhttp.open(type, serverAddress + address, true); - xmlhttp.onreadystatechange = function () { - callback(xmlhttp, false); - }; - - xmlhttp.onerror = function(ev) { - callback(xmlhttp, true); - }; + if (callback != undefined) { + xmlhttp.onreadystatechange = function () { + callback(xmlhttp, false); + }; + + xmlhttp.onerror = function(ev) { + callback(xmlhttp, true); + }; + } //submit this request xmlhttp.send(); diff --git a/content.js b/content.js index 9203acc6..cafd9db7 100644 --- a/content.js +++ b/content.js @@ -118,24 +118,21 @@ function sponsorsLookup(id) { let xmlhttp = new XMLHttpRequest(); //check database for sponsor times - xmlhttp.open('GET', serverAddress + "/api/getVideoSponsorTimes?videoID=" + id, true); + sendRequestToServer('GET', "/api/getVideoSponsorTimes?videoID=" + id, function(xmlhttp) { + if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { + sponsorDataFound = true; - xmlhttp.onreadystatechange = function () { - if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { - sponsorDataFound = true; + sponsorTimes = JSON.parse(xmlhttp.responseText).sponsorTimes; + UUIDs = JSON.parse(xmlhttp.responseText).UUIDs; - sponsorTimes = JSON.parse(xmlhttp.responseText).sponsorTimes; - UUIDs = JSON.parse(xmlhttp.responseText).UUIDs; - - // If the sponsor data exists, add the event to run on the videos "ontimeupdate" - v.ontimeupdate = function () { - sponsorCheck(sponsorTimes); - }; - } else { - sponsorDataFound = false; - } - }; - xmlhttp.send(null); + // If the sponsor data exists, add the event to run on the videos "ontimeupdate" + v.ontimeupdate = function () { + sponsorCheck(sponsorTimes); + }; + } else { + sponsorDataFound = false; + } + }); } function sponsorCheck(sponsorTimes) { // Video skipping @@ -463,6 +460,25 @@ function sponsorMessageStarted() { 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 var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/; var match = url.match(regExp);