diff --git a/background.js b/background.js index 752653ec..c8d7d593 100644 --- a/background.js +++ b/background.js @@ -1,27 +1,7 @@ -//when a new tab is highlighted -chrome.tabs.onActivated.addListener( - function(activeInfo) { - chrome.tabs.get(activeInfo.tabId, function(tab) { - let id = getYouTubeVideoID(tab.url); - - //if this even is a YouTube tab - if (id) { - videoIDChange(id, activeInfo.tabId); - } - }) - } -); - -//when a tab changes URLs chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { - if (changeInfo != undefined && changeInfo.url != undefined) { - let id = getYouTubeVideoID(changeInfo.url); - - //if URL changed and is youtube video message contentScript the video id - if (changeInfo.url && id) { - videoIDChange(id, tabId); - } - } + chrome.tabs.sendMessage(tabId, { + message: 'update', + }); }); chrome.runtime.onMessage.addListener(function (request, sender, callback) { @@ -49,7 +29,14 @@ chrome.runtime.onMessage.addListener(function (request, sender, callback) { //this allows the callback to be called later return true; - } + } else if (request.message == "alertPrevious") { + chrome.notifications.create("stillThere" + Math.random(), { + type: "basic", + title: "Do you want to submit the sponsor times for video id " + request.previousVideoID + "?", + message: "You seem to have left some sponsor times unsubmitted. Go back to that page to submit them (they are not deleted).", + iconUrl: "./icons/LogoSponsorBlocker256px.png" + }); + } }); //add help page on install @@ -154,7 +141,7 @@ function submitVote(type, UUID, callback) { function submitTimes(videoID, callback) { //get the video times from storage let sponsorTimeKey = 'sponsorTimes' + videoID; - chrome.storage.sync.get([sponsorTimeKey], function(result) { + chrome.storage.sync.get([sponsorTimeKey, "userID"], function(result) { let sponsorTimes = result[sponsorTimeKey]; let userID = result.userID; @@ -193,56 +180,6 @@ function submitTimes(videoID, callback) { }); } -function videoIDChange(currentVideoID, tabId) { - //send a message to the content script - chrome.tabs.sendMessage(tabId, { - message: 'ytvideoid', - id: currentVideoID - }); - - chrome.storage.sync.get(["sponsorVideoID", "previousVideoID"], function(result) { - const sponsorVideoID = result.sponsorVideoID; - const previousVideoID = result.previousVideoID; - - //not a url change - if (sponsorVideoID == currentVideoID){ - return; - } - - chrome.storage.sync.set({ - "sponsorVideoID": currentVideoID - }); - - //warn them if they had unsubmitted times - if (previousVideoID != null) { - //get the sponsor times from storage - let sponsorTimeKey = 'sponsorTimes' + previousVideoID; - chrome.storage.sync.get([sponsorTimeKey], function(result) { - let sponsorTimes = result[sponsorTimeKey]; - - if (sponsorTimes != undefined && sponsorTimes.length > 0) { - //warn them that they have unsubmitted sponsor times - chrome.notifications.create("stillThere" + Math.random(), { - type: "basic", - title: "Do you want to submit the sponsor times for watch?v=" + previousVideoID + "?", - message: "You seem to have left some sponsor times unsubmitted. Go back to that page to submit them (they are not deleted).", - iconUrl: "./icons/LogoSponsorBlocker256px.png" - }); - } - - //set the previous video id to the currentID - chrome.storage.sync.set({ - "previousVideoID": currentVideoID - }); - }); - } else { - chrome.storage.sync.set({ - "previousVideoID": currentVideoID - }); - } - }); -} - function sendRequestToServer(type, address, callback) { let xmlhttp = new XMLHttpRequest(); diff --git a/content.js b/content.js index d4269fea..6c5227d2 100644 --- a/content.js +++ b/content.js @@ -1,6 +1,6 @@ //was sponsor data found when doing SponsorsLookup var sponsorDataFound = false; - +var previousVideoID = null; //the actual sponsorTimes if loaded and UUIDs associated with them var sponsorTimes = null; var UUIDs = null; @@ -81,14 +81,14 @@ chrome.storage.sync.get(["dontShowNoticeAgain"], function(result) { //get messages from the background script and the popup chrome.runtime.onMessage.addListener(messageListener); - + function messageListener(request, sender, sendResponse) { - //message from background script - if (request.message == "ytvideoid") { - videoIDChange(request.id); - } - //messages from popup script + + if (request.message == "update") { + if(id = getYouTubeVideoID(document.URL)) videoIDChange(id); + } + if (request.message == "sponsorStart") { sponsorMessageStarted(sendResponse); } @@ -119,6 +119,16 @@ function messageListener(request, sender, sendResponse) { }) } + if (request.message == "skipToTime") { + v.currentTime = request.time; + } + + if (request.message == "getCurrentTime") { + sendResponse({ + currentTime: v.currentTime + }); + } + if (request.message == "getChannelURL") { sendResponse({ channelURL: channelURL @@ -183,11 +193,33 @@ document.onkeydown = function(e){ } function videoIDChange(id) { - //not a url change - if (sponsorVideoID == id){ - return; - } + //not a url change + if (sponsorVideoID == id) return; + + //warn them if they had unsubmitted times + if (previousVideoID != null) { + //get the sponsor times from storage + let sponsorTimeKey = 'sponsorTimes' + previousVideoID; + chrome.storage.sync.get([sponsorTimeKey], function(result) { + let sponsorTimes = result[sponsorTimeKey]; + + if (sponsorTimes != undefined && sponsorTimes.length > 0) { + //warn them that they have unsubmitted sponsor times + chrome.runtime.sendMessage({ + message: "alertPrevious", + previousVideoID: previousVideoID + }) + } + + //set the previous video id to the currentID + previousVideoID = id; + }); + } else { + //set the previous id now, don't wait for chrome.storage.get + previousVideoID = id; + } + //close popup closeInfoMenu(); diff --git a/manifest.json b/manifest.json index 215ea951..a7450545 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "name": "__MSG_fullName__", "short_name": "__MSG_Name__", - "version": "1.0.31", + "version": "1.0.33", "default_locale": "en", "description": "__MSG_Description__", "content_scripts": [ @@ -37,7 +37,6 @@ "popup.html" ], "permissions": [ - "tabs", "storage", "notifications", "https://sponsor.ajay.app/*" diff --git a/popup.css b/popup.css index cb86b962..1d5dd91e 100644 --- a/popup.css +++ b/popup.css @@ -80,8 +80,14 @@ h1.popupElement { .mediumLink.popupElement { font-size: 15px; - padding-left: 15px; - padding-right: 15px; + margin-left: 25px; + margin-right: 25px; + text-decoration: underline; + cursor: pointer; +} + +.tinyLink.popupElement { + font-size: 10px; text-decoration: underline; cursor: pointer; } diff --git a/popup.html b/popup.html index 30195294..82c2d66c 100644 --- a/popup.html +++ b/popup.html @@ -93,6 +93,8 @@ + +
diff --git a/popup.js b/popup.js index ded54fef..149c546b 100644 --- a/popup.js +++ b/popup.js @@ -192,17 +192,25 @@ function runThePopup() { } }); - chrome.tabs.query({ active: true, currentWindow: true - }, loadTabData); + }, onTabs); + function onTabs(tabs) { + chrome.tabs.sendMessage(tabs[0].id, {message: 'getVideoID'}, function(result) { + if (result != undefined && result.videoID) { + currentVideoID = result.videoID; + + loadTabData(tabs); + } else if (result == undefined && chrome.runtime.lastError) { + //this isn't a YouTube video then, or at least the content script is not loaded + displayNoVideo(); + } + }); + } function loadTabData(tabs) { - //set current videoID - currentVideoID = getYouTubeVideoID(tabs[0].url); - if (!currentVideoID) { //this isn't a YouTube video then displayNoVideo(); @@ -456,8 +464,11 @@ function runThePopup() { let index = i; deleteButton.addEventListener("click", () => deleteSponsorTime(index)); - let spacer = document.createElement("span"); - spacer.innerText = " "; + let previewButton = document.createElement("span"); + previewButton.id = "sponsorTimePreviewButton" + i; + previewButton.innerText = "Preview"; + previewButton.className = "mediumLink popupElement"; + previewButton.addEventListener("click", () => previewSponsorTime(index)); let editButton = document.createElement("span"); editButton.id = "sponsorTimeEditButton" + i; @@ -479,19 +490,46 @@ function runThePopup() { } currentSponsorTimeContainer.innerText = currentSponsorTimeMessage; - currentSponsorTimeContainer.addEventListener("click", () => editSponsorTime(index)); sponsorTimesContainer.appendChild(currentSponsorTimeContainer); sponsorTimesContainer.appendChild(deleteButton); //only if it is a complete sponsor time if (sponsorTimes[i].length > 1) { + sponsorTimesContainer.appendChild(previewButton); sponsorTimesContainer.appendChild(editButton); + + currentSponsorTimeContainer.addEventListener("click", () => editSponsorTime(index)); } } return sponsorTimesContainer; } + + function previewSponsorTime(index) { + let skipTime = sponsorTimes[index][0]; + + if (document.getElementById("startTimeMinutes" + index) != null) { + //edit is currently open, use that time + + skipTime = getSponsorTimeEditTimes("startTime", index); + + //save the edit + saveSponsorTimeEdit(index, false); + } + + chrome.tabs.query({ + active: true, + currentWindow: true + }, tabs => { + chrome.tabs.sendMessage( + tabs[0].id, { + message: "skipToTime", + time: skipTime - 2 + } + ); + }); + } function editSponsorTime(index) { if (document.getElementById("startTimeMinutes" + index) != null) { @@ -504,6 +542,13 @@ function runThePopup() { let sponsorTimeContainer = document.getElementById("sponsorTimeContainer" + index); + //the button to set the current time + let startTimeNowButton = document.createElement("span"); + startTimeNowButton.id = "startTimeNowButton" + index; + startTimeNowButton.innerText = "(Now)"; + startTimeNowButton.className = "tinyLink popupElement"; + startTimeNowButton.addEventListener("click", () => setEditTimeToCurrentTime("startTime", index)); + //get sponsor time minutes and seconds boxes let startTimeMinutes = document.createElement("input"); startTimeMinutes.id = "startTimeMinutes" + index; @@ -518,7 +563,7 @@ function runThePopup() { startTimeSeconds.type = "text"; startTimeSeconds.value = getTimeInFormattedSeconds(sponsorTimes[index][0]); startTimeSeconds.style.width = "60px"; - + let endTimeMinutes = document.createElement("input"); endTimeMinutes.id = "endTimeMinutes" + index; endTimeMinutes.className = "sponsorTime popupElement"; @@ -532,6 +577,13 @@ function runThePopup() { endTimeSeconds.type = "text"; endTimeSeconds.value = getTimeInFormattedSeconds(sponsorTimes[index][1]); endTimeSeconds.style.width = "60px"; + + //the button to set the current time + let endTimeNowButton = document.createElement("span"); + endTimeNowButton.id = "endTimeNowButton" + index; + endTimeNowButton.innerText = "(Now)"; + endTimeNowButton.className = "tinyLink popupElement"; + endTimeNowButton.addEventListener("click", () => setEditTimeToCurrentTime("endTime", index)); let colonText = document.createElement("span"); colonText.innerText = ":"; @@ -544,6 +596,7 @@ function runThePopup() { sponsorTimeContainer.removeChild(sponsorTimeContainer.firstChild); } + sponsorTimeContainer.appendChild(startTimeNowButton); sponsorTimeContainer.appendChild(startTimeMinutes); sponsorTimeContainer.appendChild(colonText); sponsorTimeContainer.appendChild(startTimeSeconds); @@ -551,6 +604,7 @@ function runThePopup() { sponsorTimeContainer.appendChild(endTimeMinutes); sponsorTimeContainer.appendChild(colonText); sponsorTimeContainer.appendChild(endTimeSeconds); + sponsorTimeContainer.appendChild(endTimeNowButton); //add save button and remove edit button let saveButton = document.createElement("span"); @@ -564,16 +618,37 @@ function runThePopup() { sponsorTimesContainer.replaceChild(saveButton, editButton); } + + function setEditTimeToCurrentTime(idStartName, index) { + chrome.tabs.query({ + active: true, + currentWindow: true + }, tabs => { + chrome.tabs.sendMessage( + tabs[0].id, + {message: "getCurrentTime"}, + function (response) { + let minutes = document.getElementById(idStartName + "Minutes" + index); + let seconds = document.getElementById(idStartName + "Seconds" + index); + + minutes.value = getTimeInMinutes(response.currentTime); + seconds.value = getTimeInFormattedSeconds(response.currentTime); + }); + }); + } + + //id start name is whether it is the startTime or endTime + //gives back the time in seconds + function getSponsorTimeEditTimes(idStartName, index) { + let minutes = document.getElementById(idStartName + "Minutes" + index); + let seconds = document.getElementById(idStartName + "Seconds" + index); + + return parseInt(minutes.value) * 60 + parseFloat(seconds.value); + } - function saveSponsorTimeEdit(index) { - let startTimeMinutes = document.getElementById("startTimeMinutes" + index); - let startTimeSeconds = document.getElementById("startTimeSeconds" + index); - - let endTimeMinutes = document.getElementById("endTimeMinutes" + index); - let endTimeSeconds = document.getElementById("endTimeSeconds" + index); - - sponsorTimes[index][0] = parseInt(startTimeMinutes.value) * 60 + parseFloat(startTimeSeconds.value); - sponsorTimes[index][1] = parseInt(endTimeMinutes.value) * 60 + parseFloat(endTimeSeconds.value); + function saveSponsorTimeEdit(index, closeEditMode = true) { + sponsorTimes[index][0] = getSponsorTimeEditTimes("startTime", index); + sponsorTimes[index][1] = getSponsorTimeEditTimes("endTime", index); //save this let sponsorTimeKey = "sponsorTimes" + currentVideoID; @@ -589,9 +664,11 @@ function runThePopup() { }); }); - displaySponsorTimes(); + if (closeEditMode) { + displaySponsorTimes(); - showSubmitTimesIfNecessary(); + showSubmitTimesIfNecessary(); + } } //deletes the sponsor time submitted at an index