2019-07-09 20:50:19 +02:00
|
|
|
if(id = getYouTubeVideoID(document.URL)){ // Direct Links
|
2019-07-09 23:03:44 +02:00
|
|
|
//reset sponsor data found check
|
|
|
|
sponsorDataFound = false;
|
2019-07-09 20:50:19 +02:00
|
|
|
sponsorsLookup(id);
|
2019-07-10 00:59:27 +02:00
|
|
|
|
|
|
|
//tell background.js about this
|
|
|
|
chrome.runtime.sendMessage({
|
|
|
|
message: "ytvideoid",
|
|
|
|
videoID: id
|
|
|
|
});
|
2019-01-18 20:49:43 +01:00
|
|
|
}
|
|
|
|
|
2019-07-09 20:50:19 +02:00
|
|
|
//was sponsor data found when doing SponsorsLookup
|
|
|
|
var sponsorDataFound = false;
|
|
|
|
|
2019-07-10 00:03:56 +02:00
|
|
|
//the actual sponsorTimes if loaded
|
|
|
|
var sponsorTimes = undefined;
|
|
|
|
|
2019-07-09 21:05:16 +02:00
|
|
|
//the video
|
|
|
|
var v;
|
|
|
|
|
|
|
|
//the last time looked at (used to see if this time is in the interval)
|
|
|
|
var lastTime;
|
2019-01-18 20:49:43 +01:00
|
|
|
|
2019-07-10 03:44:41 +02:00
|
|
|
//the last time in the video a sponsor was skipped
|
|
|
|
//used for the go back button
|
|
|
|
var lastSponsorTimeSkipped = null;
|
|
|
|
|
2019-07-10 04:10:25 +02:00
|
|
|
//if the notice should not be shown
|
|
|
|
//happens when the user click's the "Don't show notice again" button
|
|
|
|
var dontShowNotice = false;
|
|
|
|
chrome.storage.local.get(["dontShowNoticeAgain"], function(result) {
|
|
|
|
let dontShowNoticeAgain = result.dontShowNoticeAgain;
|
|
|
|
if (dontShowNoticeAgain != undefined) {
|
|
|
|
dontShowNotice = dontShowNoticeAgain;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-01-18 20:59:34 +01:00
|
|
|
chrome.runtime.onMessage.addListener( // Detect URL Changes
|
2019-01-15 19:29:25 +01:00
|
|
|
function(request, sender, sendResponse) {
|
2019-07-09 23:03:44 +02:00
|
|
|
//message from background script
|
2019-07-10 04:10:25 +02:00
|
|
|
if (request.message == "ytvideoid") {
|
2019-07-09 23:03:44 +02:00
|
|
|
//reset sponsor data found check
|
|
|
|
sponsorDataFound = false;
|
|
|
|
sponsorsLookup(request.id);
|
2019-01-15 19:29:25 +01:00
|
|
|
}
|
2019-07-09 05:43:06 +02:00
|
|
|
|
2019-07-09 06:05:27 +02:00
|
|
|
//messages from popup script
|
2019-07-10 04:10:25 +02:00
|
|
|
if (request.message == "sponsorStart") {
|
2019-07-09 05:43:06 +02:00
|
|
|
sponsorMessageStarted();
|
|
|
|
}
|
2019-07-09 06:05:27 +02:00
|
|
|
|
2019-07-10 04:10:25 +02:00
|
|
|
if (request.message == "isInfoFound") {
|
2019-07-10 00:03:56 +02:00
|
|
|
//send the sponsor times along with if it's found
|
2019-07-09 06:05:27 +02:00
|
|
|
sendResponse({
|
2019-07-10 00:03:56 +02:00
|
|
|
found: sponsorDataFound,
|
|
|
|
sponsorTimes: sponsorTimes
|
2019-07-09 06:05:27 +02:00
|
|
|
})
|
|
|
|
}
|
2019-07-09 21:55:33 +02:00
|
|
|
|
2019-07-10 04:10:25 +02:00
|
|
|
if (request.message == "getVideoID") {
|
2019-07-09 21:55:33 +02:00
|
|
|
sendResponse({
|
|
|
|
videoID: getYouTubeVideoID(document.URL)
|
|
|
|
})
|
|
|
|
}
|
2019-07-10 04:10:25 +02:00
|
|
|
|
|
|
|
if (request.message == "showNoticeAgain") {
|
|
|
|
dontShowNotice = false;
|
|
|
|
}
|
2019-01-15 19:29:25 +01:00
|
|
|
});
|
2018-06-23 17:09:05 +02:00
|
|
|
|
2019-07-09 20:50:19 +02:00
|
|
|
function sponsorsLookup(id) {
|
2019-01-18 20:59:34 +01:00
|
|
|
v = document.querySelector('video') // Youtube video player
|
2019-07-09 21:05:16 +02:00
|
|
|
let xmlhttp = new XMLHttpRequest();
|
2019-07-09 20:50:19 +02:00
|
|
|
|
|
|
|
//check database for sponsor times
|
2019-07-10 04:52:59 +02:00
|
|
|
xmlhttp.open('GET', serverAddresss + "/api/getVideoSponsorTimes?videoID=" + id, true);
|
2019-07-09 20:50:19 +02:00
|
|
|
|
2018-06-23 17:09:05 +02:00
|
|
|
xmlhttp.onreadystatechange = function () {
|
|
|
|
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
|
2019-07-09 23:03:44 +02:00
|
|
|
sponsorDataFound = true;
|
2019-07-09 20:50:19 +02:00
|
|
|
|
2019-07-09 23:03:44 +02:00
|
|
|
sponsorTimes = JSON.parse(xmlhttp.responseText).sponsorTimes;
|
|
|
|
|
|
|
|
// If the sponsor data exists, add the event to run on the videos "ontimeupdate"
|
|
|
|
v.ontimeupdate = function () {
|
|
|
|
sponsorCheck(sponsorTimes);
|
|
|
|
};
|
2019-07-09 22:36:06 +02:00
|
|
|
} else {
|
|
|
|
sponsorDataFound = false;
|
2018-06-23 17:09:05 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
xmlhttp.send(null);
|
2018-06-23 16:06:18 +02:00
|
|
|
}
|
2018-06-23 17:09:05 +02:00
|
|
|
|
2019-07-09 21:05:16 +02:00
|
|
|
function sponsorCheck(sponsorTimes) { // Video skipping
|
|
|
|
//see if any sponsor start time was just passed
|
|
|
|
sponsorTimes.forEach(function (sponsorTime, index) { // Foreach Sponsor in video
|
|
|
|
//the sponsor time is in between these times, skip it
|
|
|
|
//if the time difference is more than 1 second, than the there was probably a skip in time,
|
|
|
|
// and it's not due to playback
|
|
|
|
if (Math.abs(v.currentTime - lastTime) < 1 && sponsorTime[0] >= lastTime && sponsorTime[0] <= v.currentTime) {
|
|
|
|
//skip it
|
|
|
|
v.currentTime = sponsorTime[1];
|
2019-07-10 03:31:21 +02:00
|
|
|
|
2019-07-10 03:44:41 +02:00
|
|
|
lastSponsorTimeSkipped = sponsorTime[0];
|
|
|
|
|
2019-07-10 03:31:21 +02:00
|
|
|
//send out the message saying that a sponsor message was skipped
|
|
|
|
openSkipNotice();
|
|
|
|
|
2019-07-11 01:25:29 +02:00
|
|
|
setTimeout(closeSkipNotice, 2500);
|
2018-06-23 17:09:05 +02:00
|
|
|
}
|
2019-07-09 21:05:16 +02:00
|
|
|
|
|
|
|
lastTime = v.currentTime;
|
2018-06-23 17:09:05 +02:00
|
|
|
});
|
|
|
|
}
|
2019-01-18 20:49:43 +01:00
|
|
|
|
2019-07-10 03:44:41 +02:00
|
|
|
function goBackToPreviousTime() {
|
|
|
|
if (lastSponsorTimeSkipped != null) {
|
|
|
|
//add a tiny bit of time to make sure it is not skipped again
|
|
|
|
v.currentTime = lastSponsorTimeSkipped + 0.001;
|
|
|
|
|
|
|
|
closeSkipNotice();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 03:31:21 +02:00
|
|
|
//Opens the notice that tells the user that a sponsor was just skipped
|
2019-07-10 03:15:43 +02:00
|
|
|
function openSkipNotice(){
|
2019-07-10 04:10:25 +02:00
|
|
|
if (dontShowNotice) {
|
|
|
|
//don't show, return
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-10 03:15:43 +02:00
|
|
|
var noticeElement = document.createElement("div");
|
2019-07-10 20:43:14 +02:00
|
|
|
noticeElement.id = "sponsorSkipNotice";
|
2019-07-10 21:11:55 +02:00
|
|
|
noticeElement.className = "sponsorSkipObject";
|
2019-07-10 20:43:14 +02:00
|
|
|
|
|
|
|
var noticeMessage = document.createElement("p");
|
|
|
|
noticeMessage.id = "sponsorSkipMessage";
|
2019-07-10 21:11:55 +02:00
|
|
|
noticeMessage.className = "sponsorSkipObject";
|
2019-07-10 03:15:43 +02:00
|
|
|
noticeMessage.innerText = "Hey, you just skipped a sponsor!";
|
|
|
|
|
2019-07-10 03:44:41 +02:00
|
|
|
var buttonContainer = document.createElement("div");
|
|
|
|
buttonContainer.setAttribute("align", "center");
|
|
|
|
|
|
|
|
var goBackButton = document.createElement("button");
|
2019-07-10 20:43:14 +02:00
|
|
|
goBackButton.innerText = "Go back";
|
2019-07-10 21:11:55 +02:00
|
|
|
goBackButton.className = "sponsorSkipObject";
|
2019-07-10 20:43:14 +02:00
|
|
|
goBackButton.className = "sponsorSkipButton";
|
2019-07-10 03:44:41 +02:00
|
|
|
goBackButton.addEventListener("click", goBackToPreviousTime);
|
|
|
|
|
2019-07-10 03:46:11 +02:00
|
|
|
var hideButton = document.createElement("button");
|
2019-07-10 21:11:55 +02:00
|
|
|
hideButton.innerText = "Hide";
|
|
|
|
hideButton.className = "sponsorSkipObject";
|
2019-07-10 20:43:14 +02:00
|
|
|
hideButton.className = "sponsorSkipButton";
|
2019-07-10 03:46:11 +02:00
|
|
|
hideButton.addEventListener("click", closeSkipNotice);
|
|
|
|
|
2019-07-10 04:10:25 +02:00
|
|
|
var dontShowAgainButton = document.createElement("button");
|
2019-07-10 21:11:55 +02:00
|
|
|
dontShowAgainButton.innerText = "Don't Show This Again";
|
|
|
|
dontShowAgainButton.className = "sponsorSkipObject";
|
2019-07-10 20:43:14 +02:00
|
|
|
dontShowAgainButton.className = "sponsorSkipDontShowButton";
|
2019-07-10 04:10:25 +02:00
|
|
|
dontShowAgainButton.addEventListener("click", dontShowNoticeAgain);
|
|
|
|
|
2019-07-10 03:44:41 +02:00
|
|
|
buttonContainer.appendChild(goBackButton);
|
2019-07-10 03:46:11 +02:00
|
|
|
buttonContainer.appendChild(hideButton);
|
2019-07-10 04:12:47 +02:00
|
|
|
buttonContainer.appendChild(document.createElement("br"));
|
2019-07-10 20:43:14 +02:00
|
|
|
buttonContainer.appendChild(document.createElement("br"));
|
2019-07-10 04:10:25 +02:00
|
|
|
buttonContainer.appendChild(dontShowAgainButton);
|
2019-07-10 03:44:41 +02:00
|
|
|
|
|
|
|
noticeElement.appendChild(noticeMessage);
|
|
|
|
noticeElement.appendChild(buttonContainer);
|
2019-07-10 03:15:43 +02:00
|
|
|
|
|
|
|
var referenceNode = document.getElementById("info");
|
|
|
|
if (referenceNode == null) {
|
|
|
|
//old YouTube
|
|
|
|
referenceNode = document.getElementById("watch-header");
|
|
|
|
}
|
|
|
|
referenceNode.prepend(noticeElement);
|
2019-01-18 20:49:43 +01:00
|
|
|
}
|
2019-07-09 05:43:06 +02:00
|
|
|
|
2019-07-10 03:31:21 +02:00
|
|
|
//Closes the notice that tells the user that a sponsor was just skipped
|
|
|
|
function closeSkipNotice(){
|
2019-07-10 03:44:41 +02:00
|
|
|
let notice = document.getElementById("sponsorSkipNotice");
|
|
|
|
if (notice != null) {
|
|
|
|
notice.remove();
|
|
|
|
}
|
2019-07-10 03:31:21 +02:00
|
|
|
}
|
|
|
|
|
2019-07-10 04:10:25 +02:00
|
|
|
function dontShowNoticeAgain() {
|
|
|
|
chrome.storage.local.set({"dontShowNoticeAgain": true});
|
|
|
|
|
|
|
|
dontShowNotice = true;
|
|
|
|
|
|
|
|
closeSkipNotice();
|
|
|
|
}
|
|
|
|
|
2019-07-09 05:43:06 +02:00
|
|
|
function sponsorMessageStarted() {
|
|
|
|
let v = document.querySelector('video');
|
|
|
|
|
|
|
|
console.log(v.currentTime)
|
|
|
|
|
|
|
|
//send back current time
|
|
|
|
chrome.runtime.sendMessage({
|
|
|
|
message: "time",
|
|
|
|
time: v.currentTime
|
|
|
|
});
|
2019-07-10 03:15:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getYouTubeVideoID(url) { // Returns with video id else returns 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
|
|
|
}
|