Made the onvideo control start with the stop icon if needed.

This commit is contained in:
Ajay Ramachandran 2019-07-12 18:28:41 -04:00
parent 8e783e8466
commit baa85cc7d3
2 changed files with 44 additions and 12 deletions

View file

@ -26,26 +26,42 @@ chrome.runtime.onMessage.addListener(function (request, sender, callback) {
callback({
success: true
});
} else if(request.message == "ytvideoid") {
} else if (request.message == "ytvideoid") {
if (previousVideoID != request.videoID) {
videoIDChange(request.videoID);
}
} else if(request.message == "addSponsorTime") {
} else if (request.message == "addSponsorTime") {
addSponsorTime(request.time);
} else if (request.message == "getSponsorTimes") {
getSponsorTimes(request.videoID, function(sponsorTimes) {
callback({
sponsorTimes: sponsorTimes
})
});
//this allows the callback to be called later
return true;
}
});
function addSponsorTime(time) {
//get sponsor times
//gets the sponsor times from memory
function getSponsorTimes(videoID, callback) {
let sponsorTimes = [];
let sponsorTimeKey = "sponsorTimes" + previousVideoID;
let sponsorTimeKey = "sponsorTimes" + videoID;
chrome.storage.local.get([sponsorTimeKey], function(result) {
let sponsorTimesStorage = result[sponsorTimeKey];
if (sponsorTimesStorage != undefined && sponsorTimesStorage.length > 0) {
sponsorTimes = sponsorTimesStorage;
}
//add to sponsorTimes
callback(sponsorTimes)
});
}
function addSponsorTime(time) {
getSponsorTimes(previousVideoID, function(sponsorTimes) {
//add to sponsorTimes
if (sponsorTimes.length > 0 && sponsorTimes[sponsorTimes.length - 1].length < 2) {
//it is an end time
sponsorTimes[sponsorTimes.length - 1][1] = parseInt(time);
@ -58,6 +74,7 @@ function addSponsorTime(time) {
}
//save this info
let sponsorTimeKey = "sponsorTimes" + previousVideoID;
chrome.storage.local.set({[sponsorTimeKey]: sponsorTimes});
});
}

View file

@ -1,7 +1,5 @@
if(id = getYouTubeVideoID(document.URL)){ // Direct Links
//reset sponsor data found check
sponsorDataFound = false;
sponsorsLookup(id);
videoIDChange(id);
//tell background.js about this
chrome.runtime.sendMessage({
@ -43,9 +41,7 @@ chrome.runtime.onMessage.addListener( // Detect URL Changes
function(request, sender, sendResponse) {
//message from background script
if (request.message == "ytvideoid") {
//reset sponsor data found check
sponsorDataFound = false;
sponsorsLookup(request.id);
videoIDChange(request.id);
}
//messages from popup script
@ -76,6 +72,25 @@ chrome.runtime.onMessage.addListener( // Detect URL Changes
}
});
function videoIDChange(id) {
//reset sponsor data found check
sponsorDataFound = false;
sponsorsLookup(id);
//see if the onvideo control image needs to be changed
chrome.runtime.sendMessage({
message: "getSponsorTimes",
videoID: id
}, function(response) {
if (response != undefined) {
let sponsorTimes = response.sponsorTimes;
if (sponsorTimes != undefined && sponsorTimes.length > 0 && sponsorTimes[sponsorTimes.length - 1].length < 2) {
toggleStartSponsorButton();
}
}
});
}
function sponsorsLookup(id) {
v = document.querySelector('video') // Youtube video player
let xmlhttp = new XMLHttpRequest();