Added error message for a duplicate vote.

This commit is contained in:
Ajay Ramachandran
2019-07-19 22:24:59 -04:00
parent 888a03a708
commit b319729da8
4 changed files with 85 additions and 22 deletions

View File

@@ -42,7 +42,10 @@ chrome.runtime.onMessage.addListener(function (request, sender, callback) {
//this allows the callback to be called later //this allows the callback to be called later
return true; return true;
} else if (request.message == "submitVote") { } 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) { function submitVote(type, UUID, callback) {
let xmlhttp = new XMLHttpRequest();
getUserID(function(userID) { getUserID(function(userID) {
//publish this vote //publish this vote
console.log(serverAddress + "/api/voteOnSponsorTime?UUID=" + UUID + "&userID=" + userID + "&type=" + type); sendRequestToUser('GET', "/api/voteOnSponsorTime?UUID=" + UUID + "&userID=" + userID + "&type=" + type, function(xmlhttp) {
xmlhttp.open('GET', serverAddress + "/api/voteOnSponsorTime?UUID=" + UUID + "&userID=" + userID + "&type=" + type, true); if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
callback({
//submit this vote successType: 1
xmlhttp.send(); });
} 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 function getYouTubeVideoID(url) { // Return video id or false
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/; var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp); var match = url.match(regExp);

View File

@@ -58,6 +58,13 @@
text-align: center; text-align: center;
} }
#sponsorTimesErrorMessage {
font-size: 15px;
font-weight: bold;
color: #000000;
text-align: center;
}
#sponsorTimesThanksForVotingInfoText { #sponsorTimesThanksForVotingInfoText {
font-size: 12px; font-size: 12px;
font-weight: bold; font-weight: bold;

View File

@@ -286,13 +286,13 @@ function openSkipNotice(){
upvoteButton.id = "sponsorTimesUpvoteButtonsContainer" + lastSponsorTimeSkippedUUID; upvoteButton.id = "sponsorTimesUpvoteButtonsContainer" + lastSponsorTimeSkippedUUID;
upvoteButton.className = "sponsorSkipObject voteButton"; upvoteButton.className = "sponsorSkipObject voteButton";
upvoteButton.src = chrome.extension.getURL("icons/upvote.png"); upvoteButton.src = chrome.extension.getURL("icons/upvote.png");
upvoteButton.addEventListener("click", () => upvote(UUID)); upvoteButton.addEventListener("click", () => vote(1, UUID));
let downvoteButton = document.createElement("img"); let downvoteButton = document.createElement("img");
downvoteButton.id = "sponsorTimesDownvoteButtonsContainer" + lastSponsorTimeSkippedUUID; downvoteButton.id = "sponsorTimesDownvoteButtonsContainer" + lastSponsorTimeSkippedUUID;
downvoteButton.className = "sponsorSkipObject voteButton"; downvoteButton.className = "sponsorSkipObject voteButton";
downvoteButton.src = chrome.extension.getURL("icons/downvote.png"); 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 //add thumbs up and down buttons to the container
voteButtonsContainer.appendChild(upvoteButton); voteButtonsContainer.appendChild(upvoteButton);
@@ -336,15 +336,7 @@ function openSkipNotice(){
referenceNode.prepend(noticeElement); referenceNode.prepend(noticeElement);
} }
function upvote(UUID) { function afterDownvote(UUID) {
vote(1, UUID);
closeSkipNotice(UUID);
}
function downvote(UUID) {
vote(0, UUID);
//change text to say thanks for voting //change text to say thanks for voting
//remove buttons //remove buttons
document.getElementById("sponsorTimesVoteButtonsContainer" + UUID).removeChild(document.getElementById("sponsorTimesUpvoteButtonsContainer" + UUID)); document.getElementById("sponsorTimesVoteButtonsContainer" + UUID).removeChild(document.getElementById("sponsorTimesUpvoteButtonsContainer" + UUID));
@@ -365,11 +357,41 @@ function downvote(UUID) {
document.getElementById("sponsorTimesVoteButtonsContainer" + UUID).appendChild(thanksForVotingInfoText); 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) { function vote(type, UUID) {
chrome.runtime.sendMessage({ chrome.runtime.sendMessage({
message: "submitVote", message: "submitVote",
type: type, type: type,
UUID: UUID 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)
}
}
}); });
} }

View File

@@ -370,7 +370,7 @@ function reportAnIssue() {
document.getElementById("reportAnIssue").style.display = "none"; document.getElementById("reportAnIssue").style.display = "none";
} }
function vote(type, UUID) { function addVoteMessage(message, UUID) {
let container = document.getElementById("sponsorTimesVoteButtonsContainer" + UUID); let container = document.getElementById("sponsorTimesVoteButtonsContainer" + UUID);
//remove all children //remove all children
while (container.firstChild) { while (container.firstChild) {
@@ -378,17 +378,30 @@ function vote(type, UUID) {
} }
let thanksForVotingText = document.createElement("h2"); let thanksForVotingText = document.createElement("h2");
thanksForVotingText.innerText = "Thanks for voting!"; thanksForVotingText.innerText = message;
//there are already breaks there //there are already breaks there
thanksForVotingText.style.marginBottom = "0px"; thanksForVotingText.style.marginBottom = "0px";
container.appendChild(thanksForVotingText); container.appendChild(thanksForVotingText);
}
function vote(type, UUID) {
//send the vote message to the tab //send the vote message to the tab
chrome.runtime.sendMessage({ chrome.runtime.sendMessage({
message: "submitVote", message: "submitVote",
type: type, type: type,
UUID: UUID 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)
}
}
}); });
} }