Moved server code to utils

This commit is contained in:
Ajay Ramachandran 2020-02-01 16:41:08 -05:00
parent 5ebdfd8466
commit 4a491f1ebf
3 changed files with 83 additions and 102 deletions

View file

@ -153,7 +153,7 @@ function submitVote(type, UUID, callback) {
}
//publish this vote
sendRequestToServer("POST", "/api/voteOnSponsorTime?UUID=" + UUID + "&userID=" + userID + "&type=" + type, function(xmlhttp, error) {
Utils.sendRequestToServer("POST", "/api/voteOnSponsorTime?UUID=" + UUID + "&userID=" + userID + "&type=" + type, function(xmlhttp, error) {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
callback({
successType: 1
@ -205,7 +205,7 @@ async function submitTimes(videoID, callback) {
let increasedContributionAmount = false;
//submit the sponsorTime
sendRequestToServer("GET", "/api/postVideoSponsorTimes?videoID=" + videoID + "&startTime=" + sponsorTimes[i][0] + "&endTime=" + sponsorTimes[i][1]
Utils.sendRequestToServer("GET", "/api/postVideoSponsorTimes?videoID=" + videoID + "&startTime=" + sponsorTimes[i][0] + "&endTime=" + sponsorTimes[i][1]
+ "&userID=" + userID, function(xmlhttp, error) {
if (xmlhttp.readyState == 4 && !error) {
callback({
@ -227,23 +227,4 @@ async function submitTimes(videoID, callback) {
});
}
}
}
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();
}
}

View file

@ -1,4 +1,3 @@
import * as CompileConfig from "../config.json";
import Utils from "./utils";
import SB from "./SB";
@ -170,43 +169,43 @@ async function runThePopup() {
OptionsElements.sponsorTimesContributionsContainer.style.display = "unset";
//get the userID
let userID = SB.config.userID;
if (userID != undefined) {
//there are probably some views on these submissions then
//get the amount of views from the sponsors submitted
sendRequestToServer("GET", "/api/getViewsForUser?userID=" + userID, function(xmlhttp) {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
let viewCount = JSON.parse(xmlhttp.responseText).viewCount;
if (viewCount != 0) {
if (viewCount > 1) {
OptionsElements.sponsorTimesViewsDisplayEndWord.innerText = chrome.i18n.getMessage("Segments");
} else {
OptionsElements.sponsorTimesViewsDisplayEndWord.innerText = chrome.i18n.getMessage("Segment");
}
OptionsElements.sponsorTimesViewsDisplay.innerText = viewCount;
OptionsElements.sponsorTimesViewsContainer.style.display = "unset";
let userID = SB.config.userID;
if (userID != undefined) {
//there are probably some views on these submissions then
//get the amount of views from the sponsors submitted
Utils.sendRequestToServer("GET", "/api/getViewsForUser?userID=" + userID, function(xmlhttp) {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
let viewCount = JSON.parse(xmlhttp.responseText).viewCount;
if (viewCount != 0) {
if (viewCount > 1) {
OptionsElements.sponsorTimesViewsDisplayEndWord.innerText = chrome.i18n.getMessage("Segments");
} else {
OptionsElements.sponsorTimesViewsDisplayEndWord.innerText = chrome.i18n.getMessage("Segment");
}
OptionsElements.sponsorTimesViewsDisplay.innerText = viewCount;
OptionsElements.sponsorTimesViewsContainer.style.display = "unset";
}
});
}
});
//get this time in minutes
sendRequestToServer("GET", "/api/getSavedTimeForUser?userID=" + userID, function(xmlhttp) {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
let minutesSaved = JSON.parse(xmlhttp.responseText).timeSaved;
if (minutesSaved != 0) {
if (minutesSaved != 1) {
OptionsElements.sponsorTimesOthersTimeSavedEndWord.innerText = chrome.i18n.getMessage("minsLower");
} else {
OptionsElements.sponsorTimesOthersTimeSavedEndWord.innerText = chrome.i18n.getMessage("minLower");
}
OptionsElements.sponsorTimesOthersTimeSavedDisplay.innerText = getFormattedHours(minutesSaved);
OptionsElements.sponsorTimesOthersTimeSavedContainer.style.display = "unset";
//get this time in minutes
Utils.sendRequestToServer("GET", "/api/getSavedTimeForUser?userID=" + userID, function(xmlhttp) {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
let minutesSaved = JSON.parse(xmlhttp.responseText).timeSaved;
if (minutesSaved != 0) {
if (minutesSaved != 1) {
OptionsElements.sponsorTimesOthersTimeSavedEndWord.innerText = chrome.i18n.getMessage("minsLower");
} else {
OptionsElements.sponsorTimesOthersTimeSavedEndWord.innerText = chrome.i18n.getMessage("minLower");
}
OptionsElements.sponsorTimesOthersTimeSavedDisplay.innerText = getFormattedHours(minutesSaved);
OptionsElements.sponsorTimesOthersTimeSavedContainer.style.display = "unset";
}
});
}
}
});
}
}
//get the amount of times this user has skipped a sponsor
@ -859,27 +858,27 @@ async function runThePopup() {
//make the options username setting option visible
function setUsernameButton() {
//get username from the server
sendRequestToServer("GET", "/api/getUsername?userID=" + SB.config.userID, function (xmlhttp, error) {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
OptionsElements.usernameInput.value = JSON.parse(xmlhttp.responseText).userName;
//get username from the server
Utils.sendRequestToServer("GET", "/api/getUsername?userID=" + SB.config.userID, function (xmlhttp, error) {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
OptionsElements.usernameInput.value = JSON.parse(xmlhttp.responseText).userName;
OptionsElements.submitUsername.style.display = "unset";
OptionsElements.usernameInput.style.display = "unset";
OptionsElements.submitUsername.style.display = "unset";
OptionsElements.usernameInput.style.display = "unset";
OptionsElements.setUsernameContainer.style.display = "none";
OptionsElements.setUsername.style.display = "unset";
OptionsElements
OptionsElements.setUsernameStatusContainer.style.display = "none";
} else if (xmlhttp.readyState == 4) {
OptionsElements.setUsername.style.display = "unset";
OptionsElements.submitUsername.style.display = "none";
OptionsElements.usernameInput.style.display = "none";
OptionsElements.setUsernameContainer.style.display = "none";
OptionsElements.setUsername.style.display = "unset";
OptionsElements
OptionsElements.setUsernameStatusContainer.style.display = "none";
} else if (xmlhttp.readyState == 4) {
OptionsElements.setUsername.style.display = "unset";
OptionsElements.submitUsername.style.display = "none";
OptionsElements.usernameInput.style.display = "none";
OptionsElements.setUsernameStatusContainer.style.display = "unset";
OptionsElements.setUsernameStatus.innerText = Utils.getErrorMessage(xmlhttp.status);
}
});
OptionsElements.setUsernameStatusContainer.style.display = "unset";
OptionsElements.setUsernameStatus.innerText = Utils.getErrorMessage(xmlhttp.status);
}
});
}
//submit the new username
@ -889,17 +888,17 @@ async function runThePopup() {
OptionsElements.setUsernameStatus.innerText = "Loading...";
//get the userID
sendRequestToServer("POST", "/api/setUsername?userID=" + SB.config.userID + "&username=" + OptionsElements.usernameInput.value, function (xmlhttp, error) {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
//submitted
OptionsElements.submitUsername.style.display = "none";
OptionsElements.usernameInput.style.display = "none";
Utils.sendRequestToServer("POST", "/api/setUsername?userID=" + SB.config.userID + "&username=" + OptionsElements.usernameInput.value, function (xmlhttp, error) {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
//submitted
OptionsElements.submitUsername.style.display = "none";
OptionsElements.usernameInput.style.display = "none";
OptionsElements.setUsernameStatus.innerText = chrome.i18n.getMessage("success");
} else if (xmlhttp.readyState == 4) {
OptionsElements.setUsernameStatus.innerText = Utils.getErrorMessage(xmlhttp.status);
}
});
OptionsElements.setUsernameStatus.innerText = chrome.i18n.getMessage("success");
} else if (xmlhttp.readyState == 4) {
OptionsElements.setUsernameStatus.innerText = Utils.getErrorMessage(xmlhttp.status);
}
});
OptionsElements.setUsernameContainer.style.display = "none";
@ -1106,25 +1105,6 @@ async function runThePopup() {
return secondsFormatted;
}
function sendRequestToServer(type, address, callback) {
let xmlhttp = new XMLHttpRequest();
xmlhttp.open(type, CompileConfig.serverAddress + address, true);
if (callback != undefined) {
xmlhttp.onreadystatechange = function () {
callback(xmlhttp, false);
};
xmlhttp.onerror = function(ev) {
callback(xmlhttp, true);
};
}
//submit this request
xmlhttp.send();
}
/**
* Converts time in hours to 5h 25.1
* If less than 1 hour, just returns minutes

View file

@ -1,3 +1,4 @@
import * as CompileConfig from "../config.json";
import SB from "./SB";
class Utils {
@ -268,6 +269,25 @@ class Utils {
return errorMessage;
}
static sendRequestToServer(type, address, callback) {
let xmlhttp = new XMLHttpRequest();
xmlhttp.open(type, CompileConfig.serverAddress + address, true);
if (callback != undefined) {
xmlhttp.onreadystatechange = function () {
callback(xmlhttp, false);
};
xmlhttp.onerror = function(ev) {
callback(xmlhttp, true);
};
}
//submit this request
xmlhttp.send();
}
/**
* Is this Firefox (web-extensions)
*/