2019-07-10 00:59:27 +02:00
|
|
|
var previousVideoID = null
|
|
|
|
|
2019-01-18 21:00:57 +01:00
|
|
|
chrome.tabs.onUpdated.addListener( // On tab update
|
2019-01-15 19:24:44 +01:00
|
|
|
function(tabId, changeInfo, tab) {
|
2019-07-09 05:43:06 +02:00
|
|
|
if (changeInfo != undefined && changeInfo.url != undefined) {
|
2019-07-09 21:55:33 +02:00
|
|
|
let id = getYouTubeVideoID(changeInfo.url);
|
2019-07-09 05:43:06 +02:00
|
|
|
if (changeInfo.url && id) { // If URL changed and is youtube video message contentScript the video id
|
2019-07-10 00:59:27 +02:00
|
|
|
videoIDChange(id);
|
|
|
|
|
2019-07-09 05:43:06 +02:00
|
|
|
chrome.tabs.sendMessage( tabId, {
|
|
|
|
message: 'ytvideoid',
|
|
|
|
id: id
|
2019-07-10 00:59:27 +02:00
|
|
|
});
|
2019-07-09 05:43:06 +02:00
|
|
|
}
|
2019-01-15 19:24:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2019-07-09 05:43:06 +02:00
|
|
|
|
2019-07-10 00:59:27 +02:00
|
|
|
|
|
|
|
|
2019-07-09 21:55:33 +02:00
|
|
|
chrome.runtime.onMessage.addListener(function (request, sender, callback) {
|
|
|
|
if (request.message == "submitTimes") {
|
|
|
|
submitTimes(request.videoID);
|
2019-07-09 22:36:06 +02:00
|
|
|
|
|
|
|
callback({
|
|
|
|
success: true
|
|
|
|
});
|
2019-07-10 00:59:27 +02:00
|
|
|
} else if(request.message == "ytvideoid") {
|
|
|
|
if (previousVideoID != request.videoID) {
|
|
|
|
videoIDChange(request.videoID);
|
|
|
|
}
|
2019-07-09 21:55:33 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function submitTimes(videoID) {
|
|
|
|
//get the video times from storage
|
2019-07-10 01:46:55 +02:00
|
|
|
let sponsorTimeKey = 'sponsorTimes' + videoID;
|
2019-07-09 22:36:06 +02:00
|
|
|
chrome.storage.local.get([sponsorTimeKey], function(result) {
|
2019-07-10 01:46:55 +02:00
|
|
|
let sponsorTimes = result[sponsorTimeKey];
|
2019-07-09 21:55:33 +02:00
|
|
|
|
2019-07-10 04:23:35 +02:00
|
|
|
if (sponsorTimes != undefined && sponsorTimes.length > 0) {
|
2019-07-09 21:55:33 +02:00
|
|
|
//submit these times
|
2019-07-10 01:46:55 +02:00
|
|
|
for (let i = 0; i < sponsorTimes.length; i++) {
|
2019-07-09 21:55:33 +02:00
|
|
|
let xmlhttp = new XMLHttpRequest();
|
|
|
|
//submit the sponsorTime
|
2019-07-10 01:46:55 +02:00
|
|
|
xmlhttp.open('GET', 'http://localhost/api/postVideoSponsorTimes?videoID=' + videoID + "&startTime=" + sponsorTimes[i][0] + "&endTime=" + sponsorTimes[i][1], true);
|
2019-07-09 21:55:33 +02:00
|
|
|
xmlhttp.send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-07-09 22:36:06 +02:00
|
|
|
}
|
|
|
|
|
2019-07-10 00:59:27 +02:00
|
|
|
function videoIDChange(currentVideoID) {
|
|
|
|
//warn them if they had unsubmitted times
|
|
|
|
if (previousVideoID != null) {
|
|
|
|
//get the sponsor times from storage
|
2019-07-10 01:46:55 +02:00
|
|
|
let sponsorTimeKey = 'sponsorTimes' + previousVideoID;
|
2019-07-10 00:59:27 +02:00
|
|
|
chrome.storage.local.get([sponsorTimeKey], function(result) {
|
2019-07-10 01:46:55 +02:00
|
|
|
let sponsorTimes = result[sponsorTimeKey];
|
2019-07-10 00:59:27 +02:00
|
|
|
|
2019-07-10 01:46:55 +02:00
|
|
|
if (sponsorTimes != undefined && sponsorTimes.length > 0) {
|
2019-07-10 00:59:27 +02:00
|
|
|
//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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 22:36:06 +02:00
|
|
|
function getYouTubeVideoID(url) { // Return video id or false
|
|
|
|
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
|
|
|
|
var match = url.match(regExp);
|
|
|
|
return (match && match[7].length == 11) ? match[7] : false;
|
2019-07-09 05:43:06 +02:00
|
|
|
}
|