import { Player, PlayerPlugin, $ } from '@oplayer/core' const topRight = $.css({ position: 'absolute', right: '0.5em', top: '0.5em', 'z-index': 1, display: 'flex', gap: '5px', 'align-items': 'center', 'justify-content': 'center' }) const area = $.css( 'display: inline-block;background: rgba(28 ,28 ,28 , 0.9);color: #fff;padding: 4px 8px;border-radius: 4px; fill:#fff; font-size: 14px' ) const mute = $.css({ position: 'relative', '&::before': { display: 'block', content: "''", position: 'absolute', width: '50%', height: '2px', 'background-color': '#fff', transform: 'rotate(40deg) translateY(-100%)', top: '48%', left: '6px' } }) type Options = { video?: string image?: string position?: { left?: string bottom?: string top?: string right?: string } autoplay?: boolean plugins?: PlayerPlugin[] skipDuration?: number duration: number target?: string onSkip?: (duration: number) => void } export default ({ duration, skipDuration, video, image, plugins, target, autoplay, onSkip, position }: Options): PlayerPlugin => ({ name: 'oplayer-plugin-ad', version: __VERSION__, key: 'ad', apply: (player) => { if (autoplay) { bootstrap() } else { $.render( $.create( `div.${$.css({ width: '100%', height: '100%', position: 'absolute', top: '0', left: '0', 'z-index': 999 })}` ), player.$root ).onclick = function () { ;(this as HTMLDivElement).remove() bootstrap() } } function bootstrap() { const $container = $.create( `div.${$.css({ width: '100%', height: '100%', position: 'absolute', top: '0', left: '0', 'z-index': 999, cursor: 'pointer' })}`, {}, `
${ skipDuration ? `
${player.locales.get( 'Can be closed after %ss', skipDuration )}
` : '' }
${player.locales.get('%ss', duration)}
${ video ? `
` : '' }
` ) let instance: Player const $skip = $container.querySelector(`.${area}[skipDuration]`) const $duration = $container.querySelector(`.${area}[duration]`)! const $volume = $container.querySelector(`.${area}[volume]`) if (video) { instance = Player.make($container, { muted: true, autoplay: true, loop: true, source: { src: video } }) .use(plugins || ([] as any)) .create() $volume!.addEventListener('click', (e) => { e.preventDefault() e.stopPropagation() // Prevent the click event from bubbling to the $container instance.isMuted ? instance.unmute() : instance.mute() $volume!.classList.toggle(mute) }) } else { $container.style.background = `url(${image}) center center` } $.render($container, player.$root) let count = 0 const destroy = () => { clearInterval(timer) instance?.destroy() $container.remove() player.play() } //TODO: fix hotkey const timer = setInterval(() => { if (skipDuration !== undefined) { count++ if (skipDuration - count > 0) { $skip!.innerText = player.locales.get('Can be closed after %ss', skipDuration - count) } else { $skip!.innerText = player.locales.get('Close') $skip!.onclick ??= () => { onSkip?.(count) destroy() } } } if (duration - count > 0) { $duration.innerText = player.locales.get('%ss', duration - count) } else { destroy() } }, 1000) if (target) { $container.addEventListener('click', (e) => { if (!(e.target as HTMLElement).classList.contains(area)) { window.open(target, '_blank') } }) } } } })