mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2024-11-10 09:07:45 +01:00
Added preview before uploading submissions.
This commit is contained in:
parent
ca8404147d
commit
932702cca1
2 changed files with 187 additions and 63 deletions
198
content.js
198
content.js
|
@ -34,6 +34,13 @@ var hideVideoPlayerControls = false;
|
||||||
var hideInfoButtonPlayerControls = false;
|
var hideInfoButtonPlayerControls = false;
|
||||||
var hideDeleteButtonPlayerControls = false;
|
var hideDeleteButtonPlayerControls = false;
|
||||||
|
|
||||||
|
//the downloaded sponsor times
|
||||||
|
var sponsorTimes = [];
|
||||||
|
var UUIDs = [];
|
||||||
|
|
||||||
|
//the sponsor times being prepared to be submitted
|
||||||
|
var sponsorTimesSubmitting = [];
|
||||||
|
|
||||||
//becomes true when isInfoFound is called
|
//becomes true when isInfoFound is called
|
||||||
//this is used to close the popup on YouTube when the other popup opens
|
//this is used to close the popup on YouTube when the other popup opens
|
||||||
var popupInitialised = false;
|
var popupInitialised = false;
|
||||||
|
@ -73,6 +80,10 @@ function messageListener(request, sender, sendResponse) {
|
||||||
sponsorMessageStarted(sendResponse);
|
sponsorMessageStarted(sendResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (request.message == "sponsorDataChanged") {
|
||||||
|
updateSponsorTimesSubmitting();
|
||||||
|
}
|
||||||
|
|
||||||
if (request.message == "isInfoFound") {
|
if (request.message == "isInfoFound") {
|
||||||
//send the sponsor times along with if it's found
|
//send the sponsor times along with if it's found
|
||||||
sendResponse({
|
sendResponse({
|
||||||
|
@ -160,6 +171,9 @@ function videoIDChange(id) {
|
||||||
sponsorDataFound = false;
|
sponsorDataFound = false;
|
||||||
sponsorsLookup(id);
|
sponsorsLookup(id);
|
||||||
|
|
||||||
|
//reset sponsor times submitting
|
||||||
|
sponsorTimesSubmitting = [];
|
||||||
|
|
||||||
//see if the onvideo control image needs to be changed
|
//see if the onvideo control image needs to be changed
|
||||||
chrome.runtime.sendMessage({
|
chrome.runtime.sendMessage({
|
||||||
message: "getSponsorTimes",
|
message: "getSponsorTimes",
|
||||||
|
@ -174,6 +188,11 @@ function videoIDChange(id) {
|
||||||
} else {
|
} else {
|
||||||
changeStartSponsorButton(true, false);
|
changeStartSponsorButton(true, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//see if this data should be saved in the sponsorTimesSubmitting variable
|
||||||
|
if (sponsorTimes != undefined && sponsorTimes.length > 0) {
|
||||||
|
sponsorTimesSubmitting = sponsorTimes;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -203,82 +222,121 @@ function videoIDChange(id) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function sponsorsLookup(id) {
|
function sponsorsLookup(id) {
|
||||||
v = document.querySelector('video') // Youtube video player
|
v = document.querySelector('video') // Youtube video player
|
||||||
|
|
||||||
//check database for sponsor times
|
//check database for sponsor times
|
||||||
sendRequestToServer('GET', "/api/getVideoSponsorTimes?videoID=" + id, function(xmlhttp) {
|
sendRequestToServer('GET', "/api/getVideoSponsorTimes?videoID=" + id, function(xmlhttp) {
|
||||||
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
|
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
|
||||||
sponsorDataFound = true;
|
sponsorDataFound = true;
|
||||||
|
|
||||||
sponsorTimes = JSON.parse(xmlhttp.responseText).sponsorTimes;
|
sponsorTimes = JSON.parse(xmlhttp.responseText).sponsorTimes;
|
||||||
UUIDs = JSON.parse(xmlhttp.responseText).UUIDs;
|
UUIDs = JSON.parse(xmlhttp.responseText).UUIDs;
|
||||||
|
} else if (xmlhttp.readyState == 4) {
|
||||||
|
sponsorDataFound = false;
|
||||||
|
|
||||||
// If the sponsor data exists, add the event to run on the videos "ontimeupdate"
|
//check if this video was uploaded recently
|
||||||
v.ontimeupdate = function () {
|
//use the invidious api to get the time published
|
||||||
sponsorCheck(sponsorTimes);
|
sendRequestToCustomServer('GET', "https://invidio.us/api/v1/videos/" + id, function(xmlhttp, error) {
|
||||||
};
|
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
|
||||||
} else if (xmlhttp.readyState == 4) {
|
let unixTimePublished = JSON.parse(xmlhttp.responseText).published;
|
||||||
sponsorDataFound = false;
|
|
||||||
|
|
||||||
//check if this video was uploaded recently
|
//if less than 3 days old
|
||||||
//use the invidious api to get the time published
|
if ((Date.now() / 1000) - unixTimePublished < 259200) {
|
||||||
sendRequestToCustomServer('GET', "https://invidio.us/api/v1/videos/" + id, function(xmlhttp, error) {
|
setTimeout(() => sponsorsLookup(id), 10000);
|
||||||
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
|
|
||||||
let unixTimePublished = JSON.parse(xmlhttp.responseText).published;
|
|
||||||
|
|
||||||
//if less than 3 days old
|
|
||||||
if ((Date.now() / 1000) - unixTimePublished < 259200) {
|
|
||||||
setTimeout(() => sponsorsLookup(id), 10000);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
});
|
||||||
});
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
//add the event to run on the videos "ontimeupdate"
|
||||||
|
v.ontimeupdate = function () {
|
||||||
|
sponsorCheck();
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function sponsorCheck(sponsorTimes) { // Video skipping
|
//video skipping
|
||||||
//see if any sponsor start time was just passed
|
function sponsorCheck() {
|
||||||
for (let i = 0; i < sponsorTimes.length; i++) {
|
let skipHappened = false;
|
||||||
//this means part of the video was just skipped
|
|
||||||
if (Math.abs(v.currentTime - lastTime) > 1 && lastTime != -1) {
|
if (sponsorTimes != null) {
|
||||||
//make lastTime as if the video was playing normally
|
//see if any sponsor start time was just passed
|
||||||
lastTime = v.currentTime - 0.0001;
|
for (let i = 0; i < sponsorTimes.length; i++) {
|
||||||
|
//if something was skipped
|
||||||
|
if (checkSponsorTime(sponsorTimes, i), true) {
|
||||||
|
skipHappened = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let currentTime = Date.now();
|
if (!skipHappened) {
|
||||||
|
//check for the "preview" sponsors (currently edited by this user)
|
||||||
//If the sponsor time is in between these times, skip it
|
for (let i = 0; i < sponsorTimesSubmitting.length; i++) {
|
||||||
//Checks if the last time skipped to is not too close to now, to make sure not to get too many
|
//must be a finished sponsor and be valid
|
||||||
// sponsor times in a row (from one troll)
|
if (sponsorTimesSubmitting[i].length > 1 && sponsorTimesSubmitting[i][1] > sponsorTimesSubmitting[i][0]) {
|
||||||
//the last term makes 0 second start times possible
|
checkSponsorTime(sponsorTimesSubmitting, i, false);
|
||||||
if ((Math.abs(v.currentTime - sponsorTimes[i][0]) < 0.3 && sponsorTimes[i][0] >= lastTime && sponsorTimes[i][0] <= v.currentTime
|
|
||||||
&& (lastUnixTimeSkipped == -1 || currentTime - lastUnixTimeSkipped > 500)) || (lastTime == -1 && sponsorTimes[i][0] == 0)) {
|
|
||||||
//skip it
|
|
||||||
v.currentTime = sponsorTimes[i][1];
|
|
||||||
|
|
||||||
lastSponsorTimeSkipped = sponsorTimes[i][0];
|
|
||||||
|
|
||||||
let currentUUID = UUIDs[i];
|
|
||||||
lastSponsorTimeSkippedUUID = currentUUID;
|
|
||||||
|
|
||||||
//send out the message saying that a sponsor message was skipped
|
|
||||||
openSkipNotice(currentUUID);
|
|
||||||
|
|
||||||
setTimeout(() => closeSkipNotice(currentUUID), 7000);
|
|
||||||
|
|
||||||
//send telemetry that a this sponsor was skipped happened
|
|
||||||
if (trackViewCount) {
|
|
||||||
sendRequestToServer("GET", "/api/viewedVideoSponsorTime?UUID=" + currentUUID);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//don't keep track until they are loaded in
|
//don't keep track until they are loaded in
|
||||||
if (sponsorTimes.length > 0) {
|
if (sponsorTimes != null || sponsorTimesSubmitting.length > 0) {
|
||||||
lastTime = v.currentTime;
|
lastTime = v.currentTime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function checkSponsorTime(sponsorTimes, index, openNotice) {
|
||||||
|
//this means part of the video was just skipped
|
||||||
|
if (Math.abs(v.currentTime - lastTime) > 1 && lastTime != -1) {
|
||||||
|
//make lastTime as if the video was playing normally
|
||||||
|
lastTime = v.currentTime - 0.0001;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (checkIfTimeToSkip(v.currentTime, sponsorTimes[index][0])) {
|
||||||
|
//skip it
|
||||||
|
skipToTime(v, index, sponsorTimes, openNotice);
|
||||||
|
|
||||||
|
//something was skipped
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkIfTimeToSkip(currentVideoTime, startTime) {
|
||||||
|
let currentTime = Date.now();
|
||||||
|
|
||||||
|
//If the sponsor time is in between these times, skip it
|
||||||
|
//Checks if the last time skipped to is not too close to now, to make sure not to get too many
|
||||||
|
// sponsor times in a row (from one troll)
|
||||||
|
//the last term makes 0 second start times possible
|
||||||
|
return (Math.abs(currentVideoTime - startTime) < 0.3 && startTime >= lastTime && startTime <= currentVideoTime &&
|
||||||
|
(lastUnixTimeSkipped == -1 || currentTime - lastUnixTimeSkipped > 500)) || (lastTime == -1 && startTime == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
//skip fromt he start time to the end time for a certain index sponsor time
|
||||||
|
function skipToTime(v, index, sponsorTimes, openNotice) {
|
||||||
|
v.currentTime = sponsorTimes[index][1];
|
||||||
|
|
||||||
|
lastSponsorTimeSkipped = sponsorTimes[index][0];
|
||||||
|
|
||||||
|
let currentUUID = UUIDs[index];
|
||||||
|
lastSponsorTimeSkippedUUID = currentUUID;
|
||||||
|
|
||||||
|
if (openNotice) {
|
||||||
|
//send out the message saying that a sponsor message was skipped
|
||||||
|
openSkipNotice(currentUUID);
|
||||||
|
|
||||||
|
setTimeout(() => closeSkipNotice(currentUUID), 7000);
|
||||||
|
|
||||||
|
//send telemetry that a this sponsor was skipped happened
|
||||||
|
if (trackViewCount) {
|
||||||
|
sendRequestToServer("GET", "/api/viewedVideoSponsorTime?UUID=" + currentUUID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function goBackToPreviousTime(UUID) {
|
function goBackToPreviousTime(UUID) {
|
||||||
if (sponsorTimes != null) {
|
if (sponsorTimes != null) {
|
||||||
//add a tiny bit of time to make sure it is not skipped again
|
//add a tiny bit of time to make sure it is not skipped again
|
||||||
|
@ -347,6 +405,25 @@ function startSponsorClicked() {
|
||||||
message: "addSponsorTime",
|
message: "addSponsorTime",
|
||||||
time: v.currentTime,
|
time: v.currentTime,
|
||||||
videoID: getYouTubeVideoID(document.URL)
|
videoID: getYouTubeVideoID(document.URL)
|
||||||
|
}, function(response) {
|
||||||
|
//see if the sponsorTimesSubmitting needs to be updated
|
||||||
|
updateSponsorTimesSubmitting();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateSponsorTimesSubmitting() {
|
||||||
|
chrome.runtime.sendMessage({
|
||||||
|
message: "getSponsorTimes",
|
||||||
|
videoID: getYouTubeVideoID(document.URL)
|
||||||
|
}, function(response) {
|
||||||
|
if (response != undefined) {
|
||||||
|
let sponsorTimes = response.sponsorTimes;
|
||||||
|
|
||||||
|
//see if this data should be saved in the sponsorTimesSubmitting variable
|
||||||
|
if (sponsorTimes != undefined) {
|
||||||
|
sponsorTimesSubmitting = sponsorTimes;
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -544,6 +621,9 @@ function clearSponsorTimes() {
|
||||||
let sponsorTimeKey = "sponsorTimes" + currentVideoID;
|
let sponsorTimeKey = "sponsorTimes" + currentVideoID;
|
||||||
chrome.storage.sync.set({[sponsorTimeKey]: []});
|
chrome.storage.sync.set({[sponsorTimeKey]: []});
|
||||||
|
|
||||||
|
//clear sponsor times submitting
|
||||||
|
sponsorTimesSubmitting = [];
|
||||||
|
|
||||||
//set buttons to be correct
|
//set buttons to be correct
|
||||||
changeStartSponsorButton(true, false);
|
changeStartSponsorButton(true, false);
|
||||||
}
|
}
|
||||||
|
|
52
popup.js
52
popup.js
|
@ -301,7 +301,21 @@ function runThePopup() {
|
||||||
sponsorTimes[sponsorTimesIndex][startTimeChosen ? 1 : 0] = response.time;
|
sponsorTimes[sponsorTimesIndex][startTimeChosen ? 1 : 0] = response.time;
|
||||||
|
|
||||||
let sponsorTimeKey = "sponsorTimes" + currentVideoID;
|
let sponsorTimeKey = "sponsorTimes" + currentVideoID;
|
||||||
chrome.storage.sync.set({[sponsorTimeKey]: sponsorTimes});
|
let localStartTimeChosen = startTimeChosen;
|
||||||
|
chrome.storage.sync.set({[sponsorTimeKey]: sponsorTimes}, function() {
|
||||||
|
//send a message to the client script
|
||||||
|
if (localStartTimeChosen) {
|
||||||
|
chrome.tabs.query({
|
||||||
|
active: true,
|
||||||
|
currentWindow: true
|
||||||
|
}, tabs => {
|
||||||
|
chrome.tabs.sendMessage(
|
||||||
|
tabs[0].id,
|
||||||
|
{message: "sponsorDataChanged"}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
updateStartTimeChosen();
|
updateStartTimeChosen();
|
||||||
|
|
||||||
|
@ -546,7 +560,17 @@ function runThePopup() {
|
||||||
|
|
||||||
//save this
|
//save this
|
||||||
let sponsorTimeKey = "sponsorTimes" + currentVideoID;
|
let sponsorTimeKey = "sponsorTimes" + currentVideoID;
|
||||||
chrome.storage.sync.set({[sponsorTimeKey]: sponsorTimes});
|
chrome.storage.sync.set({[sponsorTimeKey]: sponsorTimes}, function() {
|
||||||
|
chrome.tabs.query({
|
||||||
|
active: true,
|
||||||
|
currentWindow: true
|
||||||
|
}, tabs => {
|
||||||
|
chrome.tabs.sendMessage(
|
||||||
|
tabs[0].id,
|
||||||
|
{message: "sponsorDataChanged"}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
displaySponsorTimes();
|
displaySponsorTimes();
|
||||||
|
|
||||||
|
@ -575,7 +599,17 @@ function runThePopup() {
|
||||||
|
|
||||||
//save this
|
//save this
|
||||||
let sponsorTimeKey = "sponsorTimes" + currentVideoID;
|
let sponsorTimeKey = "sponsorTimes" + currentVideoID;
|
||||||
chrome.storage.sync.set({[sponsorTimeKey]: sponsorTimes});
|
chrome.storage.sync.set({[sponsorTimeKey]: sponsorTimes}, function() {
|
||||||
|
chrome.tabs.query({
|
||||||
|
active: true,
|
||||||
|
currentWindow: true
|
||||||
|
}, tabs => {
|
||||||
|
chrome.tabs.sendMessage(
|
||||||
|
tabs[0].id,
|
||||||
|
{message: "sponsorDataChanged"}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
//update display
|
//update display
|
||||||
displaySponsorTimes();
|
displaySponsorTimes();
|
||||||
|
@ -618,7 +652,17 @@ function runThePopup() {
|
||||||
sponsorTimes = [];
|
sponsorTimes = [];
|
||||||
|
|
||||||
let sponsorTimeKey = "sponsorTimes" + currentVideoID;
|
let sponsorTimeKey = "sponsorTimes" + currentVideoID;
|
||||||
chrome.storage.sync.set({[sponsorTimeKey]: sponsorTimes});
|
chrome.storage.sync.set({[sponsorTimeKey]: sponsorTimes}, function() {
|
||||||
|
chrome.tabs.query({
|
||||||
|
active: true,
|
||||||
|
currentWindow: true
|
||||||
|
}, tabs => {
|
||||||
|
chrome.tabs.sendMessage(
|
||||||
|
tabs[0].id,
|
||||||
|
{message: "sponsorDataChanged"}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
displaySponsorTimes();
|
displaySponsorTimes();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue