From 3dc85cfd5bf5572e496b1e7f6f36f52453309e1b Mon Sep 17 00:00:00 2001 From: Gerben Date: Sun, 20 Oct 2019 21:46:34 +0530 Subject: [PATCH] Nitpick code --- app/audio-player/contentscript.js | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/app/audio-player/contentscript.js b/app/audio-player/contentscript.js index 7df4b95..ab4e90a 100644 --- a/app/audio-player/contentscript.js +++ b/app/audio-player/contentscript.js @@ -35,11 +35,14 @@ async function init() { e => eventEmitter.emit("mastervolumechange", e.target.value) ); + // Load the file + await playlist.load([{ + src: document.URL, + }]); - // Read target fragment from URL - let start, end; - - async function setFragmentToSelection() { + function syncFragmentToSelection() { + // Read target fragment from URL + let start, end; const fragmentIdentifier = window.location.hash; if (fragmentIdentifier) { @@ -56,30 +59,22 @@ async function init() { // Emit event to update selection in player eventEmitter.emit('select', start, end); - } - // Bind window hash change to update player - window.addEventListener('hashchange', async function() { + window.addEventListener('hashchange', async function() { if(playlist.isPlaying()) { eventEmitter.emit('stop') // pause needs a small delay, coz the lib doesn't // update player view (drawRequest) when isPlaying() = true. await delay(10); } - await setFragmentToSelection(); - eventEmitter.emit('play'); + await syncFragmentToSelection(); + eventEmitter.emit('play'); }); - - // Load Playlist - await playlist.load([{ - src: document.URL - }]); - - // update start & end for initial load - setFragmentToSelection(); + // Read start & end from window location. + syncFragmentToSelection(); // Start playing. A tiny delay seems needed in Firefox to show the cursor at the right place. requestAnimationFrame(() => eventEmitter.emit('play'));