zielheart commited on
Commit
0946581
·
verified ·
1 Parent(s): 7bf2859

Create script.js

Browse files
Files changed (1) hide show
  1. script.js +124 -0
script.js ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (function(){
2
+ function preventAll(el){
3
+ var evs = ['click','dblclick','contextmenu','mousedown','mouseup','touchstart','touchend','pointerdown','pointerup','keydown'];
4
+ evs.forEach(function(evt){
5
+ el.addEventListener(evt, function(e){ e.preventDefault(); e.stopPropagation(); return false; }, true);
6
+ });
7
+ }
8
+
9
+ function prepareVideo(v){
10
+ if (!v || v.dataset.noUiPrepared) return;
11
+ v.dataset.noUiPrepared = '1';
12
+ v.autoplay = true;
13
+ v.muted = true;
14
+ v.loop = true;
15
+ v.playsInline = true;
16
+ v.setAttribute('playsinline','');
17
+ v.setAttribute('webkit-playsinline','');
18
+ v.removeAttribute('controls');
19
+ v.controls = false;
20
+ try { v.disablePictureInPicture = true; } catch(e){}
21
+ try { v.setAttribute('tabindex','-1'); } catch(e){}
22
+ try { v.setAttribute('aria-hidden','true'); } catch(e){}
23
+ preventAll(v);
24
+ var fig = (v.closest && v.closest('figure.wp-block-video')) || v.parentElement;
25
+ if (fig){
26
+ preventAll(fig);
27
+ if (!fig.querySelector('.no-ui-video-cover')){
28
+ var cover = document.createElement('div');
29
+ cover.className = 'no-ui-video-cover';
30
+ fig.appendChild(cover);
31
+ // Try to start background audio on first interaction within the video area
32
+ try {
33
+ var started = false;
34
+ var startAudio = function(){
35
+ if (started) return; started = true;
36
+ var a = document.getElementById('bg-music') || document.querySelector('audio');
37
+ if (a) {
38
+ try { a.muted = false; } catch(e){}
39
+ try { a.play(); } catch(e){}
40
+ }
41
+ };
42
+ ['pointerdown','touchstart','click'].forEach(function(evt){
43
+ cover.addEventListener(evt, function(){ startAudio(); }, { capture:true, once:true });
44
+ });
45
+ } catch(e){}
46
+ preventAll(cover);
47
+ }
48
+ }
49
+ function tryPlay(){
50
+ var p; try { p = v.play(); } catch(e){}
51
+ if (p && typeof p.catch === 'function') p.catch(function(){});
52
+ }
53
+ tryPlay();
54
+ }
55
+
56
+ // Attempt to autoplay background audio immediately on load.
57
+ function getBgAudio(){
58
+ return document.getElementById('bg-music') || document.querySelector('audio');
59
+ }
60
+
61
+ function setupAudioUnlock(a){
62
+ a = a || getBgAudio();
63
+ if (!a) return;
64
+ var fired = false;
65
+ function unlock(){
66
+ if (fired) return; fired = true;
67
+ try { a.muted = false; } catch(e){}
68
+ try { a.play(); } catch(e){}
69
+ }
70
+ ['pointerdown','touchstart','click','keydown'].forEach(function(evt){
71
+ window.addEventListener(evt, function handler(){ unlock(); window.removeEventListener(evt, handler, true); }, { capture:true });
72
+ });
73
+ document.addEventListener('visibilitychange', function(){ if (!document.hidden && !fired) unlock(); });
74
+ }
75
+
76
+ function autoPlayAudioImmediate(){
77
+ var a = getBgAudio();
78
+ if (!a) return;
79
+ try { a.loop = true; } catch(e){}
80
+ try { a.autoplay = true; } catch(e){}
81
+ try { if (!a.preload) a.preload = 'auto'; } catch(e){}
82
+ try { a.muted = false; } catch(e){}
83
+ var p;
84
+ try { p = a.play(); } catch(e) { p = null; }
85
+ if (p && typeof p.then === 'function') {
86
+ p.then(function(){ /* started with sound */ }).catch(function(){
87
+ try { a.muted = true; a.play().catch(function(){}); } catch(e){}
88
+ setupAudioUnlock(a);
89
+ });
90
+ } else {
91
+ // If no promise support, set up unlock fallback
92
+ setupAudioUnlock(a);
93
+ }
94
+ }
95
+
96
+ function setupAll(){
97
+ var vids = document.querySelectorAll('figure.wp-block-video video, video#mainVideo, video.auto-noui');
98
+ vids.forEach(prepareVideo);
99
+ autoPlayAudioImmediate();
100
+ }
101
+
102
+ if (document.readyState === 'loading') {
103
+ document.addEventListener('DOMContentLoaded', setupAll);
104
+ } else {
105
+ setupAll();
106
+ }
107
+ try {
108
+ var mo = new MutationObserver(function(muts){
109
+ muts.forEach(function(m){
110
+ m.addedNodes && m.addedNodes.forEach(function(n){
111
+ if (n.nodeType === 1) {
112
+ if (n.matches && (n.matches('video') || n.matches('figure.wp-block-video'))){
113
+ if (n.matches('video')) prepareVideo(n);
114
+ n.querySelectorAll && n.querySelectorAll('video').forEach(prepareVideo);
115
+ } else {
116
+ n.querySelectorAll && n.querySelectorAll('video').forEach(prepareVideo);
117
+ }
118
+ }
119
+ });
120
+ });
121
+ });
122
+ mo.observe(document.documentElement, { childList: true, subtree: true });
123
+ } catch(e){}
124
+ })();