Added warning if you leave a page before submitting your sponsor times.

This commit is contained in:
Ajay Ramachandran 2019-07-09 18:59:27 -04:00
parent fcdb091d66
commit a1f645ea0e
5 changed files with 50 additions and 5 deletions

View file

@ -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);

View file

@ -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

BIN
icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

View file

@ -14,7 +14,8 @@
],
"permissions": [
"tabs",
"storage"
"storage",
"notifications"
],
"browser_action": {
"default_title": "SponsorBlock",

View file

@ -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 = []
}
});