Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

3.3 KB
Raw Blame
import { ReplayPlugin, Replayer } from 'posthog-js/rrweb'
import { EventType, IncrementalSource, eventWithTime } from 'posthog-js/rrweb-types'

export const AudioMuteReplayerPlugin = (isMuted: boolean): ReplayPlugin => {
    const applyMuteToMediaElement = (element: HTMLElement): void => {
        const mediaElement = element as HTMLMediaElement

        // Clean up any existing listeners first
        if ((mediaElement as any).__muteListenersCleanup) {
            ;(mediaElement as any).__muteListenersCleanup()
        }

        if (isMuted) {
            element.setAttribute('muted', 'true')
            mediaElement.muted = true

            // Pause if it's playing
            if (!mediaElement.paused) {
                mediaElement.pause()
            }

            // Store listeners to clean them up later
            const playListener = (): void => {
                mediaElement.muted = true
            }
            const volumeChangeListener = (): void => {
                if (!mediaElement.muted) {
                    mediaElement.muted = true
                }
            }

            mediaElement.addEventListener('play', playListener)
            mediaElement.addEventListener('volumechange', volumeChangeListener)

            // Store cleanup function on the element for later removal
            ;(mediaElement as any).__muteListenersCleanup = (): void => {
                mediaElement.removeEventListener('play', playListener)
                mediaElement.removeEventListener('volumechange', volumeChangeListener)
            }
        } else {
            element.removeAttribute('muted')
            mediaElement.muted = false

            // Clean up listeners when unmuting since they're no longer needed
            if ((mediaElement as any).__muteListenersCleanup) {
                ;(mediaElement as any).__muteListenersCleanup()
                delete (mediaElement as any).__muteListenersCleanup
            }
        }
    }

    const applyMuteToElement = (element: HTMLElement): void => {
        if (element.nodeName === 'AUDIO' || element.nodeName === 'VIDEO') {
            applyMuteToMediaElement(element)
        }

        const mediaElements = element.querySelectorAll('audio, video')
        mediaElements.forEach((media) => {
            applyMuteToMediaElement(media as HTMLElement)
        })
    }

    return {
        onBuild: (node) => {
            if (!node || node.nodeType !== Node.ELEMENT_NODE) {
                return
            }

            const element = node as HTMLElement
            applyMuteToElement(element)
        },

        handler: (e: eventWithTime, _isSync: boolean, { replayer }: { replayer: Replayer }) => {
            if (e.type === EventType.IncrementalSnapshot && e.data.source === IncrementalSource.Mutation) {
                if (Array.isArray(e.data.adds)) {
                    e.data.adds.forEach((addedNode: any) => {
                        if (addedNode.node && addedNode.node.type === 1) {
                            const domNode = replayer.getMirror().getNode(addedNode.node.id)
                            if (domNode && domNode.nodeType === Node.ELEMENT_NODE) {
                                applyMuteToElement(domNode as HTMLElement)
                            }
                        }
                    })
                }
            }
        },
    } as ReplayPlugin
}