Browse Source

listen to hash change and update

tags/v0.1.0
salus-sage 5 years ago
parent
commit
f2e0b7402a
1 changed files with 38 additions and 11 deletions
  1. +38
    -11
      app/scripts/contentscript.js

+ 38
- 11
app/scripts/contentscript.js View File

@@ -1,4 +1,5 @@
import * as WaveformPlaylist from 'waveform-playlist'; import * as WaveformPlaylist from 'waveform-playlist';
import delay from 'delay';


async function init() { async function init() {
document.body.innerHTML = ` document.body.innerHTML = `
@@ -34,26 +35,52 @@ async function init() {
e => eventEmitter.emit("mastervolumechange", e.target.value) e => eventEmitter.emit("mastervolumechange", e.target.value)
); );



// Read target fragment from URL // Read target fragment from URL
let start, end; let start, end;
const fragmentIdentifier = window.location.hash;
if (fragmentIdentifier) {
const match = fragmentIdentifier.match(/#t=(\d+(?:\.\d+)?)?(?:,(\d+(?:\.\d+)?))?/);
if (match) {
if (match[1] !== undefined) {
start = Number.parseFloat(match[1]);
}
if (match[2] !== undefined) {
end = Number.parseFloat(match[2]);

async function setFragmentToSelection() {

const fragmentIdentifier = window.location.hash;
if (fragmentIdentifier) {
const match = fragmentIdentifier.match(/#t=(\d+(?:\.\d+)?)?(?:,(\d+(?:\.\d+)?))?/);
if (match) {
if (match[1] !== undefined) {
start = Number.parseFloat(match[1]);
}
if (match[2] !== undefined) {
end = Number.parseFloat(match[2]);
}
} }
} }

// Emit event to update selection in player
eventEmitter.emit('select', start, end);

} }



// Bind window hash change to update player
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');
});

// Load Playlist
await playlist.load([{ await playlist.load([{
src: document.URL,
selected: { start, end },
src: document.URL
}]); }]);


// update start & end for initial load
setFragmentToSelection();

// Start playing. A tiny delay seems needed in Firefox to show the cursor at the right place. // Start playing. A tiny delay seems needed in Firefox to show the cursor at the right place.
requestAnimationFrame(() => eventEmitter.emit('play')); requestAnimationFrame(() => eventEmitter.emit('play'));
} }


Loading…
Cancel
Save