learn.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* lynx-share.com: a tutorial page (/learn/<slug>/), built by _src/build.py from _src/learn/. The page works without
  2. this script (English video, every caption track in the CC menu, chapter list as text). The script:
  3. - plays the narration of the reader's language when the video has one (figure data-voices), otherwise English;
  4. - turns on the captions in the reader's language when the narration is in another one;
  5. - lets the chapter buttons seek;
  6. - with a YouTube address (figure data-yt, {lang: video id}), shows a click-to-load YouTube player: nothing is
  7. requested from YouTube or Google before that click. */
  8. (() => {
  9. const fig = document.querySelector('.tvid'); if (!fig) return;
  10. const video = fig.querySelector('video'), M = '/assets/media/learn/', v = fig.dataset.v;
  11. const voices = (fig.dataset.voices || 'en').split(' ');
  12. let voice = 'en', lang = 'en';
  13. const started = () => started.done || video.currentTime > 0;
  14. let want = 'en';
  15. function swap(){ // the reader's narration: done at the first Play, so nothing loads before
  16. if (want === voice) return false;
  17. voice = want;
  18. const s = video.querySelectorAll('source');
  19. if (s[0]) s[0].src = M + v + '-' + voice + '-1080.mp4';
  20. if (s[1]) s[1].src = M + v + '-' + voice + '-720.mp4';
  21. video.load();
  22. return true;
  23. }
  24. function apply(l){
  25. lang = l;
  26. want = voices.includes(l) ? l : 'en';
  27. if (!started()){
  28. video.poster = '/assets/img/learn/' + v + '-' + want + '.webp';
  29. fig.querySelectorAll('a[download]').forEach(a => { a.href = a.href.replace(/-(?:[a-z]{2}(?:-[A-Za-z]+)?)-(720|1080)\.mp4$/, '-' + want + '-$1.mp4'); });
  30. }
  31. for (const t of video.textTracks) t.mode = (t.language === l && l !== want) ? 'showing' : 'disabled';
  32. }
  33. video.addEventListener('play', () => { if (!started.done && swap()){ const p = video.play(); if (p && p.catch) p.catch(() => {}); } started.done = true; });
  34. document.addEventListener('lynx:lang', e => apply(e.detail));
  35. apply(document.documentElement.lang || 'en');
  36. fig.parentElement.querySelectorAll('[data-seek]').forEach(b => b.addEventListener('click', () => {
  37. if (!started.done){ swap(); started.done = true; } // (a seek before the first Play: the reader's narration first)
  38. video.currentTime = +b.dataset.seek; const p = video.play(); if (p && p.catch) p.catch(() => {});
  39. }));
  40. let yt = {}; try { yt = JSON.parse(fig.dataset.yt || '{}'); } catch (e) {}
  41. const btn = fig.querySelector('.ytbtn');
  42. if (btn) btn.addEventListener('click', () => {
  43. const id = yt[lang] || yt[voice] || yt.en || Object.values(yt)[0]; if (!id) return;
  44. const f = document.createElement('iframe');
  45. f.src = 'https://www.youtube-nocookie.com/embed/' + encodeURIComponent(id) + '?autoplay=1&rel=0&cc_load_policy=1&cc_lang_pref=' + encodeURIComponent(lang) + '&hl=' + encodeURIComponent(lang);
  46. f.title = 'YouTube'; f.allow = 'autoplay; encrypted-media; picture-in-picture; fullscreen'; f.allowFullscreen = true;
  47. f.referrerPolicy = 'strict-origin-when-cross-origin';
  48. video.pause(); fig.querySelector('.vbox').replaceChildren(f); btn.closest('.ytline').remove();
  49. });
  50. })();