2019-07-10 04:10:25 +02:00
|
|
|
//setup click listeners
|
2019-07-09 05:43:06 +02:00
|
|
|
document.getElementById("sponsorStart").addEventListener("click", sendSponsorStartMessage);
|
|
|
|
document.getElementById("clearTimes").addEventListener("click", clearTimes);
|
2019-07-09 21:55:33 +02:00
|
|
|
document.getElementById("submitTimes").addEventListener("click", submitTimes);
|
2019-07-10 04:10:25 +02:00
|
|
|
document.getElementById("showNoticeAgain").addEventListener("click", showNoticeAgain);
|
2019-07-09 05:43:06 +02:00
|
|
|
|
|
|
|
//if true, the button now selects the end time
|
|
|
|
var startTimeChosen = false;
|
|
|
|
|
|
|
|
//the start and end time pairs (2d)
|
2019-07-10 01:46:55 +02:00
|
|
|
var sponsorTimes = [];
|
2019-07-09 05:43:06 +02:00
|
|
|
|
2019-07-09 22:36:06 +02:00
|
|
|
//current video ID of this tab
|
|
|
|
var currentVideoID = null;
|
2019-07-09 05:43:06 +02:00
|
|
|
|
2019-07-09 23:51:49 +02:00
|
|
|
//is this a YouTube tab?
|
|
|
|
var isYouTubeTab = false;
|
|
|
|
|
2019-07-10 04:10:25 +02:00
|
|
|
//if the don't show notice again variable is true, an option to
|
|
|
|
// disable should be available
|
|
|
|
chrome.storage.local.get(["dontShowNoticeAgain"], function(result) {
|
|
|
|
let dontShowNoticeAgain = result.dontShowNoticeAgain;
|
|
|
|
if (dontShowNoticeAgain != undefined && dontShowNoticeAgain) {
|
|
|
|
document.getElementById("showNoticeAgain").style.display = "unset";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-07-09 23:51:49 +02:00
|
|
|
//if no response comes by this point, give up
|
|
|
|
setTimeout(function() {
|
|
|
|
if (!isYouTubeTab) {
|
2019-07-10 00:13:46 +02:00
|
|
|
document.getElementById("loadingIndicator").innerHTML = "This probably isn't a YouTube tab, or you clicked too early. " +
|
2019-07-09 23:51:49 +02:00
|
|
|
"If you know this is a YouTube tab, close this popup and open it again.";
|
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
|
2019-07-09 06:05:27 +02:00
|
|
|
chrome.tabs.query({
|
|
|
|
active: true,
|
|
|
|
currentWindow: true
|
2019-07-09 22:36:06 +02:00
|
|
|
}, loadTabData);
|
|
|
|
|
|
|
|
function loadTabData(tabs) {
|
|
|
|
//set current videoID
|
|
|
|
currentVideoID = getYouTubeVideoID(tabs[0].url);
|
|
|
|
|
|
|
|
//load video times for this video
|
2019-07-10 01:46:55 +02:00
|
|
|
let sponsorTimeKey = "sponsorTimes" + currentVideoID;
|
|
|
|
chrome.storage.local.get([sponsorTimeKey], function(result) {
|
2019-07-10 03:53:07 +02:00
|
|
|
let sponsorTimesStorage = result[sponsorTimeKey];
|
2019-07-10 04:23:35 +02:00
|
|
|
if (sponsorTimesStorage != undefined && sponsorTimesStorage.length > 0) {
|
2019-07-10 03:53:07 +02:00
|
|
|
if (sponsorTimesStorage[sponsorTimesStorage.length - 1] != undefined && sponsorTimesStorage[sponsorTimesStorage.length - 1].length < 2) {
|
2019-07-09 22:36:06 +02:00
|
|
|
startTimeChosen = true;
|
|
|
|
}
|
|
|
|
|
2019-07-10 03:53:07 +02:00
|
|
|
sponsorTimes = sponsorTimesStorage;
|
|
|
|
|
2019-07-10 01:46:55 +02:00
|
|
|
displaySponsorTimes();
|
2019-07-10 04:23:35 +02:00
|
|
|
|
|
|
|
//show submission section
|
|
|
|
document.getElementById("submissionSection").style.display = "unset";
|
2019-07-09 22:36:06 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//check if this video's sponsors are known
|
2019-07-09 06:05:27 +02:00
|
|
|
chrome.tabs.sendMessage(
|
|
|
|
tabs[0].id,
|
2019-07-10 04:10:25 +02:00
|
|
|
{message: 'isInfoFound'},
|
2019-07-09 06:05:27 +02:00
|
|
|
infoFound
|
|
|
|
);
|
2019-07-09 22:36:06 +02:00
|
|
|
}
|
2019-07-09 21:55:33 +02:00
|
|
|
|
2019-07-09 06:05:27 +02:00
|
|
|
function infoFound(request) {
|
2019-07-09 18:07:39 +02:00
|
|
|
//if request is undefined, then the page currently being browsed is not YouTube
|
|
|
|
if (request != undefined) {
|
2019-07-09 23:51:49 +02:00
|
|
|
//this must be a YouTube video
|
|
|
|
//set variable
|
|
|
|
isYouTubeTab = true;
|
|
|
|
|
|
|
|
//remove loading text
|
|
|
|
document.getElementById("mainControls").style.display = "unset"
|
|
|
|
document.getElementById("loadingIndicator").innerHTML = "";
|
|
|
|
|
2019-07-09 18:07:39 +02:00
|
|
|
if (request.found) {
|
|
|
|
document.getElementById("videoFound").innerHTML = "This video's sponsors are in the database!"
|
2019-07-10 00:03:56 +02:00
|
|
|
|
2019-07-10 01:46:55 +02:00
|
|
|
displayDownloadedSponsorTimes(request);
|
2019-07-09 18:07:39 +02:00
|
|
|
} else {
|
|
|
|
document.getElementById("videoFound").innerHTML = "No sponsors found"
|
|
|
|
}
|
2019-07-09 06:05:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 21:55:33 +02:00
|
|
|
function setVideoID(request) {
|
|
|
|
//if request is undefined, then the page currently being browsed is not YouTube
|
|
|
|
if (request != undefined) {
|
|
|
|
videoID = request.videoID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 05:43:06 +02:00
|
|
|
function sendSponsorStartMessage() {
|
|
|
|
//the content script will get the message if a YouTube page is open
|
|
|
|
chrome.tabs.query({
|
|
|
|
active: true,
|
|
|
|
currentWindow: true
|
|
|
|
}, tabs => {
|
|
|
|
chrome.tabs.sendMessage(
|
|
|
|
tabs[0].id,
|
2019-07-09 06:05:27 +02:00
|
|
|
{from: 'popup', message: 'sponsorStart'}
|
2019-07-09 05:43:06 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
chrome.runtime.onMessage.addListener(function (request, sender, callback) {
|
|
|
|
if (request.message == "time") {
|
2019-07-10 01:46:55 +02:00
|
|
|
let sponsorTimesIndex = sponsorTimes.length - (startTimeChosen ? 1 : 0);
|
2019-07-09 05:43:06 +02:00
|
|
|
|
2019-07-10 01:46:55 +02:00
|
|
|
if (sponsorTimes[sponsorTimesIndex] == undefined) {
|
|
|
|
sponsorTimes[sponsorTimesIndex] = [];
|
2019-07-09 05:43:06 +02:00
|
|
|
}
|
|
|
|
|
2019-07-10 01:46:55 +02:00
|
|
|
sponsorTimes[sponsorTimesIndex][startTimeChosen ? 1 : 0] = request.time;
|
2019-07-09 05:43:06 +02:00
|
|
|
|
2019-07-10 01:46:55 +02:00
|
|
|
let sponsorTimeKey = "sponsorTimes" + currentVideoID;
|
|
|
|
chrome.storage.local.set({[sponsorTimeKey]: sponsorTimes});
|
2019-07-09 05:43:06 +02:00
|
|
|
|
|
|
|
//update startTimeChosen variable
|
|
|
|
if (!startTimeChosen) {
|
|
|
|
startTimeChosen = true;
|
2019-07-10 04:23:35 +02:00
|
|
|
document.getElementById("sponsorStart").innerHTML = "Sponsorship Ends Now";
|
2019-07-09 05:43:06 +02:00
|
|
|
} else {
|
|
|
|
startTimeChosen = false;
|
2019-07-10 04:23:35 +02:00
|
|
|
document.getElementById("sponsorStart").innerHTML = "Sponsorship Starts Now";
|
2019-07-09 05:43:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//display video times on screen
|
2019-07-10 01:46:55 +02:00
|
|
|
displaySponsorTimes();
|
2019-07-10 04:23:35 +02:00
|
|
|
|
|
|
|
//show submission section
|
|
|
|
document.getElementById("submissionSection").style.display = "unset";
|
2019-07-09 05:43:06 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
//display the video times from the array
|
2019-07-10 01:46:55 +02:00
|
|
|
function displaySponsorTimes() {
|
2019-07-10 00:03:56 +02:00
|
|
|
//set it to the message
|
2019-07-10 01:46:55 +02:00
|
|
|
document.getElementById("sponsorMessageTimes").innerHTML = getSponsorTimesMessage(sponsorTimes);
|
2019-07-10 00:03:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//display the video times from the array at the top, in a different section
|
2019-07-10 01:46:55 +02:00
|
|
|
function displayDownloadedSponsorTimes(request) {
|
2019-07-10 00:03:56 +02:00
|
|
|
if (request.sponsorTimes != undefined) {
|
|
|
|
//set it to the message
|
2019-07-10 01:46:55 +02:00
|
|
|
document.getElementById("downloadedSponsorMessageTimes").innerHTML = getSponsorTimesMessage(request.sponsorTimes);
|
2019-07-10 00:03:56 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-09 05:43:06 +02:00
|
|
|
|
2019-07-10 00:03:56 +02:00
|
|
|
//get the message that visually displays the video times
|
2019-07-10 01:46:55 +02:00
|
|
|
function getSponsorTimesMessage(sponsorTimes) {
|
2019-07-10 00:03:56 +02:00
|
|
|
let sponsorTimesMessage = "";
|
|
|
|
|
|
|
|
for (let i = 0; i < sponsorTimes.length; i++) {
|
|
|
|
for (let s = 0; s < sponsorTimes[i].length; s++) {
|
2019-07-10 00:10:45 +02:00
|
|
|
let timeMessage = getFormattedTime(sponsorTimes[i][s]);
|
2019-07-09 05:43:06 +02:00
|
|
|
//if this is an end time
|
|
|
|
if (s == 1) {
|
|
|
|
timeMessage = " to " + timeMessage;
|
|
|
|
} else if (i > 0) {
|
|
|
|
//add commas if necessary
|
|
|
|
timeMessage = ", " + timeMessage;
|
|
|
|
}
|
|
|
|
|
2019-07-10 00:03:56 +02:00
|
|
|
sponsorTimesMessage += timeMessage;
|
2019-07-09 05:43:06 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-10 00:03:56 +02:00
|
|
|
|
|
|
|
return sponsorTimesMessage;
|
2019-07-09 05:43:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function clearTimes() {
|
2019-07-10 01:46:55 +02:00
|
|
|
sponsorTimes = [];
|
2019-07-09 05:43:06 +02:00
|
|
|
|
2019-07-10 01:46:55 +02:00
|
|
|
let sponsorTimeKey = "sponsorTimes" + currentVideoID;
|
|
|
|
chrome.storage.local.set({[sponsorTimeKey]: sponsorTimes});
|
2019-07-09 05:43:06 +02:00
|
|
|
|
2019-07-10 01:46:55 +02:00
|
|
|
displaySponsorTimes();
|
2019-07-10 04:23:35 +02:00
|
|
|
|
|
|
|
//hide submission section
|
|
|
|
document.getElementById("submissionSection").style.display = "none";
|
2019-07-09 21:55:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function submitTimes() {
|
2019-07-10 03:53:07 +02:00
|
|
|
if (sponsorTimes.length > 0) {
|
|
|
|
chrome.runtime.sendMessage({
|
|
|
|
message: "submitTimes",
|
|
|
|
videoID: currentVideoID
|
|
|
|
}, function(request) {
|
|
|
|
clearTimes();
|
|
|
|
});
|
|
|
|
}
|
2019-07-09 22:36:06 +02:00
|
|
|
}
|
|
|
|
|
2019-07-10 04:10:25 +02:00
|
|
|
function showNoticeAgain() {
|
|
|
|
chrome.storage.local.set({"dontShowNoticeAgain": false});
|
|
|
|
|
|
|
|
chrome.tabs.query({
|
|
|
|
active: true,
|
|
|
|
currentWindow: true
|
|
|
|
}, function(tabs) {
|
|
|
|
chrome.tabs.sendMessage(tabs[0].id, {
|
|
|
|
message: "showNoticeAgain"
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
document.getElementById("showNoticeAgain").style.display = "none";
|
|
|
|
}
|
|
|
|
|
2019-07-10 00:10:45 +02:00
|
|
|
//converts time in seconds to minutes:seconds
|
|
|
|
function getFormattedTime(seconds) {
|
|
|
|
let minutes = Math.floor(seconds / 60);
|
|
|
|
let secondsDisplay = Math.round(seconds - minutes * 60);
|
|
|
|
let formatted = minutes+ ":" + secondsDisplay;
|
|
|
|
|
|
|
|
return formatted;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|