diff --git a/background.js b/background.js index 396cff91..bba98675 100644 --- a/background.js +++ b/background.js @@ -1,25 +1,34 @@ +var previousVideoID = null + chrome.tabs.onUpdated.addListener( // On tab update function(tabId, changeInfo, tab) { if (changeInfo != undefined && changeInfo.url != undefined) { let id = getYouTubeVideoID(changeInfo.url); if (changeInfo.url && id) { // If URL changed and is youtube video message contentScript the video id + videoIDChange(id); + chrome.tabs.sendMessage( tabId, { message: 'ytvideoid', id: id - }) + }); } } } ); + + chrome.runtime.onMessage.addListener(function (request, sender, callback) { - console.log(request.message) if (request.message == "submitTimes") { submitTimes(request.videoID); callback({ success: true }); + } else if(request.message == "ytvideoid") { + if (previousVideoID != request.videoID) { + videoIDChange(request.videoID); + } } }); @@ -29,7 +38,7 @@ function submitTimes(videoID) { chrome.storage.local.get([sponsorTimeKey], function(result) { let videoTimes = result[sponsorTimeKey]; - if (videoTimes != undefined && result.videoTimes != []) { + if (videoTimes != undefined && videoTimes != []) { //submit these times for (let i = 0; i < videoTimes.length; i++) { let xmlhttp = new XMLHttpRequest(); @@ -41,6 +50,33 @@ function submitTimes(videoID) { }); } +function videoIDChange(currentVideoID) { + //warn them if they had unsubmitted times + if (previousVideoID != null) { + //get the sponsor times from storage + let sponsorTimeKey = 'videoTimes' + previousVideoID; + chrome.storage.local.get([sponsorTimeKey], function(result) { + let videoTimes = result[sponsorTimeKey]; + + if (videoTimes != undefined && videoTimes.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: "icon.png" + }); + } + + //set the previous video id to the currentID + previousVideoID = currentVideoID; + }); + } else { + console.log(currentVideoID) + previousVideoID = currentVideoID; + } +} + function getYouTubeVideoID(url) { // Return video id or false var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/; var match = url.match(regExp); diff --git a/content.js b/content.js index 72b87fad..2a4c0718 100644 --- a/content.js +++ b/content.js @@ -2,6 +2,12 @@ if(id = getYouTubeVideoID(document.URL)){ // Direct Links //reset sponsor data found check sponsorDataFound = false; sponsorsLookup(id); + + //tell background.js about this + chrome.runtime.sendMessage({ + message: "ytvideoid", + videoID: id + }); } //was sponsor data found when doing SponsorsLookup diff --git a/icon.png b/icon.png new file mode 100644 index 00000000..242daa29 Binary files /dev/null and b/icon.png differ diff --git a/manifest.json b/manifest.json index f1f04b61..eec69d12 100644 --- a/manifest.json +++ b/manifest.json @@ -14,7 +14,8 @@ ], "permissions": [ "tabs", - "storage" + "storage", + "notifications" ], "browser_action": { "default_title": "SponsorBlock", diff --git a/popup.js b/popup.js index be457b72..decd0ea3 100644 --- a/popup.js +++ b/popup.js @@ -35,12 +35,14 @@ function loadTabData(tabs) { let videoTimeKey = "videoTimes" + currentVideoID; chrome.storage.local.get([videoTimeKey], function(result) { videoTimes = result[videoTimeKey]; - if (videoTimes != undefined && result.videoTimes != []) { + if (videoTimes != undefined && videoTimes != []) { if (videoTimes[videoTimes.length - 1]!= undefined && videoTimes[videoTimes.length - 1].length < 2) { startTimeChosen = true; } displayVideoTimes(); + } else { + videoTimes = [] } });