From 4a491f1ebf0702125eca60aea42dc21b2832bc0e Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Sat, 1 Feb 2020 16:41:08 -0500 Subject: Moved server code to utils --- src/background.ts | 25 ++------- src/popup.ts | 148 +++++++++++++++++++++++------------------------------- src/utils.ts | 20 ++++++++ 3 files changed, 87 insertions(+), 106 deletions(-) diff --git a/src/background.ts b/src/background.ts index cd259b7d..29f63c51 100644 --- a/src/background.ts +++ b/src/background.ts @@ -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(); -} +} \ No newline at end of file diff --git a/src/popup.ts b/src/popup.ts index 2c1b2ac7..e5e0bfc2 100644 --- a/src/popup.ts +++ b/src/popup.ts @@ -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"); } - } - }); - //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.sponsorTimesViewsDisplay.innerText = viewCount; + OptionsElements.sponsorTimesViewsContainer.style.display = "unset"; + } + } + }); - 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; - - 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.setUsernameStatusContainer.style.display = "unset"; - OptionsElements.setUsernameStatus.innerText = Utils.getErrorMessage(xmlhttp.status); - } - }); + //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.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); + } + }); } //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"; - - OptionsElements.setUsernameStatus.innerText = chrome.i18n.getMessage("success"); - } else if (xmlhttp.readyState == 4) { - OptionsElements.setUsernameStatus.innerText = Utils.getErrorMessage(xmlhttp.status); - } - }); + 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.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 diff --git a/src/utils.ts b/src/utils.ts index 912e7cd0..838e1996 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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) */ -- cgit v1.2.3