Last active
December 11, 2016 20:32
-
-
Save nbeloglazov/828305a48e9ada939abf25ff30ce1cca to your computer and use it in GitHub Desktop.
Script for Rachel's English site to play all audio recordings in soundboards session. Each recording will be played 3 times with pauses for you to repeat. How to use: 1. go to page with audio recordings 2. in url type "javascript:" and paste this script after colon 3. hit enter, it should start playing recordings one by one
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function playAndWait(el) { | |
return new Promise((resolve) => { | |
el.play().then(() => { | |
setTimeout(resolve, (el.duration * 2.5) * 1000); | |
}); | |
}); | |
} | |
function swapEveryPair(els) { | |
const elements = Array.from(els); | |
for (let i = 0; i < elements.length; i += 2) { | |
const tmp = elements[i]; | |
elements[i] = elements[i + 1]; | |
elements[i + 1] = tmp; | |
} | |
return elements; | |
} | |
function playNTimes(audioTag, n) { | |
let res = Promise.resolve(); | |
for (let i = 0; i < n; i++) { | |
res = res.then(() => playAndWait(audioTag)); | |
} | |
return res; | |
} | |
function playAll(audioTags, timesToPlay) { | |
return audioTags.reduce((promise, tag) => { | |
return promise.then(() => playNTimes(tag, timesToPlay)); | |
}, Promise.resolve()); | |
} | |
function getAllAudio() { | |
return Array.from(document.querySelectorAll('audio')); | |
} | |
function playAllAudio(timesToPlay) { | |
return playAll(swapEveryPair(getAllAudio()), timesToPlay); | |
} | |
playAllAudio(3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment