diff --git a/background.js b/background.js index c619bcbb..ec126b20 100644 --- a/background.js +++ b/background.js @@ -42,7 +42,10 @@ chrome.runtime.onMessage.addListener(function (request, sender, callback) { //this allows the callback to be called later return true; } else if (request.message == "submitVote") { - submitVote(request.type, request.UUID) + submitVote(request.type, request.UUID, callback); + + //this allows the callback to be called later + return true; } }); @@ -81,16 +84,21 @@ function addSponsorTime(time) { }); } -function submitVote(type, UUID) { - let xmlhttp = new XMLHttpRequest(); - +function submitVote(type, UUID, callback) { getUserID(function(userID) { //publish this vote - console.log(serverAddress + "/api/voteOnSponsorTime?UUID=" + UUID + "&userID=" + userID + "&type=" + type); - xmlhttp.open('GET', serverAddress + "/api/voteOnSponsorTime?UUID=" + UUID + "&userID=" + userID + "&type=" + type, true); - - //submit this vote - xmlhttp.send(); + sendRequestToUser('GET', "/api/voteOnSponsorTime?UUID=" + UUID + "&userID=" + userID + "&type=" + type, function(xmlhttp) { + if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { + callback({ + successType: 1 + }); + } else if (xmlhttp.readyState == 4 && xmlhttp.status == 405) { + //duplicate vote + callback({ + successType: 0 + }); + } + }) }) } @@ -166,6 +174,19 @@ function getUserID(callback) { }); } +function sendRequestToUser(type, address, callback) { + let xmlhttp = new XMLHttpRequest(); + + xmlhttp.open(type, serverAddress + address, true); + + xmlhttp.onreadystatechange = function () { + callback(xmlhttp); + }; + + //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); diff --git a/content.css b/content.css index 3c5bfbda..763213ba 100644 --- a/content.css +++ b/content.css @@ -58,6 +58,13 @@ text-align: center; } +#sponsorTimesErrorMessage { + font-size: 15px; + font-weight: bold; + color: #000000; + text-align: center; +} + #sponsorTimesThanksForVotingInfoText { font-size: 12px; font-weight: bold; diff --git a/content.js b/content.js index 83d4735b..2bea7df8 100644 --- a/content.js +++ b/content.js @@ -286,13 +286,13 @@ function openSkipNotice(){ upvoteButton.id = "sponsorTimesUpvoteButtonsContainer" + lastSponsorTimeSkippedUUID; upvoteButton.className = "sponsorSkipObject voteButton"; upvoteButton.src = chrome.extension.getURL("icons/upvote.png"); - upvoteButton.addEventListener("click", () => upvote(UUID)); + upvoteButton.addEventListener("click", () => vote(1, UUID)); let downvoteButton = document.createElement("img"); downvoteButton.id = "sponsorTimesDownvoteButtonsContainer" + lastSponsorTimeSkippedUUID; downvoteButton.className = "sponsorSkipObject voteButton"; downvoteButton.src = chrome.extension.getURL("icons/downvote.png"); - downvoteButton.addEventListener("click", () => downvote(UUID)); + downvoteButton.addEventListener("click", () => vote(0, UUID)); //add thumbs up and down buttons to the container voteButtonsContainer.appendChild(upvoteButton); @@ -336,15 +336,7 @@ function openSkipNotice(){ referenceNode.prepend(noticeElement); } -function upvote(UUID) { - vote(1, UUID); - - closeSkipNotice(UUID); -} - -function downvote(UUID) { - vote(0, UUID); - +function afterDownvote(UUID) { //change text to say thanks for voting //remove buttons document.getElementById("sponsorTimesVoteButtonsContainer" + UUID).removeChild(document.getElementById("sponsorTimesUpvoteButtonsContainer" + UUID)); @@ -365,11 +357,41 @@ function downvote(UUID) { document.getElementById("sponsorTimesVoteButtonsContainer" + UUID).appendChild(thanksForVotingInfoText); } +function votingError(message, UUID) { + //change text to say thanks for voting + //remove buttons + document.getElementById("sponsorTimesVoteButtonsContainer" + UUID).removeChild(document.getElementById("sponsorTimesUpvoteButtonsContainer" + UUID)); + document.getElementById("sponsorTimesVoteButtonsContainer" + UUID).removeChild(document.getElementById("sponsorTimesDownvoteButtonsContainer" + UUID)); + + //add thanks for voting text + let thanksForVotingText = document.createElement("p"); + thanksForVotingText.id = "sponsorTimesErrorMessage"; + thanksForVotingText.innerText = message; + + //add element to div + document.getElementById("sponsorTimesVoteButtonsContainer" + UUID).appendChild(thanksForVotingText); +} + function vote(type, UUID) { chrome.runtime.sendMessage({ message: "submitVote", type: type, UUID: UUID + }, function(response) { + if (response != undefined) { + //see if it was a success or failure + if (response.successType == 1) { + //success + if (type == 0) { + afterDownvote(UUID); + } else if (type == 1) { + closeSkipNotice(UUID); + } + } else if (response.successType == 0) { + //failure: duplicate vote + votingError("It seems you've already voted before", UUID) + } + } }); } diff --git a/popup.js b/popup.js index 29deaf66..cf4d54cf 100644 --- a/popup.js +++ b/popup.js @@ -370,7 +370,7 @@ function reportAnIssue() { document.getElementById("reportAnIssue").style.display = "none"; } -function vote(type, UUID) { +function addVoteMessage(message, UUID) { let container = document.getElementById("sponsorTimesVoteButtonsContainer" + UUID); //remove all children while (container.firstChild) { @@ -378,17 +378,30 @@ function vote(type, UUID) { } let thanksForVotingText = document.createElement("h2"); - thanksForVotingText.innerText = "Thanks for voting!"; + thanksForVotingText.innerText = message; //there are already breaks there thanksForVotingText.style.marginBottom = "0px"; container.appendChild(thanksForVotingText); +} +function vote(type, UUID) { //send the vote message to the tab chrome.runtime.sendMessage({ message: "submitVote", type: type, UUID: UUID + }, function(response) { + if (response != undefined) { + //see if it was a success or failure + if (response.successType == 1) { + //success + addVoteMessage("Thanks for voting!", UUID) + } else if (response.successType == 0) { + //failure: duplicate vote + addVoteMessage("You have already voted this way before.", UUID) + } + } }); }