Skip to content

Commit

Permalink
Merge pull request #179 from ajayyy/experimental-ajay
Browse files Browse the repository at this point in the history
Added better local stats.
  • Loading branch information
ajayyy authored Dec 5, 2019
2 parents d6aeec9 + 9c51df9 commit 3d01ffe
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 14 deletions.
28 changes: 26 additions & 2 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,10 @@
"message": "This is the button that allows you to clear all sponsors on the YouTube player."
},
"disableViewTracking": {
"message": "Disable Sponsor View Tracking"
"message": "Disable Sponsor Skip Count Tracking"
},
"enableViewTracking": {
"message": "Enable Sponsor View Tracking"
"message": "Enable Sponsor Skip Count Tracking"
},
"whatViewTracking": {
"message": "This feature tracks which sponsors you have skipped to let users know how much their submission has helped others and\nused as a metric along with upvotes to ensure that spam doesn't get into the database. The extension sends a message\nto the server each time you skip a sponsor. Hopefully most people don't change this setting so that the view numbers are accurate. :)"
Expand Down Expand Up @@ -290,5 +290,29 @@
},
"autoSkipDescription": {
"message": "Auto skip will skip sponsors for you. If disabled, a notice will appear asking if you'd like to skip."
},
"youHaveSkipped": {
"message": "You have skipped "
},
"youHaveSaved": {
"message": "You have saved yourself "
},
"minLower": {
"message": "minute"
},
"minsLower": {
"message": "minutes"
},
"hourLower": {
"message": "hour"
},
"hoursLower": {
"message": "hours"
},
"youHaveSavedTime": {
"message": "You have saved people"
},
"youHaveSavedTimeEnd": {
"message": " of their lives."
}
}
50 changes: 48 additions & 2 deletions content.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ var sponsorVideoID = null;
//these are sponsors that have been downvoted
var hiddenSponsorTimes = [];

/** @type {Array[boolean]} Has the sponsor been skipped */
var sponsorSkipped = [];

//the video
var v;

Expand Down Expand Up @@ -418,6 +421,9 @@ function sponsorsLookup(id, channelIDPromise) {
sponsorTimes = JSON.parse(xmlhttp.responseText).sponsorTimes;
UUIDs = JSON.parse(xmlhttp.responseText).UUIDs;

// Reset skip save
sponsorSkipped = [];

//update the preview bar
//leave the type blank for now until categories are added
if (lastPreviewBarUpdate == id || (lastPreviewBarUpdate == null && !isNaN(v.duration))) {
Expand Down Expand Up @@ -639,15 +645,31 @@ function skipToTime(v, index, sponsorTimes, openNotice) {
}

//auto-upvote this sponsor
if (trackViewCount) {
if (trackViewCount && !disableAutoSkip) {
vote(1, currentUUID, null);
}
}
}

//send telemetry that a this sponsor was skipped happened
if (trackViewCount) {
if (trackViewCount && !sponsorSkipped[index]) {
sendRequestToServer("GET", "/api/viewedVideoSponsorTime?UUID=" + currentUUID);

if (!disableAutoSkip) {
// Count this as a skip
chrome.storage.sync.get(["minutesSaved"], function(result) {
if (result.minutesSaved === undefined) result.minutesSaved = 0;

chrome.storage.sync.set({"minutesSaved": result.minutesSaved + (sponsorTimes[index][1] - sponsorTimes[index][0]) / 60 });
});
chrome.storage.sync.get(["skipCount"], function(result) {
if (result.skipCount === undefined) result.skipCount = 0;

chrome.storage.sync.set({"skipCount": result.skipCount + 1 });
});

sponsorSkipped[index] = true;
}
}
}

Expand Down Expand Up @@ -916,6 +938,30 @@ function vote(type, UUID, skipNotice) {
skipNotice.resetNoticeInfoMessage.bind(skipNotice)();
}

let sponsorIndex = UUIDs.indexOf(UUID);

// See if the local time saved count and skip count should be reverted
if (type == 0 && sponsorSkipped[sponsorIndex] || type == 1 && !sponsorSkipped[sponsorIndex]) {
let factor = 1;
if (type == 0) {
factor = -1;
}

// Count this as a skip
chrome.storage.sync.get(["minutesSaved"], function(result) {
if (result.minutesSaved === undefined) result.minutesSaved = 0;

chrome.storage.sync.set({"minutesSaved": result.minutesSaved + factor * (sponsorTimes[sponsorIndex][1] - sponsorTimes[sponsorIndex][0]) / 60 });
});
chrome.storage.sync.get(["skipCount"], function(result) {
if (result.skipCount === undefined) result.skipCount = 0;

chrome.storage.sync.set({"skipCount": result.skipCount + factor * 1 });
});

sponsorSkipped[sponsorIndex] = !sponsorSkipped[sponsorIndex];
}

chrome.runtime.sendMessage({
message: "submitVote",
type: type,
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "__MSG_fullName__",
"short_name": "__MSG_Name__",
"version": "1.1.9.6",
"version": "1.1.9.8",
"default_locale": "en",
"description": "__MSG_Description__",
"content_scripts": [
Expand Down
42 changes: 33 additions & 9 deletions popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -114,25 +114,49 @@ <h3 style="margin-top: 0px" class="popupElement">__MSG_voteOnTime__</h3>
<h2 class="recordingSubtitle popupElement">__MSG_yourWork__</h2>

<p class="popupElement">
<span id=sponsorTimesContributionsContainer class="popupElement" style="display: none">
<span id="sponsorTimesContributionsContainer" class="popupElement" style="display: none">
__MSG_soFarUHSubmited__
<span id="sponsorTimesContributionsDisplay" class="popupElement">
0
</span>
<span id="sponsorTimesContributionsDisplayEndWord" class="popupElement">
__MSG_Sponsors__.
</span>
<span id="sponsorTimesContributionsDisplayEndWord" class="popupElement">__MSG_Sponsors__</span>.
</span>

<span id=sponsorTimesViewsContainer class="popupElement" style="display: none">
<span id="sponsorTimesViewsContainer" class="popupElement" style="display: none">
__MSG_savedPeopleFrom__
<span id="sponsorTimesViewsDisplay" class="popupElement">
0
</span>
<span id="sponsorTimesViewsDisplayEndWord" class="popupElement">
__MSG_Segments__.
__MSG_savedPeopleFrom__
</span>
<span id="sponsorTimesViewsDisplayEndWord" class="popupElement">__MSG_Segments__</span>.
</span>

<span id="sponsorTimesOthersTimeSavedContainer" class="popupElement" style="display: none">
__MSG_youHaveSavedTime__
<span id="sponsorTimesOthersTimeSavedDisplay" class="popupElement">
0
</span>
<span id="sponsorTimesOthersTimeSavedEndWord" class="popupElement">__MSG_minsLower__</span>

<span class="popupElement">__MSG_youHaveSavedTimeEnd__</span>
</span>

<br/>
<br/>

<span id="sponsorTimesSkipsDoneContainer" class="popupElement" style="display: none">
__MSG_youHaveSkipped__
<span id="sponsorTimesSkipsDoneDisplay" class="popupElement">
0
</span>
<span id="sponsorTimesSkipsDoneEndWord" class="popupElement">__MSG_Segments__</span> since December 5th.
</span>

<span id="sponsorTimeSavedContainer" class="popupElement" style="display: none">
__MSG_youHaveSaved__
<span id="sponsorTimeSavedDisplay" class="popupElement">
0
</span>
<span id="sponsorTimeSavedEndWord" class="popupElement">__MSG_minsLower__</span> since December 5th.
</span>

<div class="popupElement">
Expand Down
69 changes: 69 additions & 0 deletions popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ function runThePopup() {
"sponsorTimesViewsContainer",
"sponsorTimesViewsDisplay",
"sponsorTimesViewsDisplayEndWord",
// sponsorTimesOthersTimeSaved
"sponsorTimesOthersTimeSavedContainer",
"sponsorTimesOthersTimeSavedDisplay",
"sponsorTimesOthersTimeSavedEndWord",
// sponsorTimesSkipsDone
"sponsorTimesSkipsDoneContainer",
"sponsorTimesSkipsDoneDisplay",
"sponsorTimesSkipsDoneEndWord",
// sponsorTimeSaved
"sponsorTimeSavedContainer",
"sponsorTimeSavedDisplay",
"sponsorTimeSavedEndWord",
// discordButtons
"discordButtonContainer",
"hideDiscordButton",
Expand Down Expand Up @@ -236,10 +248,55 @@ function runThePopup() {
}
}
});

//get this time in minutes
sendRequestToServer("GET", "/api/getSavedTimeForUser?userID=" + userID, function(xmlhttp) {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
let minutesSaved = JSON.parse(xmlhttp.responseText).timeSaved;
if (minutesSaved != 0) {
if (minutesSaved != 1) {
SB.sponsorTimesOthersTimeSavedEndWord.innerText = chrome.i18n.getMessage("minsLower");
} else {
SB.sponsorTimesOthersTimeSavedEndWord.innerText = chrome.i18n.getMessage("minLower");
}

SB.sponsorTimesOthersTimeSavedDisplay.innerText = getFormattedHours(minutesSaved);
SB.sponsorTimesOthersTimeSavedContainer.style.display = "unset";
}
}
});
}
});
}
});

//get the amount of times this user has skipped a sponsor
chrome.storage.sync.get(["skipCount"], function(result) {
if (result.skipCount != undefined) {
if (result.skipCount != 1) {
SB.sponsorTimesSkipsDoneEndWord.innerText = chrome.i18n.getMessage("Sponsors");
} else {
SB.sponsorTimesSkipsDoneEndWord.innerText = chrome.i18n.getMessage("Sponsor");
}

SB.sponsorTimesSkipsDoneDisplay.innerText = result.skipCount;
SB.sponsorTimesSkipsDoneContainer.style.display = "unset";
}
});

//get the amount of time this user has saved.
chrome.storage.sync.get(["minutesSaved"], function(result) {
if (result.minutesSaved != undefined) {
if (result.minutesSaved != 1) {
SB.sponsorTimeSavedEndWord.innerText = chrome.i18n.getMessage("minsLower");
} else {
SB.sponsorTimeSavedEndWord.innerText = chrome.i18n.getMessage("minLower");
}

SB.sponsorTimeSavedDisplay.innerText = getFormattedHours(result.minutesSaved);
SB.sponsorTimeSavedContainer.style.display = "unset";
}
});

chrome.tabs.query({
active: true,
Expand Down Expand Up @@ -1377,6 +1434,18 @@ function runThePopup() {
//submit this request
xmlhttp.send();
}

/**
* Converts time in hours to 5h 25.1
* If less than 1 hour, just returns minutes
*
* @param {float} seconds
* @returns {string}
*/
function getFormattedHours(minues) {
let hours = Math.floor(minues / 60);
return (hours > 0 ? hours + "h " : "") + (minues % 60).toFixed(1);
}

//end of function
}
Expand Down
2 changes: 2 additions & 0 deletions utils/skipNotice.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ class SkipNotice {
// See if the title should be changed
if (this.manualSkip) {
this.changeNoticeTitle(chrome.i18n.getMessage("noticeTitle"));

vote(1, this.UUID, this);
}
}

Expand Down

0 comments on commit 3d01ffe

Please sign in to comment.