Added duration change listener check to prevent mid-video zero second skips.

Sometimes the video gets reset to zero seconds for a few milliseconds, this should not trigger a skip.

Resolves https://github.com/ajayyy/SponsorBlock/issues/183
This commit is contained in:
Ajay Ramachandran
2020-02-18 15:03:55 -05:00
parent af7ba31c2f
commit aeabf806ac

View File

@@ -37,6 +37,8 @@ var lastPreviewBarUpdate;
//whether the duration listener listening for the duration changes of the video has been setup yet //whether the duration listener listening for the duration changes of the video has been setup yet
var durationListenerSetUp = false; var durationListenerSetUp = false;
// Timestamp of the last duration change
var lastDurationChange = 0;
//the channel this video is about //the channel this video is about
var channelURL; var channelURL;
@@ -403,8 +405,17 @@ function createPreviewBar(): void {
} }
} }
function sponsorsLookup(id: string, channelIDPromise?) { /**
* Triggered every time the video duration changes.
* This happens when the resolution changes or at random time to clear memory.
*/
function durationChangeListener() {
lastDurationChange = Date.now();
updatePreviewBar();
}
function sponsorsLookup(id: string, channelIDPromise?) {
video = document.querySelector('video') // Youtube video player video = document.querySelector('video') // Youtube video player
//there is no video here //there is no video here
if (video == null) { if (video == null) {
@@ -416,7 +427,7 @@ function sponsorsLookup(id: string, channelIDPromise?) {
durationListenerSetUp = true; durationListenerSetUp = true;
//wait until it is loaded //wait until it is loaded
video.addEventListener('durationchange', updatePreviewBar); video.addEventListener('durationchange', durationChangeListener);
} }
if (channelIDPromise !== undefined) { if (channelIDPromise !== undefined) {
@@ -671,6 +682,9 @@ function sponsorCheck() {
return; return;
} }
// Don't skip right after duration change (the time resets to zero) 400ms
if (Date.now() - lastDurationChange < 400000 && lastTime > 1) return;
let skipHappened = false; let skipHappened = false;
if (sponsorTimes != null) { if (sponsorTimes != null) {