diff --git a/background.js b/background.js index 64c7813c..aa25e379 100644 --- a/background.js +++ b/background.js @@ -147,11 +147,29 @@ function submitVote(type, UUID, callback) { function submitTimes(videoID, callback) { //get the video times from storage let sponsorTimeKey = 'sponsorTimes' + videoID; - chrome.storage.sync.get([sponsorTimeKey, "userID"], function(result) { + chrome.storage.sync.get([sponsorTimeKey, "userID"], async function(result) { let sponsorTimes = result[sponsorTimeKey]; let userID = result.userID; if (sponsorTimes != undefined && sponsorTimes.length > 0) { + let durationResult = await new Promise((resolve, reject) => { + chrome.tabs.query({ + active: true, + currentWindow: true + }, function(tabs) { + chrome.tabs.sendMessage(tabs[0].id, { + message: "getVideoDuration" + }, (response) => resolve(response)); + }); + }); + + //check if a sponsor exceeds the duration of the video + for (let i = 0; i < sponsorTimes.length; i++) { + if (sponsorTimes[i][1] > durationResult.duration) { + sponsorTimes[i][1] = durationResult.duration; + } + } + //submit these times for (let i = 0; i < sponsorTimes.length; i++) { //submit the sponsorTime diff --git a/content.js b/content.js index e88b602b..c63ef7c5 100644 --- a/content.js +++ b/content.js @@ -128,6 +128,12 @@ function messageListener(request, sender, sendResponse) { }) } + if (request.message == "getVideoDuration") { + sendResponse({ + duration: v.duration + }); + } + if (request.message == "skipToTime") { v.currentTime = request.time; } @@ -329,6 +335,9 @@ function sponsorsLookup(id) { } //check database for sponsor times + + //made true once a setTimeout has been created to try again after a server error + let recheckStarted = false; sendRequestToServer('GET', "/api/getVideoSponsorTimes?videoID=" + id, function(xmlhttp) { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { sponsorDataFound = true; @@ -364,7 +373,9 @@ function sponsorsLookup(id) { }); sponsorLookupRetries = 0; - } else if (xmlhttp.readyState == 4 && sponsorLookupRetries < 90) { + } else if (xmlhttp.readyState == 4 && sponsorLookupRetries < 90 && !recheckStarted) { + recheckStarted = true; + //some error occurred, try again in a second setTimeout(() => sponsorsLookup(id), 1000); @@ -953,6 +964,15 @@ function submitSponsorTimes() { let sponsorTimes = result[sponsorTimeKey]; if (sponsorTimes != undefined && sponsorTimes.length > 0) { + //check if a sponsor exceeds the duration of the video + for (let i = 0; i < sponsorTimes.length; i++) { + if (sponsorTimes[i][1] > v.duration) { + sponsorTimes[i][1] = v.duration; + } + } + //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"); if(!confirm(confirmMessage)) return; diff --git a/popup.js b/popup.js index e8d1c59d..39358977 100644 --- a/popup.js +++ b/popup.js @@ -981,7 +981,7 @@ function runThePopup() { //set it to false function resetStartTimeChosen() { startTimeChosen = false; - SB.sponsorStart.innerHTML = "SP_START"; + SB.sponsorStart.innerHTML = chrome.i18n.getMessage("sponsorStart"); } //hides and shows the submit times button when needed @@ -1278,7 +1278,7 @@ function runThePopup() { if (chrome.tabs != undefined) { //add the width restriction (because Firefox) - document.getElementById("sponorBlockStyleSheet").sheet.insertRule('.popupBody { width: 300 }', 0); + document.getElementById("sponorBlockStyleSheet").sheet.insertRule('.popupBody { width: 325 }', 0); //this means it is actually opened in the popup runThePopup();