Common.js: Difference between revisions
From Blood on the Clocktower Wiki
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
$("li:contains('Toolbox')").hide(); | $("li:contains('Toolbox')").hide(); | ||
$("img").contextmenu(function() { | $("img").contextmenu(function () { | ||
return false; | return false; | ||
}); | }); | ||
$(document).ready(function() | $(document).ready(function () { | ||
{ | |||
handleAudioElements(); | handleAudioElements(); | ||
}); | }); | ||
function handleAudioElements() | function handleAudioElements() | ||
{ | { | ||
content[0].querySelectorAll('.html5audio:not(.loaded)').forEach(function (div) { | |||
var data = div.dataset; | |||
var file = data.file; | |||
if (!file) { | |||
return; | |||
} | |||
var format = file.split('.').pop(); | |||
var preload = data.preload; | |||
var download = data.download; | |||
var options = data.options; | |||
var opts = { controls: '' }; | |||
var volume = Number(Number(data.volume).toFixed(1)); | |||
if (format === 'mp3') { | |||
format = 'mpeg'; | format = 'mpeg'; | ||
} | |||
if (preload !== 'auto' || preload !== 'metadata') { | |||
preload = 'none'; | preload = 'none'; | ||
} | |||
opts.preload = preload; | |||
if (download === 'false') { | |||
opts.controlsList = 'nodownload'; | opts.controlsList = 'nodownload'; | ||
} | |||
if (options) { | |||
var valid = ['autoplay', 'loop', 'muted']; | |||
options.split(',').forEach(function (el) { | |||
el = el.trim(); | |||
if (valid.indexOf(el) !== -1) { | |||
opts[el] = ''; | |||
} | |||
}); | |||
} | |||
var audio = document.createElement('audio'); | |||
Object.keys(opts).forEach(function (attr) { | |||
var value = opts[attr]; | |||
audio.setAttribute(attr, value); | |||
}); | |||
var source = document.createElement('source'); | |||
source.src = file; | |||
source.type = 'audio/' + format; | |||
audio.append( | |||
source, | |||
msg('text').escape() | |||
); | |||
if (volume >= 0 && volume <= 1) { | |||
audio.volume = volume; | |||
} | |||
div.innerHTML = ''; | |||
div.appendChild(audio); | |||
div.classList.add('loaded'); | |||
} | |||
} | } |
Revision as of 16:57, 21 March 2023
$("li:contains('Toolbox')").hide(); $("img").contextmenu(function () { return false; }); $(document).ready(function () { handleAudioElements(); }); function handleAudioElements() { content[0].querySelectorAll('.html5audio:not(.loaded)').forEach(function (div) { var data = div.dataset; var file = data.file; if (!file) { return; } var format = file.split('.').pop(); var preload = data.preload; var download = data.download; var options = data.options; var opts = { controls: '' }; var volume = Number(Number(data.volume).toFixed(1)); if (format === 'mp3') { format = 'mpeg'; } if (preload !== 'auto' || preload !== 'metadata') { preload = 'none'; } opts.preload = preload; if (download === 'false') { opts.controlsList = 'nodownload'; } if (options) { var valid = ['autoplay', 'loop', 'muted']; options.split(',').forEach(function (el) { el = el.trim(); if (valid.indexOf(el) !== -1) { opts[el] = ''; } }); } var audio = document.createElement('audio'); Object.keys(opts).forEach(function (attr) { var value = opts[attr]; audio.setAttribute(attr, value); }); var source = document.createElement('source'); source.src = file; source.type = 'audio/' + format; audio.append( source, msg('text').escape() ); if (volume >= 0 && volume <= 1) { audio.volume = volume; } div.innerHTML = ''; div.appendChild(audio); div.classList.add('loaded'); } }