bookmark-audio-fragment/app/scripts/ contentscript.js
68 lines
1.6 KiB

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