diff --git a/popup.html b/popup.html index b180d788..99f7b099 100644 --- a/popup.html +++ b/popup.html @@ -41,8 +41,26 @@

Record the times of a sponsorship

-

+ + +

@@ -103,5 +121,8 @@ + + + \ No newline at end of file diff --git a/popup.js b/popup.js index 36f6e2d2..47f2d156 100644 --- a/popup.js +++ b/popup.js @@ -41,17 +41,49 @@ chrome.storage.sync.get(["hideVideoPlayerControls"], function(result) { //get the amount of times this user has contributed and display it to thank them chrome.storage.sync.get(["sponsorTimesContributed"], function(result) { if (result.sponsorTimesContributed != undefined) { + let sponsorTimesContributionsContainer = document.getElementById("sponsorTimesContributionsContainer"); let sponsorTimesContributionsDisplay = document.getElementById("sponsorTimesContributionsDisplay"); + let sponsorTimesContributionsDisplayEndWord = document.getElementById("sponsorTimesContributionsDisplayEndWord"); if (result.sponsorTimesContributed > 1) { - sponsorTimesContributionsDisplay.innerText = "So far, you've submitted " + result.sponsorTimesContributed + " sponsor times."; + sponsorTimesContributionsDisplayEndWord.innerText = "sponsors." } else { - sponsorTimesContributionsDisplay.innerText = "So far, you've submitted " + result.sponsorTimesContributed + " sponsor time."; + sponsorTimesContributionsDisplayEndWord.innerText = "sponsor." } - sponsorTimesContributionsDisplay.style.display = "unset"; + sponsorTimesContributionsDisplay.innerText = result.sponsorTimesContributed; + sponsorTimesContributionsContainer.style.display = "unset"; + + //get the userID + chrome.storage.sync.get(["userID"], function(result) { + let userID = result.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) { + let sponsorTimesViewsContainer = document.getElementById("sponsorTimesViewsContainer"); + let sponsorTimesViewsDisplay = document.getElementById("sponsorTimesViewsDisplay"); + let sponsorTimesViewsDisplayEndWord = document.getElementById("sponsorTimesViewsDisplayEndWord"); + + if (viewCount > 1) { + sponsorTimesViewsDisplayEndWord.innerText = "sponsor segments." + } else { + sponsorTimesViewsDisplayEndWord.innerText = "sponsor segment." + } + sponsorTimesViewsDisplay.innerText = viewCount; + sponsorTimesViewsContainer.style.display = "unset"; + } + } + }); + } + }); } }); + chrome.tabs.query({ active: true, currentWindow: true @@ -466,6 +498,25 @@ function getFormattedTime(seconds) { return formatted; } +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) { // Return video id or false var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/; var match = url.match(regExp);