I use this script to replace certain text strings with images whenever they are present in a field:
<div id=front>{{Front}}</div>
<script>
var str = document.getElementById("front").innerHTML;
var res = str.replace('1', "<img src='_1.png'>");
document.getElementById("front").innerHTML = res;
</script>
Is it possible to do the same for audio?
So whenever 1 is present in the field, the saved audio file _1.wav is played, just as if the field were to contain [sound:_1.wav].
I’ve tried a few different things but can’t get it to work. This for example just plays the audio regardless of if 1 is present in the field or not:
var res = str.replace('1', "[sound:_1.wav]");
If I instead use this script I get what I want…
<script>
var audioElement = new Audio('_1.wav');
var field = document.getElementById('front').innerText;
if (field.includes('1')) {
audioElement.play();
}
</script>
… only it results in all audio files playing at the same time if I have several present in the field.
Thank you @hkr, I’ve tried your script now. I have two issues, the audio is played in the order of the var audioMap = [ mapping and not the order of the card (so if I have 1 3 2 in a field it is played as 1 2 3 regardless). And if the same text string exists at several different places in a field it is only played once, I need it to be played for every instance. Can this be fixed?