- Overlay\Build id (plugin version + admin epoch) in X-SG-Build; overlay.js reloads once on mismatch (loop-guarded); 'Refresh all overlays now' - Media URLs carry ?v=<mtime> - Alert option 'Frameless' (schema v5): no box so transparent PNG/GIF/WebP and VP9-alpha WebM show through; admin preview with checkerboard and ffmpeg help Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
156 lines
7.5 KiB
JavaScript
156 lines
7.5 KiB
JavaScript
/* StreamGoals alert editor: Media Library pickers (wp.media) and a live preview that mirrors the overlay. */
|
|
( function () {
|
|
'use strict';
|
|
var form = document.querySelector( '.sga-form' );
|
|
var dataEl = document.getElementById( 'sga-data' );
|
|
var stage = document.getElementById( 'sga-stage' );
|
|
var playBtn = document.getElementById( 'sga-play' );
|
|
if ( ! form || ! dataEl || ! stage || ! playBtn ) { return; }
|
|
var cfg = { currency: 'USD', exp: 2, rewards: [], videoCap: 30000, warnBytes: 26214400, allowed: {} };
|
|
try { cfg = JSON.parse( dataEl.textContent ); } catch ( e ) { /* defaults */ }
|
|
var timers = [], media = [];
|
|
|
|
function el( tag, cls, text ) {
|
|
var e = document.createElement( tag );
|
|
if ( cls ) { e.className = cls; }
|
|
if ( text != null ) { e.textContent = text; }
|
|
return e;
|
|
}
|
|
|
|
/* ---- Media Library pickers ---- */
|
|
Array.prototype.forEach.call( form.querySelectorAll( '.sga-media' ), function ( box ) {
|
|
var input = box.querySelector( 'input[type=hidden]' );
|
|
var nameEl = box.querySelector( '.sga-media-name' );
|
|
var warnEl = box.querySelector( '.sga-media-warn' );
|
|
var kind = box.getAttribute( 'data-kind' );
|
|
var frame = null;
|
|
|
|
function set( id, url, mime, name, size ) {
|
|
input.value = String( id || 0 );
|
|
input.setAttribute( 'data-url', url || '' );
|
|
input.setAttribute( 'data-mime', mime || '' );
|
|
nameEl.textContent = id ? name : 'Nothing chosen';
|
|
var msg = '';
|
|
if ( id && size > cfg.warnBytes ) {
|
|
msg = 'This file is over 25 MB. Big files load slowly' + ( /^video\//.test( mime ) ? ' and large videos can make OBS stutter. A short, compressed clip is much better.' : '.' );
|
|
}
|
|
warnEl.textContent = msg;
|
|
input.dispatchEvent( new Event( 'change', { bubbles: true } ) );
|
|
}
|
|
function accepts( mime ) {
|
|
var k = cfg.allowed[ String( mime ).toLowerCase() ];
|
|
return kind === 'sound' ? k === 'audio' : ( k === 'image' || k === 'video' );
|
|
}
|
|
box.querySelector( '.sga-pick' ).addEventListener( 'click', function () {
|
|
if ( ! window.wp || ! wp.media ) { nameEl.textContent = 'The Media Library could not be loaded. Reload the page.'; return; }
|
|
if ( ! frame ) {
|
|
frame = wp.media( {
|
|
title: kind === 'sound' ? 'Choose a sound' : 'Choose a picture, GIF or video',
|
|
button: { text: 'Use this file' },
|
|
library: { type: kind === 'sound' ? [ 'audio' ] : [ 'image', 'video' ] },
|
|
multiple: false
|
|
} );
|
|
frame.on( 'select', function () {
|
|
var a = frame.state().get( 'selection' ).first().toJSON();
|
|
if ( ! accepts( a.mime ) ) {
|
|
warnEl.textContent = 'That file type is not allowed (' + a.mime + '). ' + ( kind === 'sound' ? 'Use MP3, OGG or WAV.' : 'Use PNG, JPEG, GIF, WebP, MP4 or WebM.' );
|
|
return;
|
|
}
|
|
set( a.id, a.url, a.mime, a.filename || a.title || ( 'File ' + a.id ), a.filesizeInBytes || 0 );
|
|
} );
|
|
}
|
|
frame.open();
|
|
} );
|
|
box.querySelector( '.sga-clear' ).addEventListener( 'click', function () { set( 0, '', '', '', 0 ); } );
|
|
} );
|
|
|
|
var vol = form.elements.volume, volOut = document.getElementById( 'sga_volume_out' );
|
|
if ( vol && volOut ) { vol.addEventListener( 'input', function () { volOut.textContent = vol.value; } ); }
|
|
|
|
/* ---- preview ---- */
|
|
function toMinor( raw ) {
|
|
var s = String( raw == null ? '' : raw ).replace( /[,\s$€£¥]/g, '' );
|
|
var m = /^(\d+)(?:\.(\d+))?$/.exec( s );
|
|
if ( ! m ) { return null; }
|
|
var frac = m[ 2 ] || '';
|
|
if ( frac.length > cfg.exp ) { return null; }
|
|
while ( frac.length < cfg.exp ) { frac += '0'; }
|
|
return parseInt( m[ 1 ] + frac, 10 );
|
|
}
|
|
function money( minor ) {
|
|
var s = String( minor );
|
|
if ( cfg.exp === 0 ) { return cfg.currency + ' ' + s; }
|
|
while ( s.length <= cfg.exp ) { s = '0' + s; }
|
|
return cfg.currency + ' ' + s.slice( 0, -cfg.exp ) + '.' + s.slice( -cfg.exp );
|
|
}
|
|
function clean( s ) { return String( s ).replace( /<[^>]*>/g, '' ).replace( /[<>\u0000-\u001f\u007f]/g, ' ' ).replace( /\s+/g, ' ' ).trim(); }
|
|
function render( tpl, vals ) {
|
|
var t = clean( tpl );
|
|
if ( ! t ) { return ''; }
|
|
return t.replace( /\{([A-Za-z_]{1,20})\}/g, function ( m, k ) { return vals[ k.toLowerCase() ] || ''; } ).replace( /\s+/g, ' ' ).trim().slice( 0, 200 );
|
|
}
|
|
function stop() {
|
|
timers.forEach( clearTimeout ); timers = [];
|
|
media.forEach( function ( m ) { try { m.pause(); } catch ( e ) { /* ignore */ } } ); media = [];
|
|
}
|
|
function inp( name ) { return form.querySelector( '[name="' + name + '"]' ); }
|
|
|
|
function play() {
|
|
stop();
|
|
stage.textContent = '';
|
|
var vis = inp( 'visual_id' ), snd = inp( 'sound_id' );
|
|
var visUrl = vis.value !== '0' ? vis.getAttribute( 'data-url' ) : '', visMime = vis.getAttribute( 'data-mime' ) || '';
|
|
var sndUrl = snd.value !== '0' ? snd.getAttribute( 'data-url' ) : '';
|
|
var volume = Math.max( 0, Math.min( 100, parseInt( form.elements.volume.value, 10 ) || 0 ) ) / 100;
|
|
var dur = Math.max( 1, Math.min( 60, parseFloat( form.elements.duration.value ) || 6 ) ) * 1000;
|
|
var trig = form.querySelector( 'input[name="trigger_type"]:checked' );
|
|
var amt = trig && trig.value === 'amount' ? toMinor( form.elements.amount.value ) : null;
|
|
var rewardSel = form.elements.action_id;
|
|
var reward = rewardSel && rewardSel.value !== '0' && rewardSel.selectedIndex > 0 ? rewardSel.options[ rewardSel.selectedIndex ].text.replace( / \(turned off\)$/, '' ) : 'Your reward';
|
|
var vals = { name: 'Alex', amount: money( amt || 500 ), reward: reward };
|
|
var msg = render( form.elements.message.value, vals );
|
|
|
|
var frameless = !! ( visUrl && form.elements.frameless && form.elements.frameless.checked );
|
|
var card = el( 'div', 'sga-alert' + ( frameless ? ' sga-frameless' : '' ) );
|
|
var isVideo = /^video\//.test( visMime ), video = null;
|
|
if ( visUrl ) {
|
|
var box = el( 'div', 'sga-alert-media' );
|
|
if ( isVideo ) {
|
|
video = document.createElement( 'video' );
|
|
video.playsInline = true; video.setAttribute( 'playsinline', '' );
|
|
video.muted = !! sndUrl || volume === 0; video.volume = volume;
|
|
video.src = visUrl; box.appendChild( video );
|
|
} else {
|
|
var img = new Image(); img.alt = ''; img.src = visUrl; box.appendChild( img );
|
|
}
|
|
card.appendChild( box );
|
|
}
|
|
var head = el( 'div', 'sga-alert-head' );
|
|
if ( msg ) { head.textContent = msg; }
|
|
else { head.appendChild( el( 'b', null, vals.name ) ); head.appendChild( document.createTextNode( ' donated ' ) ); head.appendChild( el( 'b', null, vals.amount ) ); }
|
|
card.appendChild( head );
|
|
stage.appendChild( card );
|
|
|
|
if ( sndUrl ) {
|
|
var au = new Audio( sndUrl ); au.volume = volume; media.push( au );
|
|
var p = au.play(); if ( p && p.catch ) { p.catch( function () { head.appendChild( el( 'div', 'sga-idle', 'Sound was blocked by the browser.' ) ); } ); }
|
|
}
|
|
var end = function () { stop(); stage.textContent = ''; stage.appendChild( el( 'span', 'sga-idle', 'Preview finished.' ) ); };
|
|
if ( video ) {
|
|
media.push( video );
|
|
video.onended = end;
|
|
var vp = video.play();
|
|
if ( vp && vp.catch ) { vp.catch( function () { video.muted = true; var again = video.play(); if ( again && again.catch ) { again.catch( function () {} ); } } ); }
|
|
timers.push( setTimeout( end, cfg.videoCap ) );
|
|
} else {
|
|
timers.push( setTimeout( end, dur ) );
|
|
}
|
|
}
|
|
/* Preview backdrop: dark / light checkerboard or a busy "scene" so transparency is visible. */
|
|
var bgSel = document.getElementById( 'sga-bg' );
|
|
function setBg() { stage.className = 'sga-stage' + ( bgSel && bgSel.value !== 'dark' ? ' sga-bg-' + bgSel.value : '' ); }
|
|
if ( bgSel ) { bgSel.addEventListener( 'change', setBg ); }
|
|
playBtn.addEventListener( 'click', play );
|
|
stage.appendChild( el( 'span', 'sga-idle', 'Press "Play preview".' ) );
|
|
}() );
|