Watch history fix

This commit is contained in:
goodtube4u
2024-07-23 11:32:32 +10:00
parent 2be8ac0f75
commit 294f7ea0c4
2 changed files with 66 additions and 2 deletions

View File

@@ -374,6 +374,57 @@
}
}
// Sync players
let goodTube_youtube_syncing = false;
let goodTube_youtube_previousSyncTime = 0;
function goodTube_youtube_syncPlayers() {
let youtubeVideo = document.querySelector('#movie_player video');
// If the youtube player exists, our player is loaded and we're viewing a video
if (youtubeVideo && goodTube_videojs_player_loaded && typeof goodTube_getParams['v'] !== 'undefined') {
// Don't keep syncing the same time over and over unless it's the start of the video
let syncTime = goodTube_player.currentTime;
if (syncTime === goodTube_youtube_previousSyncTime && parseFloat(syncTime) > 0) {
return;
}
// Setup the previous sync time
goodTube_youtube_previousSyncTime = syncTime;
// Set the current time of the Youtube player to match ours (this makes history and watched time work correctly)
youtubeVideo.currentTime = syncTime;
// We're syncing (this turns off the pausing of the Youtube video in goodTube_youtube_mutePauseSkipAds)
goodTube_youtube_syncing = true;
// Play for 10ms to make history work via JS
youtubeVideo.play();
youtubeVideo.muted = true;
youtubeVideo.volume = 0;
// Play for 10ms to make history work via the frame API
let youtubeFrameApi = document.querySelector('#movie_player');
if (youtubeFrameApi) {
if (typeof youtubeFrameApi.playVideo === 'function') {
youtubeFrameApi.playVideo();
}
if (typeof youtubeFrameApi.mute === 'function') {
youtubeFrameApi.mute();
}
if (typeof youtubeFrameApi.setVolume === 'function') {
youtubeFrameApi.setVolume(0);
}
}
// Stop syncing after 10ms (this turns on the pausing of the Youtube video in goodTube_youtube_mutePauseSkipAds)
setTimeout(function() {
goodTube_youtube_syncing = false;
}, 10);
}
}
// Mute, pause and skip ads on all Youtube videos
function goodTube_youtube_mutePauseSkipAds() {
// // Always skip the ads as soon as possible by clicking the skip button
@@ -389,7 +440,10 @@
if (!element.closest('#inline-player')) {
element.muted = true;
element.volume = 0;
element.pause();
if (!goodTube_youtube_syncing) {
element.pause();
}
}
});
}
@@ -1217,6 +1271,9 @@
// Init default quality modal
goodTube_player_defaultQualityModalInit();
// Sync players every 10s
setInterval(goodTube_youtube_syncPlayers, 10000);
// Listen for keyboard shortcuts
document.addEventListener('keydown', function(event) {
// Don't do anything if we're holding control, or we're not viewing a video
@@ -3532,6 +3589,9 @@
video.setAttribute('webkit-playsinline', '');
}
// Sync the Youtube player for watch history
goodTube_youtube_syncPlayers();
// Enable the qualities API
goodTube_qualityApi = goodTube_videojs_player.hlsQualitySelector();
@@ -3899,6 +3959,9 @@
goodTube_videojs_player.on('seeked', function() {
goodTube_seeking = false;
// Sync the Youtube player for watch history
goodTube_youtube_syncPlayers();
});
// On buffering / loading
@@ -4135,6 +4198,7 @@
// Play next video this video has ended
goodTube_videojs_player.on('ended', function() {
goodTube_youtube_syncPlayers();
goodTube_nextVideo();
});

2
goodtube.min.js vendored

File diff suppressed because one or more lines are too long