fix(player): mutate module variables directly to preserve mode state during in-place autoplay

This commit is contained in:
2026-05-01 18:05:12 +02:00
committed by Mikkel Elvers
parent 0fc554c815
commit 8e16a21189

View File

@@ -104,8 +104,8 @@ const initPlayer = (): void => {
} }
} }
const modeSources = safeJsonParse(container.getAttribute('data-mode-sources'), {} as Record<string, ModeSource>) let modeSources = safeJsonParse(container.getAttribute('data-mode-sources'), {} as Record<string, ModeSource>)
const availableModes = safeJsonParse(container.getAttribute('data-available-modes'), [] as string[]) let availableModes = safeJsonParse(container.getAttribute('data-available-modes'), [] as string[])
const initialMode = container.getAttribute('data-initial-mode') || 'dub' const initialMode = container.getAttribute('data-initial-mode') || 'dub'
const segments = safeJsonParse(container.getAttribute('data-segments'), [] as SkipSegment[]) const segments = safeJsonParse(container.getAttribute('data-segments'), [] as SkipSegment[])
const maxIntroStartSeconds = 180 const maxIntroStartSeconds = 180
@@ -893,31 +893,40 @@ const initPlayer = (): void => {
const video = container.querySelector('video') as HTMLVideoElement | null const video = container.querySelector('video') as HTMLVideoElement | null
if (!video) return if (!video) return
container.setAttribute('data-current-episode', String(nextEpisode)) // Update component state
currentEpisode = String(nextEpisode)
totalEpisodes = data.total_episodes
// We must update the module level variables, not just the DOM attributes
modeSources = data.mode_sources
availableModes = data.available_modes || []
currentMode = availableModes.includes(data.initial_mode) ? data.initial_mode : (availableModes[0] || 'dub')
const fallbackMode = Object.keys(modeSources).find((m) => typeof modeSources[m]?.token === 'string' && modeSources[m].token !== '')
if ((!modeSources[currentMode] || !modeSources[currentMode].token) && fallbackMode) {
currentMode = fallbackMode
}
container.setAttribute('data-current-episode', currentEpisode)
container.setAttribute('data-mal-id', String(animeID)) container.setAttribute('data-mal-id', String(animeID))
container.setAttribute('data-total-episodes', String(data.total_episodes)) container.setAttribute('data-total-episodes', String(totalEpisodes))
container.setAttribute('data-start-time-seconds', '0') container.setAttribute('data-start-time-seconds', '0')
container.setAttribute('data-initial-mode', data.initial_mode) container.setAttribute('data-initial-mode', data.initial_mode)
container.setAttribute('data-stream-token', data.token) container.setAttribute('data-stream-token', data.token)
container.setAttribute('data-available-modes', JSON.stringify(data.available_modes)) container.setAttribute('data-available-modes', JSON.stringify(availableModes))
container.setAttribute('data-mode-sources', JSON.stringify(data.mode_sources)) container.setAttribute('data-mode-sources', JSON.stringify(modeSources))
container.setAttribute('data-segments', JSON.stringify(data.segments)) container.setAttribute('data-segments', JSON.stringify(data.segments))
currentEpisode = String(nextEpisode)
totalEpisodes = data.total_episodes
// Reset preloader for next time // Reset preloader for next time
preloadedNextEpisodeData = null preloadedNextEpisodeData = null
preloadAttemptedForEpisode = null preloadAttemptedForEpisode = null
const newStreamURL = container.getAttribute('data-stream-url') || '/watch/proxy/stream' const newStreamURL = container.getAttribute('data-stream-url') || '/watch/proxy/stream'
const streamMode = data.initial_mode const modeSource = modeSources[currentMode]
const modeSource = data.mode_sources[streamMode]
if (modeSource?.token) { if (modeSource?.token) {
video.src = `${newStreamURL}?mode=${encodeURIComponent(streamMode)}&token=${encodeURIComponent(modeSource.token)}` video.src = `${newStreamURL}?mode=${encodeURIComponent(currentMode)}&token=${encodeURIComponent(modeSource.token)}`
} else if (data.token) { } else if (data.token) {
video.src = `${newStreamURL}?mode=${encodeURIComponent(streamMode)}&token=${encodeURIComponent(data.token)}` video.src = `${newStreamURL}?mode=${encodeURIComponent(currentMode)}&token=${encodeURIComponent(data.token)}`
} }
video.load() video.load()