mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-09 21:17:20 +03:00
Updates sponsor skipping to support decimal place times
This commit is contained in:
26
content.js
26
content.js
@@ -5,6 +5,11 @@ if(id = getYouTubeVideoID(document.URL)){ // Direct Links
|
|||||||
//was sponsor data found when doing SponsorsLookup
|
//was sponsor data found when doing SponsorsLookup
|
||||||
var sponsorDataFound = false;
|
var sponsorDataFound = false;
|
||||||
|
|
||||||
|
//the video
|
||||||
|
var v;
|
||||||
|
|
||||||
|
//the last time looked at (used to see if this time is in the interval)
|
||||||
|
var lastTime;
|
||||||
|
|
||||||
chrome.runtime.onMessage.addListener( // Detect URL Changes
|
chrome.runtime.onMessage.addListener( // Detect URL Changes
|
||||||
function(request, sender, sendResponse) {
|
function(request, sender, sendResponse) {
|
||||||
@@ -26,7 +31,7 @@ chrome.runtime.onMessage.addListener( // Detect URL Changes
|
|||||||
|
|
||||||
function sponsorsLookup(id) {
|
function sponsorsLookup(id) {
|
||||||
v = document.querySelector('video') // Youtube video player
|
v = document.querySelector('video') // Youtube video player
|
||||||
var xmlhttp = new XMLHttpRequest();
|
let xmlhttp = new XMLHttpRequest();
|
||||||
|
|
||||||
//check database for sponsor times
|
//check database for sponsor times
|
||||||
xmlhttp.open('GET', 'http://localhost/api/getVideoSponsorTimes?videoID=' + id, true);
|
xmlhttp.open('GET', 'http://localhost/api/getVideoSponsorTimes?videoID=' + id, true);
|
||||||
@@ -35,22 +40,29 @@ function sponsorsLookup(id) {
|
|||||||
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
|
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
|
||||||
sponsorDataFound = true;
|
sponsorDataFound = true;
|
||||||
|
|
||||||
sponsors = JSON.parse(xmlhttp.responseText);
|
sponsorTimes = JSON.parse(xmlhttp.responseText).sponsorTimes;
|
||||||
|
|
||||||
// If the sponsor data exists, add the event to run on the videos "ontimeupdate"
|
// If the sponsor data exists, add the event to run on the videos "ontimeupdate"
|
||||||
v.ontimeupdate = function () {
|
v.ontimeupdate = function () {
|
||||||
sponsorCheck(sponsors);
|
sponsorCheck(sponsorTimes);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
xmlhttp.send(null);
|
xmlhttp.send(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
function sponsorCheck(sponsors) { // Video skipping
|
function sponsorCheck(sponsorTimes) { // Video skipping
|
||||||
sponsors.forEach(function (el, index) { // Foreach Sponsor in video
|
//see if any sponsor start time was just passed
|
||||||
if ((Math.floor(v.currentTime)) == el[0]) { // Check time has sponsor
|
sponsorTimes.forEach(function (sponsorTime, index) { // Foreach Sponsor in video
|
||||||
v.currentTime = el[1]; // Set new time
|
//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];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lastTime = v.currentTime;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user