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
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);

View File

@@ -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;

View File

@@ -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)
}
}
});
}

View File

@@ -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)
}
}
});
}