diff --git a/.epub/example/OEBPS/scripts/ipub.js b/.epub/example/OEBPS/scripts/ipub.js index f70a681..b8a8f01 100644 --- a/.epub/example/OEBPS/scripts/ipub.js +++ b/.epub/example/OEBPS/scripts/ipub.js @@ -21,6 +21,151 @@ class IPUBElement extends HTMLElement { } } +class IPUBAudio extends IPUBElement { + static elementName = "ipub-audio"; + + /** + * @param {boolean} [forced=false] + * @throws {Error} + * @returns {Promise} + */ + play(forced = false) { + if (!this.#audioElement.readyState > HTMLMediaElement.HAVE_CURRENT_DATA) { + throw new Error("IPUBAudio: audio is not ready"); + } + if (forced) { + this.setAttribute("forced", "true"); + } + return this.#audioElement.play(); + } + + /** + * @param {boolean} [forced=false] + */ + pause(forced = false) { + if (forced) { + this.setAttribute("forced", "true"); + } + this.#audioElement.pause(); + } + + /** + * @param {boolean} state + */ + setLoop(state) { + this.#audioElement.loop = state; + } + + /** + * @returns {boolean} + */ + getLoop() { + return this.#audioElement.loop; + } + + connectedCallback() { + super.connectedCallback(); + + const audio = this.querySelector("audio"); + if (!audio) { + console.error("IPUBAudio: Missing child