File size: 4,438 Bytes
6e1f067
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
(function(){
  function preventAll(el){
    var evs = ['click','dblclick','contextmenu','mousedown','mouseup','touchstart','touchend','pointerdown','pointerup','keydown'];
    evs.forEach(function(evt){
      el.addEventListener(evt, function(e){ e.preventDefault(); e.stopPropagation(); return false; }, true);
    });
  }

  function prepareVideo(v){
    if (!v || v.dataset.noUiPrepared) return;
    v.dataset.noUiPrepared = '1';
    v.autoplay = true;
    v.muted = true;
    v.loop = true;
    v.playsInline = true;
    v.setAttribute('playsinline','');
    v.setAttribute('webkit-playsinline','');
    v.removeAttribute('controls');
    v.controls = false;
    try { v.disablePictureInPicture = true; } catch(e){}
    try { v.setAttribute('tabindex','-1'); } catch(e){}
    try { v.setAttribute('aria-hidden','true'); } catch(e){}
    preventAll(v);
    var fig = (v.closest && v.closest('figure.wp-block-video')) || v.parentElement;
    if (fig){
      preventAll(fig);
      if (!fig.querySelector('.no-ui-video-cover')){
        var cover = document.createElement('div');
        cover.className = 'no-ui-video-cover';
        fig.appendChild(cover);
        // Try to start background audio on first interaction within the video area
        try {
          var started = false;
          var startAudio = function(){
            if (started) return; started = true;
            var a = document.getElementById('bg-music') || document.querySelector('audio');
            if (a) {
              try { a.muted = false; } catch(e){}
              try { a.play(); } catch(e){}
            }
          };
          ['pointerdown','touchstart','click'].forEach(function(evt){
            cover.addEventListener(evt, function(){ startAudio(); }, { capture:true, once:true });
          });
        } catch(e){}
        preventAll(cover);
      }
    }
    function tryPlay(){
      var p; try { p = v.play(); } catch(e){}
      if (p && typeof p.catch === 'function') p.catch(function(){});
    }
    tryPlay();
  }

  // Attempt to autoplay background audio immediately on load.
  function getBgAudio(){
    return document.getElementById('bg-music') || document.querySelector('audio');
  }

  function setupAudioUnlock(a){
    a = a || getBgAudio();
    if (!a) return;
    var fired = false;
    function unlock(){
      if (fired) return; fired = true;
      try { a.muted = false; } catch(e){}
      try { a.play(); } catch(e){}
    }
    ['pointerdown','touchstart','click','keydown'].forEach(function(evt){
      window.addEventListener(evt, function handler(){ unlock(); window.removeEventListener(evt, handler, true); }, { capture:true });
    });
    document.addEventListener('visibilitychange', function(){ if (!document.hidden && !fired) unlock(); });
  }

  function autoPlayAudioImmediate(){
    var a = getBgAudio();
    if (!a) return;
    try { a.loop = true; } catch(e){}
    try { a.autoplay = true; } catch(e){}
    try { if (!a.preload) a.preload = 'auto'; } catch(e){}
    try { a.muted = false; } catch(e){}
    var p;
    try { p = a.play(); } catch(e) { p = null; }
    if (p && typeof p.then === 'function') {
      p.then(function(){ /* started with sound */ }).catch(function(){
        try { a.muted = true; a.play().catch(function(){}); } catch(e){}
        setupAudioUnlock(a);
      });
    } else {
      // If no promise support, set up unlock fallback
      setupAudioUnlock(a);
    }
  }

  function setupAll(){
    var vids = document.querySelectorAll('figure.wp-block-video video, video#mainVideo, video.auto-noui');
    vids.forEach(prepareVideo);
    autoPlayAudioImmediate();
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', setupAll);
  } else {
    setupAll();
  }
  try {
    var mo = new MutationObserver(function(muts){
      muts.forEach(function(m){
        m.addedNodes && m.addedNodes.forEach(function(n){
          if (n.nodeType === 1) {
            if (n.matches && (n.matches('video') || n.matches('figure.wp-block-video'))){
              if (n.matches('video')) prepareVideo(n);
              n.querySelectorAll && n.querySelectorAll('video').forEach(prepareVideo);
            } else {
              n.querySelectorAll && n.querySelectorAll('video').forEach(prepareVideo);
            }
          }
        });
      });
    });
    mo.observe(document.documentElement, { childList: true, subtree: true });
  } catch(e){}
})();