From 987b7b036c004c0c42cc4be1c643b45d1c71f3b2 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Mon, 15 Jul 2019 19:13:09 -0400 Subject: [PATCH] Made upvote system submit data to the server. --- background.js | 16 ++++++++++++++++ content.js | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/background.js b/background.js index 388d4cd3..c619bcbb 100644 --- a/background.js +++ b/background.js @@ -41,6 +41,8 @@ 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) } }); @@ -79,6 +81,19 @@ function addSponsorTime(time) { }); } +function submitVote(type, UUID) { + let xmlhttp = new XMLHttpRequest(); + + 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(); + }) +} + function submitTimes(videoID) { //get the video times from storage let sponsorTimeKey = 'sponsorTimes' + videoID; @@ -130,6 +145,7 @@ function videoIDChange(currentVideoID) { function getUserID(callback) { if (userID != null) { callback(userID); + return; } //if it is not cached yet, grab it from storage diff --git a/content.js b/content.js index f50a08b4..5cbc538d 100644 --- a/content.js +++ b/content.js @@ -264,10 +264,12 @@ function openSkipNotice(){ let upvoteButton = document.createElement("img"); upvoteButton.className = "sponsorSkipObject voteButton"; upvoteButton.src = chrome.extension.getURL("icons/upvote.png"); + upvoteButton.addEventListener("click", upvote); let downvoteButton = document.createElement("img"); downvoteButton.className = "sponsorSkipObject voteButton"; downvoteButton.src = chrome.extension.getURL("icons/downvote.png"); + downvoteButton.addEventListener("click", downvote); //add thumbs up and down buttons to the container voteButtonsContainer.appendChild(upvoteButton); @@ -311,6 +313,22 @@ function openSkipNotice(){ referenceNode.prepend(noticeElement); } +function upvote() { + vote(1); +} + +function downvote() { + vote(0); +} + +function vote(type) { + chrome.runtime.sendMessage({ + message: "submitVote", + type: type, + UUID: lastSponsorTimeSkippedUUID + }); +} + //Closes the notice that tells the user that a sponsor was just skipped function closeSkipNotice(){ let notice = document.getElementById("sponsorSkipNotice");