mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-06 19:47:04 +03:00
Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9342112bac | ||
|
|
3d1be7158d | ||
|
|
efe512b561 | ||
|
|
d738eac42d | ||
|
|
a83969e3eb | ||
|
|
92cb8fb65c | ||
|
|
932702cca1 | ||
|
|
ca8404147d | ||
|
|
35c3b5b97f | ||
|
|
015b283731 | ||
|
|
2667838937 | ||
|
|
a5ec7b2466 | ||
|
|
3a0a267e12 | ||
|
|
cbdb715fac | ||
|
|
64fb12289c | ||
|
|
25801b6fcd | ||
|
|
1341d5e11d | ||
|
|
e1dc5fbdf5 | ||
|
|
d51e8044f4 | ||
|
|
69dee25ea8 | ||
|
|
0d8c18a38a | ||
|
|
a4eb37db18 |
@@ -147,6 +147,20 @@ function submitTimes(videoID, callback) {
|
||||
callback({
|
||||
statusCode: xmlhttp.status
|
||||
});
|
||||
|
||||
if (xmlhttp.status == 200) {
|
||||
//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});
|
||||
});
|
||||
}
|
||||
} else if (error) {
|
||||
callback({
|
||||
statusCode: -1
|
||||
@@ -155,18 +169,6 @@ function submitTimes(videoID, callback) {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
//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});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -45,18 +45,19 @@
|
||||
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;
|
||||
right: 0;
|
||||
bottom: 90px;
|
||||
zoom: 85%;
|
||||
|
||||
animation: fadeIn 0.5s;
|
||||
}
|
||||
|
||||
/* if two are very close to eachother */
|
||||
.secondSkipNotice {
|
||||
margin-left: 500px;
|
||||
bottom: 280px;
|
||||
|
||||
transition: margin-left 0.2s;
|
||||
transition: bottom 0.2s;
|
||||
}
|
||||
|
||||
.sponsorSkipMessage {
|
||||
|
||||
247
content.js
247
content.js
@@ -34,6 +34,13 @@ var hideVideoPlayerControls = false;
|
||||
var hideInfoButtonPlayerControls = false;
|
||||
var hideDeleteButtonPlayerControls = false;
|
||||
|
||||
//the downloaded sponsor times
|
||||
var sponsorTimes = [];
|
||||
var UUIDs = [];
|
||||
|
||||
//the sponsor times being prepared to be submitted
|
||||
var sponsorTimesSubmitting = [];
|
||||
|
||||
//becomes true when isInfoFound is called
|
||||
//this is used to close the popup on YouTube when the other popup opens
|
||||
var popupInitialised = false;
|
||||
@@ -73,6 +80,10 @@ function messageListener(request, sender, sendResponse) {
|
||||
sponsorMessageStarted(sendResponse);
|
||||
}
|
||||
|
||||
if (request.message == "sponsorDataChanged") {
|
||||
updateSponsorTimesSubmitting();
|
||||
}
|
||||
|
||||
if (request.message == "isInfoFound") {
|
||||
//send the sponsor times along with if it's found
|
||||
sendResponse({
|
||||
@@ -160,6 +171,9 @@ function videoIDChange(id) {
|
||||
sponsorDataFound = false;
|
||||
sponsorsLookup(id);
|
||||
|
||||
//reset sponsor times submitting
|
||||
sponsorTimesSubmitting = [];
|
||||
|
||||
//see if the onvideo control image needs to be changed
|
||||
chrome.runtime.sendMessage({
|
||||
message: "getSponsorTimes",
|
||||
@@ -174,6 +188,11 @@ function videoIDChange(id) {
|
||||
} else {
|
||||
changeStartSponsorButton(true, false);
|
||||
}
|
||||
|
||||
//see if this data should be saved in the sponsorTimesSubmitting variable
|
||||
if (sponsorTimes != undefined && sponsorTimes.length > 0) {
|
||||
sponsorTimesSubmitting = sponsorTimes;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -203,82 +222,121 @@ function videoIDChange(id) {
|
||||
}
|
||||
|
||||
function sponsorsLookup(id) {
|
||||
v = document.querySelector('video') // Youtube video player
|
||||
|
||||
//check database for sponsor times
|
||||
sendRequestToServer('GET', "/api/getVideoSponsorTimes?videoID=" + id, function(xmlhttp) {
|
||||
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
|
||||
sponsorDataFound = true;
|
||||
v = document.querySelector('video') // Youtube video player
|
||||
|
||||
//check database for sponsor times
|
||||
sendRequestToServer('GET', "/api/getVideoSponsorTimes?videoID=" + id, function(xmlhttp) {
|
||||
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
|
||||
sponsorDataFound = true;
|
||||
|
||||
sponsorTimes = JSON.parse(xmlhttp.responseText).sponsorTimes;
|
||||
UUIDs = JSON.parse(xmlhttp.responseText).UUIDs;
|
||||
sponsorTimes = JSON.parse(xmlhttp.responseText).sponsorTimes;
|
||||
UUIDs = JSON.parse(xmlhttp.responseText).UUIDs;
|
||||
} else if (xmlhttp.readyState == 4) {
|
||||
sponsorDataFound = false;
|
||||
|
||||
// If the sponsor data exists, add the event to run on the videos "ontimeupdate"
|
||||
v.ontimeupdate = function () {
|
||||
sponsorCheck(sponsorTimes);
|
||||
};
|
||||
} else if (xmlhttp.readyState == 4) {
|
||||
sponsorDataFound = false;
|
||||
//check if this video was uploaded recently
|
||||
//use the invidious api to get the time published
|
||||
sendRequestToCustomServer('GET', "https://invidio.us/api/v1/videos/" + id, function(xmlhttp, error) {
|
||||
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
|
||||
let unixTimePublished = JSON.parse(xmlhttp.responseText).published;
|
||||
|
||||
//check if this video was uploaded recently
|
||||
//use the invidious api to get the time published
|
||||
sendRequestToCustomServer('GET', "https://invidio.us/api/v1/videos/" + id, function(xmlhttp, error) {
|
||||
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
|
||||
let unixTimePublished = JSON.parse(xmlhttp.responseText).published;
|
||||
|
||||
//if less than 3 days old
|
||||
if ((Date.now() / 1000) - unixTimePublished < 259200) {
|
||||
setTimeout(() => sponsorsLookup(id), 10000);
|
||||
}
|
||||
//if less than 3 days old
|
||||
if ((Date.now() / 1000) - unixTimePublished < 259200) {
|
||||
setTimeout(() => sponsorsLookup(id), 10000);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
//add the event to run on the videos "ontimeupdate"
|
||||
v.ontimeupdate = function () {
|
||||
sponsorCheck();
|
||||
};
|
||||
}
|
||||
|
||||
function sponsorCheck(sponsorTimes) { // Video skipping
|
||||
//see if any sponsor start time was just passed
|
||||
for (let i = 0; i < sponsorTimes.length; i++) {
|
||||
//this means part of the video was just skipped
|
||||
if (Math.abs(v.currentTime - lastTime) > 1 && lastTime != -1) {
|
||||
//make lastTime as if the video was playing normally
|
||||
lastTime = v.currentTime - 0.0001;
|
||||
//video skipping
|
||||
function sponsorCheck() {
|
||||
let skipHappened = false;
|
||||
|
||||
if (sponsorTimes != null) {
|
||||
//see if any sponsor start time was just passed
|
||||
for (let i = 0; i < sponsorTimes.length; i++) {
|
||||
//if something was skipped
|
||||
if (checkSponsorTime(sponsorTimes, i, true)) {
|
||||
skipHappened = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let currentTime = Date.now();
|
||||
|
||||
//If the sponsor time is in between these times, skip it
|
||||
//Checks if the last time skipped to is not too close to now, to make sure not to get too many
|
||||
// sponsor times in a row (from one troll)
|
||||
//the last term makes 0 second start times possible
|
||||
if ((Math.abs(v.currentTime - sponsorTimes[i][0]) < 0.3 && sponsorTimes[i][0] >= lastTime && sponsorTimes[i][0] <= v.currentTime
|
||||
&& (lastUnixTimeSkipped == -1 || currentTime - lastUnixTimeSkipped > 500)) || (lastTime == -1 && sponsorTimes[i][0] == 0)) {
|
||||
//skip it
|
||||
v.currentTime = sponsorTimes[i][1];
|
||||
|
||||
lastSponsorTimeSkipped = sponsorTimes[i][0];
|
||||
|
||||
let currentUUID = UUIDs[i];
|
||||
lastSponsorTimeSkippedUUID = currentUUID;
|
||||
|
||||
//send out the message saying that a sponsor message was skipped
|
||||
openSkipNotice(currentUUID);
|
||||
|
||||
setTimeout(() => closeSkipNotice(currentUUID), 7000);
|
||||
|
||||
//send telemetry that a this sponsor was skipped happened
|
||||
if (trackViewCount) {
|
||||
sendRequestToServer("GET", "/api/viewedVideoSponsorTime?UUID=" + currentUUID);
|
||||
if (!skipHappened) {
|
||||
//check for the "preview" sponsors (currently edited by this user)
|
||||
for (let i = 0; i < sponsorTimesSubmitting.length; i++) {
|
||||
//must be a finished sponsor and be valid
|
||||
if (sponsorTimesSubmitting[i].length > 1 && sponsorTimesSubmitting[i][1] > sponsorTimesSubmitting[i][0]) {
|
||||
checkSponsorTime(sponsorTimesSubmitting, i, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//don't keep track until they are loaded in
|
||||
if (sponsorTimes.length > 0) {
|
||||
if (sponsorTimes != null || sponsorTimesSubmitting.length > 0) {
|
||||
lastTime = v.currentTime;
|
||||
}
|
||||
}
|
||||
|
||||
function checkSponsorTime(sponsorTimes, index, openNotice) {
|
||||
//this means part of the video was just skipped
|
||||
if (Math.abs(v.currentTime - lastTime) > 1 && lastTime != -1) {
|
||||
//make lastTime as if the video was playing normally
|
||||
lastTime = v.currentTime - 0.0001;
|
||||
}
|
||||
|
||||
if (checkIfTimeToSkip(v.currentTime, sponsorTimes[index][0])) {
|
||||
//skip it
|
||||
skipToTime(v, index, sponsorTimes, openNotice);
|
||||
|
||||
//something was skipped
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function checkIfTimeToSkip(currentVideoTime, startTime) {
|
||||
let currentTime = Date.now();
|
||||
|
||||
//If the sponsor time is in between these times, skip it
|
||||
//Checks if the last time skipped to is not too close to now, to make sure not to get too many
|
||||
// sponsor times in a row (from one troll)
|
||||
//the last term makes 0 second start times possible
|
||||
return (Math.abs(currentVideoTime - startTime) < 0.3 && startTime >= lastTime && startTime <= currentVideoTime &&
|
||||
(lastUnixTimeSkipped == -1 || currentTime - lastUnixTimeSkipped > 500)) || (lastTime == -1 && startTime == 0);
|
||||
}
|
||||
|
||||
//skip fromt he start time to the end time for a certain index sponsor time
|
||||
function skipToTime(v, index, sponsorTimes, openNotice) {
|
||||
v.currentTime = sponsorTimes[index][1];
|
||||
|
||||
lastSponsorTimeSkipped = sponsorTimes[index][0];
|
||||
|
||||
let currentUUID = UUIDs[index];
|
||||
lastSponsorTimeSkippedUUID = currentUUID;
|
||||
|
||||
if (openNotice) {
|
||||
//send out the message saying that a sponsor message was skipped
|
||||
openSkipNotice(currentUUID);
|
||||
|
||||
setTimeout(() => closeSkipNotice(currentUUID), 7000);
|
||||
|
||||
//send telemetry that a this sponsor was skipped happened
|
||||
if (trackViewCount) {
|
||||
sendRequestToServer("GET", "/api/viewedVideoSponsorTime?UUID=" + currentUUID);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function goBackToPreviousTime(UUID) {
|
||||
if (sponsorTimes != null) {
|
||||
//add a tiny bit of time to make sure it is not skipped again
|
||||
@@ -347,6 +405,25 @@ function startSponsorClicked() {
|
||||
message: "addSponsorTime",
|
||||
time: v.currentTime,
|
||||
videoID: getYouTubeVideoID(document.URL)
|
||||
}, function(response) {
|
||||
//see if the sponsorTimesSubmitting needs to be updated
|
||||
updateSponsorTimesSubmitting();
|
||||
});
|
||||
}
|
||||
|
||||
function updateSponsorTimesSubmitting() {
|
||||
chrome.runtime.sendMessage({
|
||||
message: "getSponsorTimes",
|
||||
videoID: getYouTubeVideoID(document.URL)
|
||||
}, function(response) {
|
||||
if (response != undefined) {
|
||||
let sponsorTimes = response.sponsorTimes;
|
||||
|
||||
//see if this data should be saved in the sponsorTimesSubmitting variable
|
||||
if (sponsorTimes != undefined) {
|
||||
sponsorTimesSubmitting = sponsorTimes;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -544,6 +621,9 @@ function clearSponsorTimes() {
|
||||
let sponsorTimeKey = "sponsorTimes" + currentVideoID;
|
||||
chrome.storage.sync.set({[sponsorTimeKey]: []});
|
||||
|
||||
//clear sponsor times submitting
|
||||
sponsorTimesSubmitting = [];
|
||||
|
||||
//set buttons to be correct
|
||||
changeStartSponsorButton(true, false);
|
||||
}
|
||||
@@ -557,29 +637,6 @@ function openSkipNotice(UUID){
|
||||
return;
|
||||
}
|
||||
|
||||
//check if page is loaded yet (for 0 second sponsors, the page might not be loaded yet)
|
||||
//it looks for the view count div and sees if it is full yet
|
||||
//querySelectorAll is being used like findElementById for multiple objects, because for
|
||||
//some reason YouTube has put more than one object with one ID.
|
||||
let viewCountNode = document.querySelectorAll("#count");
|
||||
//check to see if the length is over zero, otherwise it's a different YouTube theme probably
|
||||
if (viewCountNode.length > 0) {
|
||||
//check if any of these have text
|
||||
let viewCountVisible = false;
|
||||
for (let i = 0; i < viewCountNode.length; i++) {
|
||||
if (viewCountNode[i].innerText != null) {
|
||||
viewCountVisible = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!viewCountVisible) {
|
||||
//this is the new YouTube layout and it is still loading
|
||||
//wait a bit for opening the notice
|
||||
setTimeout(() => openSkipNotice(UUID), 200);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
let amountOfPreviousNotices = document.getElementsByClassName("sponsorSkipNotice").length;
|
||||
|
||||
if (amountOfPreviousNotices > 0) {
|
||||
@@ -594,7 +651,7 @@ function openSkipNotice(UUID){
|
||||
noticeElement.id = "sponsorSkipNotice" + UUID;
|
||||
noticeElement.classList.add("sponsorSkipObject");
|
||||
noticeElement.classList.add("sponsorSkipNotice");
|
||||
noticeElement.style.zIndex = 5 + amountOfPreviousNotices;
|
||||
noticeElement.style.zIndex = 50 + amountOfPreviousNotices;
|
||||
|
||||
let logoElement = document.createElement("img");
|
||||
logoElement.id = "sponsorSkipLogo" + UUID;
|
||||
@@ -664,11 +721,21 @@ function openSkipNotice(UUID){
|
||||
noticeElement.appendChild(voteButtonsContainer);
|
||||
noticeElement.appendChild(buttonContainer);
|
||||
|
||||
let referenceNode = document.getElementById("info");
|
||||
let referenceNode = document.getElementById("movie_player");
|
||||
if (referenceNode == null) {
|
||||
//old YouTube
|
||||
referenceNode = document.getElementById("watch-header");
|
||||
//for embeds
|
||||
let player = document.getElementById("player");
|
||||
referenceNode = player.firstChild;
|
||||
let index = 1;
|
||||
|
||||
//find the child that is the video player (sometimes it is not the first)
|
||||
while (!referenceNode.classList.contains("html5-video-player") || !referenceNode.classList.contains("ytp-embed")) {
|
||||
referenceNode = player.children[index];
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
referenceNode.prepend(noticeElement);
|
||||
}
|
||||
|
||||
@@ -850,8 +917,7 @@ function sendSubmitMessage(){
|
||||
submitButton.style.animation = "rotate 1s";
|
||||
//when the animation is over, hide the button
|
||||
submitButton.addEventListener("animationend", function() {
|
||||
submitButton.style.animation = "unset";
|
||||
submitButton.style.display = "none";
|
||||
changeStartSponsorButton(true, false);
|
||||
});
|
||||
|
||||
//clear the sponsor times
|
||||
@@ -957,5 +1023,10 @@ function getYouTubeVideoID(url) { // Returns with video id else returns false
|
||||
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
|
||||
var match = url.match(regExp);
|
||||
var id = new URL(url).searchParams.get("v");
|
||||
if (url.includes("/embed/")) {
|
||||
//it is an embed, don't search for v
|
||||
id = match[7];
|
||||
}
|
||||
|
||||
return (match && match[7].length == 11) ? id : false;
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@
|
||||
|
||||
<p>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></p>
|
||||
|
||||
<p>Some icons made by <a href="https://www.flaticon.com/authors/freepik" title="Freepik">Freepik</a> from <a href="https://www.flaticon.com/" title="Flaticon">www.flaticon.com</a> is licensed by <a href="http://creativecommons.org/licenses/by/3.0/" title="Creative Commons BY 3.0" target="_blank">CC 3.0 BY</a></p>
|
||||
<p>Some icons made by <a href="https://www.flaticon.com/authors/freepik" title="Freepik">Freepik</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></p>
|
||||
|
||||
</center>
|
||||
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
{
|
||||
"name": "SponsorBlock for YouTube - Skip Sponsorships",
|
||||
"short_name": "SponsorBlock",
|
||||
"version": "1.0.23",
|
||||
"version": "1.0.26",
|
||||
"description": "Skip over sponsorship on YouTube videos. Report sponsors on videos you watch to save the time of others.",
|
||||
"content_scripts": [
|
||||
{
|
||||
"matches": [
|
||||
"https://*.youtube.com/*"
|
||||
],
|
||||
"all_frames": true,
|
||||
"js": [
|
||||
"config.js",
|
||||
"content.js",
|
||||
|
||||
190
popup.js
190
popup.js
@@ -22,35 +22,45 @@ function runThePopup() {
|
||||
inPopup = false;
|
||||
}
|
||||
|
||||
// References
|
||||
let SB = {};
|
||||
|
||||
SB.sponsorStart = document.getElementById("sponsorStart");
|
||||
SB.clearTimes = document.getElementById("clearTimes");
|
||||
SB.submitTimes = document.getElementById("submitTimes");
|
||||
SB.showNoticeAgain = document.getElementById("showNoticeAgain");
|
||||
SB.hideVideoPlayerControls = document.getElementById("hideVideoPlayerControls");
|
||||
SB.showVideoPlayerControls = document.getElementById("showVideoPlayerControls");
|
||||
SB.hideInfoButtonPlayerControls = document.getElementById("hideInfoButtonPlayerControls");
|
||||
SB.showInfoButtonPlayerControls = document.getElementById("showInfoButtonPlayerControls");
|
||||
SB.hideDeleteButtonPlayerControls = document.getElementById("hideDeleteButtonPlayerControls");
|
||||
SB.showDeleteButtonPlayerControls = document.getElementById("showDeleteButtonPlayerControls");
|
||||
SB.disableSponsorViewTracking = document.getElementById("disableSponsorViewTracking");
|
||||
SB.enableSponsorViewTracking = document.getElementById("enableSponsorViewTracking");
|
||||
SB.optionsButton = document.getElementById("optionsButton");
|
||||
SB.reportAnIssue = document.getElementById("reportAnIssue");
|
||||
var SB = {};
|
||||
|
||||
["sponsorStart",
|
||||
"clearTimes",
|
||||
"submitTimes",
|
||||
"showNoticeAgain",
|
||||
"hideVideoPlayerControls",
|
||||
"showVideoPlayerControls",
|
||||
"hideInfoButtonPlayerControls",
|
||||
"showInfoButtonPlayerControls",
|
||||
"hideDeleteButtonPlayerControls",
|
||||
"showDeleteButtonPlayerControls",
|
||||
"disableSponsorViewTracking",
|
||||
"enableSponsorViewTracking",
|
||||
"optionsButton",
|
||||
"reportAnIssue",
|
||||
// sponsorTimesContributions
|
||||
SB.sponsorTimesContributionsContainer = document.getElementById("sponsorTimesContributionsContainer");
|
||||
SB.sponsorTimesContributionsDisplay = document.getElementById("sponsorTimesContributionsDisplay");
|
||||
SB.sponsorTimesContributionsDisplayEndWord = document.getElementById("sponsorTimesContributionsDisplayEndWord");
|
||||
"sponsorTimesContributionsContainer",
|
||||
"sponsorTimesContributionsDisplay",
|
||||
"sponsorTimesContributionsDisplayEndWord",
|
||||
// sponsorTimesViewsDisplay
|
||||
SB.sponsorTimesViewsContainer = document.getElementById("sponsorTimesViewsDisplayContainer");
|
||||
SB.sponsorTimesViewsDisplay = document.getElementById("sponsorTimesViewsDisplayDisplay");
|
||||
SB.sponsorTimesViewsDisplayEndWord = document.getElementById("sponsorTimesViewsDisplayDisplayEndWord");
|
||||
"sponsorTimesViewsContainer",
|
||||
"sponsorTimesViewsDisplay",
|
||||
"sponsorTimesViewsDisplayEndWord",
|
||||
// discordButtons
|
||||
SB.discordButtonContainer = document.getElementById("discordButtonContainer");
|
||||
SB.hideDiscordButton = document.getElementById("hideDiscordButton");
|
||||
|
||||
"discordButtonContainer",
|
||||
"hideDiscordButton",
|
||||
// submitTimesInfoMessage
|
||||
"submitTimesInfoMessageContainer",
|
||||
"submitTimesInfoMessage",
|
||||
// More
|
||||
"submissionSection",
|
||||
"mainControls",
|
||||
"loadingIndicator",
|
||||
"videoFound",
|
||||
"sponsorMessageTimes",
|
||||
"downloadedSponsorMessageTimes",
|
||||
].forEach(id => SB[id] = document.getElementById(id));
|
||||
|
||||
//setup click listeners
|
||||
SB.sponsorStart.addEventListener("click", sendSponsorStartMessage);
|
||||
SB.clearTimes.addEventListener("click", clearTimes);
|
||||
@@ -68,7 +78,15 @@ function runThePopup() {
|
||||
SB.reportAnIssue.addEventListener("click", reportAnIssue);
|
||||
SB.hideDiscordButton.addEventListener("click", hideDiscordButton);
|
||||
|
||||
|
||||
//setup error message languages
|
||||
var EN_US = new Map();
|
||||
|
||||
EN_US.set(400, 'Server said this request was invalid"')
|
||||
.set(429, 'You have submitted too many sponsor times for this one video, are you sure there are this many?')
|
||||
.set(409, 'This has already been submitted before')
|
||||
.set(502, 'It seems the server is down. Contact the dev to inform them.')
|
||||
.set('Unknown', 'There was an error submitting your sponsor times, please try again later.');
|
||||
|
||||
//if true, the button now selects the end time
|
||||
let startTimeChosen = false;
|
||||
|
||||
@@ -168,6 +186,7 @@ function runThePopup() {
|
||||
} else {
|
||||
SB.sponsorTimesViewsDisplayEndWord.innerText = "sponsor segment."
|
||||
}
|
||||
|
||||
SB.sponsorTimesViewsDisplay.innerText = viewCount;
|
||||
SB.sponsorTimesViewsContainer.style.display = "unset";
|
||||
}
|
||||
@@ -210,7 +229,7 @@ function runThePopup() {
|
||||
displaySponsorTimes();
|
||||
|
||||
//show submission section
|
||||
document.getElementById("submissionSection").style.display = "unset";
|
||||
SB.submissionSection.style.display = "unset";
|
||||
|
||||
showSubmitTimesIfNecessary();
|
||||
}
|
||||
@@ -238,15 +257,15 @@ function runThePopup() {
|
||||
isYouTubeTab = true;
|
||||
|
||||
//remove loading text
|
||||
document.getElementById("mainControls").style.display = "unset"
|
||||
document.getElementById("loadingIndicator").innerHTML = "";
|
||||
|
||||
SB.mainControls.style.display = "unset"
|
||||
SB.loadingIndicator.innerHTML = "";
|
||||
|
||||
if (request.found) {
|
||||
document.getElementById("videoFound").innerHTML = "This video's sponsors are in the database!"
|
||||
|
||||
SB.videoFound.innerHTML = "This video's sponsors are in the database!"
|
||||
|
||||
displayDownloadedSponsorTimes(request);
|
||||
} else {
|
||||
document.getElementById("videoFound").innerHTML = "No sponsors found"
|
||||
SB.videoFound.innerHTML = "No sponsors found"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -282,7 +301,21 @@ function runThePopup() {
|
||||
sponsorTimes[sponsorTimesIndex][startTimeChosen ? 1 : 0] = response.time;
|
||||
|
||||
let sponsorTimeKey = "sponsorTimes" + currentVideoID;
|
||||
chrome.storage.sync.set({[sponsorTimeKey]: sponsorTimes});
|
||||
let localStartTimeChosen = startTimeChosen;
|
||||
chrome.storage.sync.set({[sponsorTimeKey]: sponsorTimes}, function() {
|
||||
//send a message to the client script
|
||||
if (localStartTimeChosen) {
|
||||
chrome.tabs.query({
|
||||
active: true,
|
||||
currentWindow: true
|
||||
}, tabs => {
|
||||
chrome.tabs.sendMessage(
|
||||
tabs[0].id,
|
||||
{message: "sponsorDataChanged"}
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
updateStartTimeChosen();
|
||||
|
||||
@@ -290,31 +323,28 @@ function runThePopup() {
|
||||
displaySponsorTimes();
|
||||
|
||||
//show submission section
|
||||
document.getElementById("submissionSection").style.display = "unset";
|
||||
SB.submissionSection.style.display = "unset";
|
||||
|
||||
showSubmitTimesIfNecessary();
|
||||
}
|
||||
|
||||
//display the video times from the array
|
||||
function displaySponsorTimes() {
|
||||
//set it to the message
|
||||
let sponsorMessageTimes = document.getElementById("sponsorMessageTimes");
|
||||
|
||||
//remove all children
|
||||
while (sponsorMessageTimes.firstChild) {
|
||||
sponsorMessageTimes.removeChild(sponsorMessageTimes.firstChild);
|
||||
while (SB.sponsorMessageTimes.firstChild) {
|
||||
SB.sponsorMessageTimes.removeChild(SB.sponsorMessageTimes.firstChild);
|
||||
}
|
||||
|
||||
|
||||
//add sponsor times
|
||||
sponsorMessageTimes.appendChild(getSponsorTimesMessageDiv(sponsorTimes));
|
||||
SB.sponsorMessageTimes.appendChild(getSponsorTimesMessageDiv(sponsorTimes));
|
||||
}
|
||||
|
||||
//display the video times from the array at the top, in a different section
|
||||
function displayDownloadedSponsorTimes(request) {
|
||||
if (request.sponsorTimes != undefined) {
|
||||
//set it to the message
|
||||
document.getElementById("downloadedSponsorMessageTimes").innerHTML = getSponsorTimesMessage(request.sponsorTimes);
|
||||
|
||||
SB.downloadedSponsorMessageTimes.innerText = getSponsorTimesMessage(request.sponsorTimes);
|
||||
|
||||
//add them as buttons to the issue reporting container
|
||||
let container = document.getElementById("issueReporterTimeButtons");
|
||||
for (let i = 0; i < request.sponsorTimes.length; i++) {
|
||||
@@ -530,7 +560,17 @@ function runThePopup() {
|
||||
|
||||
//save this
|
||||
let sponsorTimeKey = "sponsorTimes" + currentVideoID;
|
||||
chrome.storage.sync.set({[sponsorTimeKey]: sponsorTimes});
|
||||
chrome.storage.sync.set({[sponsorTimeKey]: sponsorTimes}, function() {
|
||||
chrome.tabs.query({
|
||||
active: true,
|
||||
currentWindow: true
|
||||
}, tabs => {
|
||||
chrome.tabs.sendMessage(
|
||||
tabs[0].id,
|
||||
{message: "sponsorDataChanged"}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
displaySponsorTimes();
|
||||
|
||||
@@ -559,7 +599,17 @@ function runThePopup() {
|
||||
|
||||
//save this
|
||||
let sponsorTimeKey = "sponsorTimes" + currentVideoID;
|
||||
chrome.storage.sync.set({[sponsorTimeKey]: sponsorTimes});
|
||||
chrome.storage.sync.set({[sponsorTimeKey]: sponsorTimes}, function() {
|
||||
chrome.tabs.query({
|
||||
active: true,
|
||||
currentWindow: true
|
||||
}, tabs => {
|
||||
chrome.tabs.sendMessage(
|
||||
tabs[0].id,
|
||||
{message: "sponsorDataChanged"}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
//update display
|
||||
displaySponsorTimes();
|
||||
@@ -602,7 +652,17 @@ function runThePopup() {
|
||||
sponsorTimes = [];
|
||||
|
||||
let sponsorTimeKey = "sponsorTimes" + currentVideoID;
|
||||
chrome.storage.sync.set({[sponsorTimeKey]: sponsorTimes});
|
||||
chrome.storage.sync.set({[sponsorTimeKey]: sponsorTimes}, function() {
|
||||
chrome.tabs.query({
|
||||
active: true,
|
||||
currentWindow: true
|
||||
}, tabs => {
|
||||
chrome.tabs.sendMessage(
|
||||
tabs[0].id,
|
||||
{message: "sponsorDataChanged"}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
displaySponsorTimes();
|
||||
|
||||
@@ -612,10 +672,15 @@ function runThePopup() {
|
||||
resetStartTimeChosen();
|
||||
}
|
||||
|
||||
function getErrorMessage(lang, statusCode) {
|
||||
if(lang.has(statusCode)) return lang.get(statusCode);
|
||||
return lang.get('Unknown').concat(" Error code: ") + statusCode;
|
||||
}
|
||||
|
||||
function submitTimes() {
|
||||
//make info message say loading
|
||||
document.getElementById("submitTimesInfoMessage").innerText = "Loading...";
|
||||
document.getElementById("submitTimesInfoMessageContainer").style.display = "unset";
|
||||
SB.submitTimesInfoMessage.innerText = "Loading...";
|
||||
SB.submitTimesInfoMessageContainer.style.display = "unset";
|
||||
|
||||
if (sponsorTimes.length > 0) {
|
||||
chrome.runtime.sendMessage({
|
||||
@@ -625,24 +690,16 @@ function runThePopup() {
|
||||
if (response != undefined) {
|
||||
if (response.statusCode == 200) {
|
||||
//hide loading message
|
||||
document.getElementById("submitTimesInfoMessageContainer").style.display = "none";
|
||||
|
||||
SB.submitTimesInfoMessageContainer.style.display = "none";
|
||||
|
||||
clearTimes();
|
||||
} else if(response.statusCode == 400) {
|
||||
document.getElementById("submitTimesInfoMessage").innerText = "Server said this request was invalid";
|
||||
document.getElementById("submitTimesInfoMessageContainer").style.display = "unset";
|
||||
} else if(response.statusCode == 429) {
|
||||
document.getElementById("submitTimesInfoMessage").innerText = "You have submitted too many sponsor times for this one video, are you sure there are this many?";
|
||||
document.getElementById("submitTimesInfoMessageContainer").style.display = "unset";
|
||||
} else if(response.statusCode == 409) {
|
||||
document.getElementById("submitTimesInfoMessage").innerText = "This has already been submitted before";
|
||||
document.getElementById("submitTimesInfoMessageContainer").style.display = "unset";
|
||||
} else if(response.statusCode == 502) {
|
||||
document.getElementById("submitTimesInfoMessage").innerText = "It seems the server is down. Contact the dev to inform them. Error code " + response.statusCode;
|
||||
document.getElementById("submitTimesInfoMessageContainer").style.display = "unset";
|
||||
} else {
|
||||
document.getElementById("submitTimesInfoMessage").innerText = "There was an error submitting your sponsor times, please try again later. Error code " + response.statusCode;
|
||||
let errorMessage = getErrorMessage(EN_US, response.statusCode);
|
||||
|
||||
document.getElementById("submitTimesInfoMessage").innerText = errorMessage;
|
||||
document.getElementById("submitTimesInfoMessageContainer").style.display = "unset";
|
||||
|
||||
SB.submitTimesInfoMessageContainer.style.display = "unset";
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -963,5 +1020,4 @@ if (chrome.tabs != undefined) {
|
||||
|
||||
//this means it is actually opened in the popup
|
||||
runThePopup();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user