PluginProbe
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor / 2.0.6
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor v2.0.6
2.0.13 2.0.12 2.0.11 2.0.10 2.0.9 trunk 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8
blockenberg / blocks / youtube-performance / frontend.js

frontend.js in Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor 2.0.6, at blocks/youtube-performance/frontend.js

102 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function () {
2 'use strict';
3
4 function clamp(n, min, max) {
5 n = parseFloat(n);
6 if (isNaN(n)) n = min;
7 return Math.max(min, Math.min(max, n));
8 }
9
10 function buildSrc(root) {
11 var id = root.getAttribute('data-video-id') || '';
12 if (!id) return '';
13
14 var privacy = root.getAttribute('data-privacy') === '1';
15 var host = privacy ? 'https://www.youtube-nocookie.com' : 'https://www.youtube.com';
16
17 var controls = root.getAttribute('data-controls') === '1';
18 var rel = root.getAttribute('data-rel') === '1';
19 var mute = root.getAttribute('data-mute') === '1';
20 var loop = root.getAttribute('data-loop') === '1';
21 var start = clamp(root.getAttribute('data-start') || 0, 0, 86400);
22
23 var params = [];
24 params.push('autoplay=1');
25 params.push('controls=' + (controls ? '1' : '0'));
26 params.push('rel=' + (rel ? '1' : '0'));
27 if (mute) params.push('mute=1');
28 if (start > 0) params.push('start=' + Math.floor(start));
29 if (loop) {
30 params.push('loop=1');
31 params.push('playlist=' + encodeURIComponent(id));
32 }
33
34 return host + '/embed/' + encodeURIComponent(id) + '?' + params.join('&');
35 }
36
37 function fallbackPosterImg(img) {
38 if (!img || !img.getAttribute) return;
39 img.addEventListener('error', function () {
40 var src = img.getAttribute('src') || '';
41 if (src.indexOf('maxresdefault.jpg') !== -1) {
42 img.setAttribute('src', src.replace('maxresdefault.jpg', 'hqdefault.jpg'));
43 }
44 });
45 }
46
47 function initOne(root) {
48 if (!root || root.__bkbgYtpInit) return;
49 root.__bkbgYtpInit = true;
50
51 var btn = root.querySelector('.bkbg-ytp-play');
52 var mount = root.querySelector('.bkbg-ytp-embed');
53 var posterImg = root.querySelector('.bkbg-ytp-poster-img');
54
55 if (posterImg) fallbackPosterImg(posterImg);
56
57 if (!btn || !mount) return;
58
59 function play() {
60 if (root.classList.contains('is-playing')) return;
61 var src = buildSrc(root);
62 if (!src) return;
63
64 var iframe = document.createElement('iframe');
65 iframe.className = 'bkbg-ytp-iframe';
66 iframe.src = src;
67 iframe.title = 'YouTube video';
68 iframe.allow = 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share';
69 iframe.allowFullscreen = true;
70
71 mount.innerHTML = '';
72 mount.appendChild(iframe);
73
74 root.classList.add('is-playing');
75 }
76
77 btn.addEventListener('click', function (e) {
78 e.preventDefault();
79 play();
80 });
81
82 btn.addEventListener('keydown', function (e) {
83 var key = e.key || e.code;
84 if (key === 'Enter' || key === ' ' || key === 'Spacebar') {
85 e.preventDefault();
86 play();
87 }
88 });
89 }
90
91 function initAll() {
92 var nodes = document.querySelectorAll('.bkbg-ytp-wrap');
93 for (var i = 0; i < nodes.length; i++) initOne(nodes[i]);
94 }
95
96 if (document.readyState === 'loading') {
97 document.addEventListener('DOMContentLoaded', initAll);
98 } else {
99 initAll();
100 }
101 })();
102