Skip to content

Commit

Permalink
Merge pull request #195 from ajayyy/experimental-ajay
Browse files Browse the repository at this point in the history
Small Fixes
  • Loading branch information
ajayyy authored Dec 14, 2019
2 parents 3d01ffe + 46ac654 commit b30acaa
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 104 deletions.
8 changes: 7 additions & 1 deletion _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"message": "Paused"
},
"confirmMSG": {
"message": "\n\nTo edit or delete individual values, click the info button or open the extension popup by clicking the extension icon in the top right corner."
"message": "To edit or delete individual values, click the info button or open the extension popup by clicking the extension icon in the top right corner."
},
"clearThis": {
"message": "Are you sure you want to clear this?\n\n"
Expand Down Expand Up @@ -314,5 +314,11 @@
},
"youHaveSavedTimeEnd": {
"message": " of their lives."
},
"guildlinesSummary": {
"message": "- Make sure your segment only contains paid promotion segment, nothing else.\n- Make sure skipping this segment will not skip valuable content\n- If the whole video is a sponsor, please do not report it. A full video reporting system will come out soon.\n- Please do not report disclaimers that could show bias (if a review video is sponsored, don't skip when they mention that)."
},
"statusReminder": {
"message": "Check status.sponsor.ajay.app for server status."
}
}
22 changes: 2 additions & 20 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ chrome.runtime.onMessage.addListener(function (request, sender, callback) {
case "alertPrevious":
chrome.notifications.create("stillThere" + Math.random(), {
type: "basic",
title: chrome.i18n.getMessage("wantToSubmit") + request.previousVideoID + "?",
title: chrome.i18n.getMessage("wantToSubmit") + " " + request.previousVideoID + "?",
message: chrome.i18n.getMessage("leftTimes"),
iconUrl: "./icons/LogoSponsorBlocker256px.png"
});
Expand Down Expand Up @@ -215,22 +215,4 @@ function sendRequestToServer(type, address, callback) {

//submit this request
xmlhttp.send();
}

function generateUserID(length = 36) {
let charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let result = "";
if (window.crypto && window.crypto.getRandomValues) {
values = new Uint32Array(length);
window.crypto.getRandomValues(values);
for (i = 0; i < length; i++) {
result += charset[values[i] % charset.length];
}
return result;
} else {
for (let i = 0; i < length; i++) {
result += charset[Math.floor(Math.random() * charset.length)];
}
return result;
}
}
}
70 changes: 33 additions & 37 deletions content.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,12 @@ function updatePreviewBar() {
//create an array of the sponsor types
let types = [];
for (let i = 0; i < localSponsorTimes.length; i++) {
types.push("sponsor");
if (!hiddenSponsorTimes.includes(i)) {
types.push("sponsor");
} else {
// Don't show this sponsor
types.push(null);
}
}
for (let i = 0; i < sponsorTimesSubmitting.length; i++) {
types.push("previewSponsor");
Expand All @@ -493,25 +498,20 @@ function updatePreviewBar() {

function getChannelID() {
//get channel id
let channelContainers = document.querySelectorAll(".ytd-channel-name#text");
let channelURLContainer = null;
let channelNameContainer = document.getElementById("channel-name");
let channelURLContainer = channelNameContainer.querySelector("#container").querySelector("#text-container").querySelector("#text").firstElementChild;


for (let i = 0; i < channelContainers.length; i++) {
let child = channelContainers[i].firstElementChild;
if (child != null && child.getAttribute("href") != "") {
channelURLContainer = child;
}
}

if (channelContainers.length == 0) {
if (channelURLContainer === null) {
//old YouTube theme
channelContainers = document.getElementsByClassName("yt-user-info");
let channelContainers = document.getElementsByClassName("yt-user-info");
if (channelContainers.length != 0) {
channelURLContainer = channelContainers[0].firstElementChild;
}
}

if (channelURLContainer == null) {
if (channelURLContainer === null) {
//try later
return false;
}
Expand Down Expand Up @@ -545,11 +545,9 @@ function whitelistCheck() {
chrome.storage.sync.get(["whitelistedChannels"], function(result) {
let whitelistedChannels = result.whitelistedChannels;

if (whitelistedChannels != undefined && whitelistedChannels.includes(channelURL)) {
//reset sponsor times to nothing
sponsorTimes = [];
UUIDs = [];
console.log(channelURL)

if (whitelistedChannels != undefined && whitelistedChannels.includes(channelURL)) {
channelWhitelisted = true;
}
});
Expand All @@ -561,6 +559,8 @@ function sponsorCheck() {
// Make sure this isn't called again
v.ontimeupdate = null;
return;
} else if (channelWhitelisted) {
return;
}

let skipHappened = false;
Expand Down Expand Up @@ -980,14 +980,8 @@ function vote(type, UUID, skipNotice) {
skipNotice.addNoticeInfoMessage.bind(skipNotice)(chrome.i18n.getMessage("voteFail"))
skipNotice.resetVoteButtonInfo.bind(skipNotice)();
} else if (response.successType == -1) {
if (response.statusCode == 502) {
skipNotice.addNoticeInfoMessage.bind(skipNotice)(chrome.i18n.getMessage("serverDown"))
skipNotice.resetVoteButtonInfo.bind(skipNotice)();
} else {
//failure: unknown error
skipNotice.addNoticeInfoMessage.bind(skipNotice)(chrome.i18n.getMessage("connectionError") + response.statusCode);
skipNotice.resetVoteButtonInfo.bind(skipNotice)();
}
skipNotice.addNoticeInfoMessage.bind(skipNotice)(getErrorMessage(response.statusCode))
skipNotice.resetVoteButtonInfo.bind(skipNotice)();
}
}
}
Expand Down Expand Up @@ -1047,8 +1041,11 @@ function submitSponsorTimes() {
//update sponsorTimes
chrome.storage.sync.set({[sponsorTimeKey]: sponsorTimes});

let confirmMessage = chrome.i18n.getMessage("submitCheck") + "\n\n" + getSponsorTimesMessage(sponsorTimes);
confirmMessage += "\n\n" + chrome.i18n.getMessage("confirmMSG");
//update sponsorTimesSubmitting
sponsorTimesSubmitting = sponsorTimes;

let confirmMessage = chrome.i18n.getMessage("submitCheck") + "\n\n" + getSponsorTimesMessage(sponsorTimes)
+ "\n\n" + chrome.i18n.getMessage("confirmMSG") + "\n\n" + chrome.i18n.getMessage("guildlinesSummary");
if(!confirm(confirmMessage)) return;

sendSubmitMessage();
Expand All @@ -1066,8 +1063,6 @@ function sendSubmitMessage(){

let currentVideoID = sponsorVideoID;

let currentSponsorTimes = submitSponsorTimes;

chrome.runtime.sendMessage({
message: "submitTimes",
videoID: currentVideoID
Expand All @@ -1091,24 +1086,25 @@ function sendSubmitMessage(){

//clear the sponsor times
let sponsorTimeKey = "sponsorTimes" + currentVideoID;
chrome.storage.sync.set({[sponsorTimeKey]: []}, () => void updatePreviewBar());
chrome.storage.sync.set({[sponsorTimeKey]: []});

//add submissions to current sponsors list
sponsorTimes = sponsorTimes.concat(sponsorTimesSubmitting);
for (let i = 0; i < sponsorTimesSubmitting.length; i++) {
// Add some random IDs
UUIDs.push(generateUserID());
}

// Empty the submitting times
sponsorTimesSubmitting = [];

updatePreviewBar();
} else {
//show that the upload failed
document.getElementById("submitButton").style.animation = "unset";
document.getElementById("submitImage").src = chrome.extension.getURL("icons/PlayerUploadFailedIconSponsorBlocker256px.png");

if([400, 429, 409, 502, 0].includes(response.statusCode)) {
//treat them the same
if (response.statusCode == 503) response.statusCode = 502;

alert(chrome.i18n.getMessage(response.statusCode + "") + " " + chrome.i18n.getMessage("errorCode") + response.statusCode);
} else {
alert(chrome.i18n.getMessage("connectionError") + response.statusCode);
}
alert(getErrorMessage(response.statusCode));
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion help/index_en.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ <h1>How skipping works</h1>
<br/>
<br/>

Whenever you skip a video, you will get a notice report that submission. If the timing seems wrong, report it! You can also vote in the popup. The extension auto upvotes it if you don't report it, so make sure to report when necessary.
Whenever you skip a video, you will get a notice report that submission. If the timing seems wrong, report it! You can also vote in the popup. The extension auto upvotes it if you don't report it, so make sure to report when necessary (this can be disabled in the options).
</p>

<center><img height="120px" src="https://user-images.githubusercontent.com/12688112/63067735-5a638700-bede-11e9-8147-f321b57527ec.gif"></center>
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.8",
"version": "1.1.9.9",
"default_locale": "en",
"description": "__MSG_Description__",
"content_scripts": [
Expand Down
14 changes: 7 additions & 7 deletions popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -140,24 +140,24 @@ <h2 class="recordingSubtitle popupElement">__MSG_yourWork__</h2>
<span class="popupElement">__MSG_youHaveSavedTimeEnd__</span>
</span>

<br/>
<br/>

<span id="sponsorTimesSkipsDoneContainer" class="popupElement" style="display: none">
<div 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>
</div>

<span id="sponsorTimeSavedContainer" class="popupElement" style="display: none">
<div 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>

</br/>
</br/>
</div>

<div class="popupElement">
__MSG_viewLeaderboard__ <a class="popupElement discreteLink" href="https://sponsor.ajay.app/stats" target="_blank">__MSG_here__</a>.
Expand Down
32 changes: 4 additions & 28 deletions popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -895,18 +895,7 @@ function runThePopup() {

clearTimes();
} else {
let errorMessage = "";

if([400, 429, 409, 502, 0].includes(response.statusCode)) {
//treat them the same
if (response.statusCode == 503) response.statusCode = 502;

errorMessage = chrome.i18n.getMessage(response.statusCode + "") + " " + chrome.i18n.getMessage("errorCode") + response.statusCode;
} else {
errorMessage = chrome.i18n.getMessage("connectionError") + response.statusCode;
}

document.getElementById("submitTimesInfoMessage").innerText = errorMessage;
document.getElementById("submitTimesInfoMessage").innerText = getErrorMessage(response.statusCode);
document.getElementById("submitTimesInfoMessageContainer").style.display = "unset";

SB.submitTimesInfoMessageContainer.style.display = "unset";
Expand Down Expand Up @@ -1138,7 +1127,7 @@ function runThePopup() {
SB.usernameInput.style.display = "none";

SB.setUsernameStatusContainer.style.display = "unset";
SB.setUsernameStatus.innerText = "Couldn't connect to server. Error code: " + xmlhttp.status;
SB.setUsernameStatus.innerText = getErrorMessage(xmlhttp.status);
}
});
});
Expand All @@ -1160,15 +1149,7 @@ function runThePopup() {

SB.setUsernameStatus.innerText = chrome.i18n.getMessage("success");
} else if (xmlhttp.readyState == 4) {
let errorMessage = "";

if([400, 429, 409, 502].includes(xmlhttp.status)) {
errorMessage = chrome.i18n.getMessage(xmlhttp.status);
} else {
errorMessage = chrome.i18n.getMessage("connectionError") + xmlhttp.status;
}

SB.setUsernameStatus.innerText = errorMessage;
SB.setUsernameStatus.innerText = getErrorMessageI(xmlhttp.status);
}
});
});
Expand Down Expand Up @@ -1224,12 +1205,7 @@ function runThePopup() {
//failure: duplicate vote
addVoteMessage(chrome.i18n.getMessage("voteFail"), UUID)
} else if (response.successType == -1) {
if (response.statusCode == 502) {
addVoteMessage(chrome.i18n.getMessage("serverDown"), UUID)
} else {
//failure: unknown error
addVoteMessage(chrome.i18n.getMessage("connectionError") + response.statusCode, UUID)
}
addVoteMessage(getErrorMessage(response.statusCode), UUID)
}
}
});
Expand Down
39 changes: 39 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,42 @@ function localizeHtmlPage() {
}
}

function generateUserID(length = 36) {
let charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let result = "";
if (window.crypto && window.crypto.getRandomValues) {
values = new Uint32Array(length);
window.crypto.getRandomValues(values);
for (i = 0; i < length; i++) {
result += charset[values[i] % charset.length];
}
return result;
} else {
for (let i = 0; i < length; i++) {
result += charset[Math.floor(Math.random() * charset.length)];
}
return result;
}
}

/**
* Gets the error message in a nice string
*
* @param {int} statusCode
* @returns {string} errorMessage
*/
function getErrorMessage(statusCode) {
let errorMessage = "";

if([400, 429, 409, 502, 0].includes(statusCode)) {
//treat them the same
if (statusCode == 503) statusCode = 502;

errorMessage = chrome.i18n.getMessage(statusCode + "") + " " + chrome.i18n.getMessage("errorCode") + statusCode
+ "\n\n" + chrome.i18n.getMessage("statusReminder");
} else {
errorMessage = chrome.i18n.getMessage("connectionError") + statusCode;
}

return errorMessage;
}
2 changes: 2 additions & 0 deletions utils/previewBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class PreviewBar {
duration = Math.floor(duration * 100) / 100;
let width;
for (let i = 0; i < timestamps.length; i++) {
if (types[i] == null) continue;

width = (timestamps[i][1] - timestamps[i][0]) / duration * 100;
width = Math.floor(width * 100) / 100;

Expand Down
10 changes: 1 addition & 9 deletions utils/skipNotice.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,7 @@ class SkipNotice {
//add this as a hidden sponsorTime
hiddenSponsorTimes.push(i);

let sponsorTimesLeft = sponsorTimes.slice();
for (let j = 0; j < hiddenSponsorTimes.length; j++) {
//remove this sponsor time
sponsorTimesLeft.splice(hiddenSponsorTimes[j], 1);
}

//update the preview
previewBar.set(sponsorTimesLeft, [], v.duration);

updatePreviewBar();
break;
}
}
Expand Down

0 comments on commit b30acaa

Please sign in to comment.