Skip to content

Commit

Permalink
Merge pull request #3 from ajayyy/experimental
Browse files Browse the repository at this point in the history
Error messages and usability improvements
  • Loading branch information
ajayyy committed Jul 22, 2019
2 parents f162d6b + 4c380aa commit 1802600
Show file tree
Hide file tree
Showing 10 changed files with 630 additions and 122 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ None at the moment

# Credit

The awesome [Invidious API](https://github.com/omarroth/invidious/wiki/API) is used to grab the time the video was published.

Some icons made by <a href="https://www.flaticon.com/authors/gregor-cresnar" title="Gregor Cresnar">Gregor Cresnar</a> from <a href="https://www.flaticon.com/" title="Flaticon">www.flaticon.com</a> and are licensed by <a href="http://creativecommons.org/licenses/by/3.0/" title="Creative Commons BY 3.0" target="_blank">CC 3.0 BY</a>
102 changes: 76 additions & 26 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ chrome.tabs.onUpdated.addListener( // On tab update

chrome.runtime.onMessage.addListener(function (request, sender, callback) {
if (request.message == "submitTimes") {
submitTimes(request.videoID);
submitTimes(request.videoID, callback);

callback({
success: true
});
//this allows the callback to be called later by the submitTimes function
return true;
} else if (request.message == "ytvideoid") {
if (previousVideoID != request.videoID) {
videoIDChange(request.videoID);
Expand All @@ -42,7 +41,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;
}
});

Expand All @@ -51,7 +53,7 @@ chrome.runtime.onMessage.addListener(function (request, sender, callback) {
function getSponsorTimes(videoID, callback) {
let sponsorTimes = [];
let sponsorTimeKey = "sponsorTimes" + videoID;
chrome.storage.local.get([sponsorTimeKey], function(result) {
chrome.storage.sync.get([sponsorTimeKey], function(result) {
let sponsorTimesStorage = result[sponsorTimeKey];
if (sponsorTimesStorage != undefined && sponsorTimesStorage.length > 0) {
sponsorTimes = sponsorTimesStorage;
Expand All @@ -77,41 +79,70 @@ function addSponsorTime(time) {

//save this info
let sponsorTimeKey = "sponsorTimes" + previousVideoID;
chrome.storage.local.set({[sponsorTimeKey]: sponsorTimes});
chrome.storage.sync.set({[sponsorTimeKey]: sponsorTimes});
});
}

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();
sendRequestToServer('GET', "/api/voteOnSponsorTime?UUID=" + UUID + "&userID=" + userID + "&type=" + type, function(xmlhttp, error) {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
callback({
successType: 1
});
} else if (xmlhttp.readyState == 4 && xmlhttp.status == 405) {
//duplicate vote
callback({
successType: 0
});
} else if (error) {
//error while connect
callback({
successType: -1
});
}
})
})
}

function submitTimes(videoID) {
function submitTimes(videoID, callback) {
//get the video times from storage
let sponsorTimeKey = 'sponsorTimes' + videoID;
chrome.storage.local.get([sponsorTimeKey], function(result) {
chrome.storage.sync.get([sponsorTimeKey], function(result) {
let sponsorTimes = result[sponsorTimeKey];

if (sponsorTimes != undefined && sponsorTimes.length > 0) {
//submit these times
for (let i = 0; i < sponsorTimes.length; i++) {
let xmlhttp = new XMLHttpRequest();

let userIDStorage = getUserID(function(userIDStorage) {
getUserID(function(userIDStorage) {
//submit the sponsorTime
xmlhttp.open('GET', serverAddress + "/api/postVideoSponsorTimes?videoID=" + videoID + "&startTime=" + sponsorTimes[i][0] + "&endTime=" + sponsorTimes[i][1]
+ "&userID=" + userIDStorage, true);
xmlhttp.send();
sendRequestToServer('GET', "/api/postVideoSponsorTimes?videoID=" + videoID + "&startTime=" + sponsorTimes[i][0] + "&endTime=" + sponsorTimes[i][1]
+ "&userID=" + userIDStorage, function(xmlhttp, error) {
if (xmlhttp.readyState == 4 && !error) {
callback({
statusCode: xmlhttp.status
});
} else if (error) {
callback({
statusCode: -1
});
}
});
});
}

//add these to the storage log
chrome.storage.sync.get(["sponsorTimesContributed"], function(result) {
let currentContributionAmount = 0;
if (result.sponsorTimesContributed != undefined) {
//current contribution amount is known
currentContributionAmount = result.sponsorTimesContributed;
}

//save the amount contributed
chrome.storage.sync.set({"sponsorTimesContributed": currentContributionAmount + sponsorTimes.length});
});
}
});
}
Expand All @@ -121,7 +152,7 @@ function videoIDChange(currentVideoID) {
if (previousVideoID != null) {
//get the sponsor times from storage
let sponsorTimeKey = 'sponsorTimes' + previousVideoID;
chrome.storage.local.get([sponsorTimeKey], function(result) {
chrome.storage.sync.get([sponsorTimeKey], function(result) {
let sponsorTimes = result[sponsorTimeKey];

if (sponsorTimes != undefined && sponsorTimes.length > 0) {
Expand Down Expand Up @@ -149,7 +180,7 @@ function getUserID(callback) {
}

//if it is not cached yet, grab it from storage
chrome.storage.local.get(["userID"], function(result) {
chrome.storage.sync.get(["userID"], function(result) {
let userIDStorage = result.userID;
if (userIDStorage != undefined) {
userID = userIDStorage;
Expand All @@ -159,13 +190,32 @@ function getUserID(callback) {
userID = generateUUID();

//save this UUID
chrome.storage.local.set({"userID": userID});
chrome.storage.sync.set({"userID": userID});

callback(userID);
}
});
}

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);
Expand Down
76 changes: 72 additions & 4 deletions content.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
.playerButton {
height: 60%;
top: 0;
bottom: 0;
display: block;
margin: auto;
}

.sponsorSkipObject {
font-family: 'Source Sans Pro', sans-serif;
}

#sponsorSkipLogo {
.sponsorSkipLogo {
height: 64px;
position: absolute;
top: 0;
Expand All @@ -11,17 +19,31 @@
margin-left: 10px;
}

#sponsorSkipNotice {
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}

.sponsorSkipNotice {
min-height: 165px;
min-width: 400px;
background-color: rgba(255, 217, 217, 0.8);
position: absolute;
z-index: 1;
border: 3px solid rgba(0, 0, 0, 0.8);
margin-top: -50px;

animation: fadeIn 0.5s;
}

/* if two are very close to eachother */
.secondSkipNotice {
margin-left: 500px;

transition: margin-left 0.2s;
}

#sponsorSkipMessage {
.sponsorSkipMessage {
font-size: 18px;
color: #000000;
text-align: center;
Expand All @@ -30,7 +52,7 @@
margin-top: 4px;
}

#sponsorSkipInfo {
.sponsorSkipInfo {
font-size: 10px;
color: #000000;
text-align: center;
Expand All @@ -42,13 +64,23 @@
font-weight: bold;
color: #000000;
text-align: center;
margin-top: 0px;
margin-bottom: 0px;
}

#sponsorTimesThanksForVotingInfoText {
font-size: 12px;
font-weight: bold;
color: #000000;
text-align: center;
margin-top: 0px;
}

.sponsorTimesInfoMessage {
font-size: 15px;
font-weight: bold;
color: #000000;
text-align: center;
}

.voteButton {
Expand All @@ -60,6 +92,42 @@
filter: brightness(80%);
}

.submitButton {
background-color:#ec1c1c;
-moz-border-radius:28px;
-webkit-border-radius:28px;
border-radius:28px;
border:1px solid #d31919;
display:inline-block;
cursor:pointer;
color:#ffffff;
font-size:14px;
padding:4px 15px;
text-decoration:none;
text-shadow:0px 0px 0px #662727;

margin-top: 5px;
margin-right: 15px;
}
.submitButton:hover {
background-color:#bf2a2a;
}

.submitButton:focus {
outline: none;
background-color:#bf2a2a;
}

.submitButton:active {
position:relative;
top:1px;
}

@keyframes rotate {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}

.sponsorSkipButton {
background-color:#ec1c1c;
-moz-border-radius:28px;
Expand Down

0 comments on commit 1802600

Please sign in to comment.