feat: expand media playback and security scan options

- Add resume, speed selection, and controls-free media mode
- Run Trivy daily and secrets/code scans weekly
- Document new MediaPlayer options in README
This commit is contained in:
Gabriel20xx committed 2026-11-15 19:24:19 +01:00
1 parent d20eb96969
commit fc01c93aeb
6 files changed
+292 -16

No files matched your search

@@ -9,7 +9,7 @@
let {
kind = 'video',
src = '',
src,
poster = '',
title = '',
autoplay = false,
@@ -35,6 +35,12 @@
onSkip,
onQualityChange,
onToggleGraph,
storageKey = '',
showSpeed = true,
controls = true,
element = $bindable(null),
preload = 'auto',
crossorigin,
endControls,
}: {
kind?: MediaPlayerKind
@@ -66,6 +72,24 @@
onSkip?: (seconds: number) => void
onQualityChange?: (value: string) => void
onToggleGraph?: () => void
/**
* Turns on resume: the position of each source is saved in localStorage under this prefix and
* restored when the source loads. Give each app its own prefix. Empty (the default) keeps
* playback position out of browser storage.
*/
storageKey?: string
/** Offers the playback speed in the settings menu. */
showSpeed?: boolean
/**
* False renders the element with no player controls, surface clicks, keyboard handling or
* fullscreen tracking. The caller then drives playback itself, through `element`, and may set
* the element's `src` directly: this component leaves `src` alone when none is passed.
*/
controls?: boolean
/** The media element, once mounted. Cleared when it goes. */
element?: HTMLMediaElement | null
preload?: 'none' | 'metadata' | 'auto'
crossorigin?: 'anonymous' | 'use-credentials'
endControls?: Snippet
} = $props()
@@ -81,12 +105,19 @@
let orientationLocked = $state(false)
let controlsHideTimeout: ReturnType<typeof setTimeout> | null = null
let orientationLockRequest = 0
let playbackRate = $state(1)
let lastProgressSaveAt = 0
const PROGRESS_SAVE_INTERVAL_MS = 1500
const currentTime = $derived(externalCurrentTime ?? internalTime)
const duration = $derived(externalDuration ?? internalDuration)
const audio = $derived(kind === 'audio')
const controlsHideDelay = 2500
$effect(() => {
element = mediaElement
})
function clearControlsHideTimer() {
if (controlsHideTimeout === null) return
clearTimeout(controlsHideTimeout)
@@ -195,6 +226,65 @@
skip(event.clientX - bounds.left < bounds.width / 2 ? -10 : 10)
}
function progressKey() {
return `${storageKey}:${src}`
}
function restorePlaybackProgress() {
if (!storageKey || !mediaElement) return
try {
const savedProgress = Number(window.localStorage.getItem(progressKey()))
const length = mediaElement.duration
if (
Number.isFinite(savedProgress) &&
savedProgress > 0 &&
Number.isFinite(length) &&
length > 0 &&
savedProgress < length - 1
) {
mediaElement.currentTime = savedProgress
}
} catch {
// Playback remains available when browser storage is disabled or full.
}
}
function savePlaybackProgress(force = false) {
if (!storageKey || !mediaElement || mediaElement.ended) return
const position = mediaElement.currentTime
if (!Number.isFinite(position)) return
try {
if (position <= 0) {
if (force) window.localStorage.removeItem(progressKey())
return
}
if (!force && Date.now() - lastProgressSaveAt < PROGRESS_SAVE_INTERVAL_MS) return
lastProgressSaveAt = Date.now()
window.localStorage.setItem(progressKey(), String(position))
} catch {
// Keep playing when browser storage is disabled or full.
}
}
function clearPlaybackProgress() {
if (!storageKey) return
try {
window.localStorage.removeItem(progressKey())
} catch {
// Keep playback completion independent from browser storage.
}
}
function changeSpeed(value: number) {
if (!Number.isFinite(value) || value <= 0) return
playbackRate = value
if (mediaElement) mediaElement.playbackRate = value
}
function handleSeeked() {
savePlaybackProgress(true)
}
function handlePlay(event: Event) {
playing = true
onPlay?.(event)
@@ -203,11 +293,13 @@
function handlePause(event: Event) {
playing = false
controlsVisible = true
savePlaybackProgress(true)
onPause?.(event)
}
function handleTimeUpdate(event: Event) {
if (mediaElement) internalTime = mediaElement.currentTime || 0
savePlaybackProgress()
onTimeUpdate?.(event)
}
@@ -216,13 +308,17 @@
internalDuration = Number.isFinite(mediaElement.duration) ? mediaElement.duration : 0
volume = mediaElement.volume
muted = mediaElement.muted || mediaElement.volume === 0
void syncScreenOrientation(fullscreen)
// Loading a source resets the rate, so the chosen speed is applied again.
mediaElement.playbackRate = playbackRate
restorePlaybackProgress()
if (controls) void syncScreenOrientation(fullscreen)
onLoadedMetadata?.(event)
}
function handleEnded(event: Event) {
playing = false
controlsVisible = true
clearPlaybackProgress()
onEnded?.(event)
}
@@ -287,6 +383,7 @@
}
function handleFullscreenChange() {
if (!controls) return
fullscreen = Boolean(document.fullscreenElement)
void syncScreenOrientation(fullscreen)
}
@@ -311,7 +408,7 @@
})
onMount(() => {
if (!playerFrame) return
if (!controls || !playerFrame) return
const activityEvents = ['mousemove', 'click', 'keydown', 'focusin', 'touchstart'] as const
for (const eventName of activityEvents) playerFrame.addEventListener(eventName, handlePlayerActivity)
@@ -329,15 +426,15 @@
<svelte:window onfullscreenchange={handleFullscreenChange} />
<div bind:this={playerFrame} class={`media-player ${className}`}>
<div bind:this={playerFrame} class={controls ? `media-player ${className}` : className}>
{#if audio}
<div
class="media-player-audio-surface"
role="button"
tabindex="0"
aria-label={title ? `Audio player for ${title}` : 'Audio player'}
onclick={handleSurfaceClick}
onkeydown={handleKeydown}
onclick={controls ? handleSurfaceClick : undefined}
onkeydown={controls ? handleKeydown : undefined}
>
<Music2 class="media-player-audio-icon" />
{#if title}<span>{title}</span>{/if}
@@ -346,7 +443,7 @@
bind:this={mediaElement}
{src}
{autoplay}
preload="auto"
{preload}
tabindex="-1"
aria-hidden="true"
onplay={handlePlay}
@@ -355,6 +452,7 @@
onloadedmetadata={handleLoadedMetadata}
onended={handleEnded}
onvolumechange={handleVolumeChange}
onseeked={handleSeeked}
onerror={handleError}
></audio>
{:else}
@@ -366,25 +464,28 @@
{poster}
{autoplay}
muted={autoplay && autoplayMuted}
preload="auto"
{preload}
{crossorigin}
playsinline
tabindex="0"
class={`media-player-video ${mediaClass}`}
tabindex={controls ? 0 : undefined}
class={[controls && 'media-player-video', mediaClass].filter(Boolean).join(' ')}
aria-label={title ? `Video player for ${title}` : 'Video player'}
onclick={handleSurfaceClick}
ondblclick={handleVideoDoubleTap}
onkeydown={handleKeydown}
onclick={controls ? handleSurfaceClick : undefined}
ondblclick={controls ? handleVideoDoubleTap : undefined}
onkeydown={controls ? handleKeydown : undefined}
onplay={handlePlay}
onpause={handlePause}
ontimeupdate={handleTimeUpdate}
onloadedmetadata={handleLoadedMetadata}
onended={handleEnded}
onvolumechange={handleVolumeChange}
onseeked={handleSeeked}
onerror={handleError}
>
<track kind="captions" srclang="en" label="Captions" src="data:text/vtt,WEBVTT%0A" />
</video>
{/if}
{#if controls}
<MediaPlayerControls
{kind}
{currentTime}
@@ -408,8 +509,12 @@
{onQualityChange}
{onToggleGraph}
{endControls}
{showSpeed}
{playbackRate}
onSpeedChange={changeSpeed}
overlay
/>
{/if}
</div>
<style>