contentscript.js 1.6 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import * as WaveformPlaylist from 'waveform-playlist';
  2. async function init() {
  3. document.body.innerHTML = `
  4. <main>
  5. <div id="controls">
  6. <button id="playpause">⏯</button>
  7. <div>
  8. <label for="volume" id="volume-label">🔊</label>
  9. <input type="range" min="0" max="100" value="100" id="volume" />
  10. </div>
  11. </div>
  12. <div id="container">
  13. </div>
  14. </main>
  15. `;
  16. const playlist = WaveformPlaylist.init({
  17. container: document.getElementById('container'),
  18. timescale: true,
  19. waveHeight: 100,
  20. state: 'select',
  21. });
  22. const eventEmitter = playlist.getEventEmitter();
  23. // Hook up play/pause and volume inputs
  24. const playpauseEl = document.getElementById('playpause');
  25. const volumeEl = document.getElementById('volume');
  26. playpauseEl.addEventListener('click',
  27. e => eventEmitter.emit(playlist.isPlaying() ? 'pause' : 'play')
  28. );
  29. volumeEl.addEventListener('input',
  30. e => eventEmitter.emit("mastervolumechange", e.target.value)
  31. );
  32. // Read target fragment from URL
  33. let start, end;
  34. const fragmentIdentifier = window.location.hash;
  35. if (fragmentIdentifier) {
  36. const match = fragmentIdentifier.match(/#t=(\d+(?:\.\d+)?)?(?:,(\d+(?:\.\d+)?))?/);
  37. if (match) {
  38. if (match[1] !== undefined) {
  39. start = Number.parseFloat(match[1]);
  40. }
  41. if (match[2] !== undefined) {
  42. end = Number.parseFloat(match[2]);
  43. }
  44. }
  45. }
  46. await playlist.load([{
  47. src: document.URL,
  48. selected: { start, end },
  49. }]);
  50. // Start playing. A tiny delay seems needed in Firefox to show the cursor at the right place.
  51. requestAnimationFrame(() => eventEmitter.emit('play'));
  52. }
  53. init();