|
4 | 4 | /*------------------------------------------------------------------------------ |
5 | 5 | 4.5.1 UP NEXT AUTOPLAY |
6 | 6 | ------------------------------------------------------------------------------*/ |
7 | | -ImprovedTube.playlistUpNextAutoplay = function () { if (this.storage.playlist_up_next_autoplay === false) { |
8 | | - const playlistData = this.elements.ytd_watch?.playlistData; |
9 | | - if (this.getParam(location.href, 'list') && playlistData |
10 | | - && playlistData.currentIndex |
11 | | - && playlistData.totalVideos |
12 | | - && playlistData.localCurrentIndex) { |
13 | | - playlistData.currentIndex = playlistData.totalVideos; |
| 7 | +ImprovedTube.playlistUpNextAutoplay = function () { |
| 8 | + if (this.storage.playlist_up_next_autoplay === false) { |
| 9 | + const playlistData = this.elements.ytd_watch?.playlistData; |
| 10 | + if (this.getParam(location.href, 'list') && playlistData |
| 11 | + && playlistData.currentIndex |
| 12 | + && playlistData.totalVideos |
| 13 | + && playlistData.localCurrentIndex) { |
| 14 | + |
| 15 | + // Fix for large playlists: ensure proper synchronization instead of forcing end |
| 16 | + // YouTube loads playlists in chunks (typically 200 videos), so we need to |
| 17 | + // keep currentIndex and localCurrentIndex in sync as new segments load |
| 18 | + if (playlistData.currentIndex !== playlistData.localCurrentIndex) { |
| 19 | + playlistData.currentIndex = playlistData.localCurrentIndex; |
| 20 | + } |
| 21 | + |
| 22 | + // Monitor for playlist data updates to handle pagination |
| 23 | + if (!this.playlistAutoplayObserver) { |
| 24 | + this.playlistAutoplayObserver = new MutationObserver((mutations) => { |
| 25 | + mutations.forEach((mutation) => { |
| 26 | + if (mutation.type === 'attributes' && |
| 27 | + mutation.attributeName === 'data' && |
| 28 | + this.elements.ytd_watch?.playlistData) { |
| 29 | + |
| 30 | + const updatedData = this.elements.ytd_watch.playlistData; |
| 31 | + // Resync when YouTube loads new playlist segments |
| 32 | + if (updatedData.currentIndex !== updatedData.localCurrentIndex) { |
| 33 | + updatedData.currentIndex = updatedData.localCurrentIndex; |
| 34 | + } |
| 35 | + } |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + // Observe the watch element for playlist data changes |
| 40 | + if (this.elements.ytd_watch) { |
| 41 | + this.playlistAutoplayObserver.observe(this.elements.ytd_watch, { |
| 42 | + attributes: true, |
| 43 | + attributeFilter: ['data'] |
| 44 | + }); |
| 45 | + } |
| 46 | + } |
14 | 47 | } |
| 48 | + } else { |
| 49 | + // Clean up observer when feature is enabled |
| 50 | + if (this.playlistAutoplayObserver) { |
| 51 | + this.playlistAutoplayObserver.disconnect(); |
| 52 | + this.playlistAutoplayObserver = null; |
| 53 | + } |
| 54 | + } |
| 55 | +}; |
| 56 | + |
| 57 | +// Enhanced playlist navigation handler for large playlists |
| 58 | +ImprovedTube.playlistLargePlaylistHandler = function() { |
| 59 | + if (!this.getParam(location.href, 'list')) return; |
| 60 | + |
| 61 | + const playlistData = this.elements.ytd_watch?.playlistData; |
| 62 | + if (!playlistData) return; |
| 63 | + |
| 64 | + // Monitor video changes to handle large playlist navigation |
| 65 | + const videoElement = this.elements.player?.querySelector('video'); |
| 66 | + if (videoElement && !this.playlistVideoChangeListener) { |
| 67 | + this.playlistVideoChangeListener = () => { |
| 68 | + setTimeout(() => { |
| 69 | + const currentData = this.elements.ytd_watch?.playlistData; |
| 70 | + if (currentData && currentData.currentIndex !== currentData.localCurrentIndex) { |
| 71 | + // Force synchronization when video changes |
| 72 | + currentData.currentIndex = currentData.localCurrentIndex; |
| 73 | + |
| 74 | + // Update the player's playlist manager if available |
| 75 | + const playlistManager = document.querySelector('yt-playlist-manager'); |
| 76 | + if (playlistManager && playlistManager.autoplayData) { |
| 77 | + playlistManager.autoplayData.currentIndex = currentData.localCurrentIndex; |
| 78 | + } |
| 79 | + } |
| 80 | + }, 100); |
| 81 | + }; |
| 82 | + |
| 83 | + videoElement.addEventListener('loadedmetadata', this.playlistVideoChangeListener); |
| 84 | + videoElement.addEventListener('play', this.playlistVideoChangeListener); |
15 | 85 | } |
| 86 | + |
| 87 | + // Cleanup function for when navigating away from playlist pages |
| 88 | + this.cleanupPlaylistHandlers = function() { |
| 89 | + if (this.playlistAutoplayObserver) { |
| 90 | + this.playlistAutoplayObserver.disconnect(); |
| 91 | + this.playlistAutoplayObserver = null; |
| 92 | + } |
| 93 | + if (this.playlistVideoChangeListener && videoElement) { |
| 94 | + videoElement.removeEventListener('loadedmetadata', this.playlistVideoChangeListener); |
| 95 | + videoElement.removeEventListener('play', this.playlistVideoChangeListener); |
| 96 | + this.playlistVideoChangeListener = null; |
| 97 | + } |
| 98 | + }; |
16 | 99 | }; |
17 | 100 | /*------------------------------------------------------------------------------ |
18 | 101 | 4.5.2 REVERSE |
|
0 commit comments