| 1 |
(function () { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
function withParams(baseUrl, updates) { |
| 5 |
if (!baseUrl) return ''; |
| 6 |
try { |
| 7 |
var u = new URL(baseUrl, window.location.href); |
| 8 |
Object.keys(updates).forEach(function (k) { |
| 9 |
u.searchParams.set(k, String(updates[k])); |
| 10 |
}); |
| 11 |
return u.toString(); |
| 12 |
} catch (e) { |
| 13 |
return baseUrl; |
| 14 |
} |
| 15 |
} |
| 16 |
|
| 17 |
function extractEmbedSrcFromOEmbed(html) { |
| 18 |
if (!html) return ''; |
| 19 |
try { |
| 20 |
var tmp = document.createElement('div'); |
| 21 |
tmp.innerHTML = html; |
| 22 |
var iframe = tmp.querySelector('iframe'); |
| 23 |
return iframe ? (iframe.getAttribute('src') || '') : ''; |
| 24 |
} catch (e) { |
| 25 |
return ''; |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
function fetchOEmbedSrc(videoUrl) { |
| 30 |
if (!videoUrl) return Promise.resolve(''); |
| 31 |
var endpoint = 'https://vimeo.com/api/oembed.json?url=' + encodeURIComponent(videoUrl); |
| 32 |
return fetch(endpoint) |
| 33 |
.then(function (r) { return r.ok ? r.json() : null; }) |
| 34 |
.then(function (data) { |
| 35 |
return data && data.html ? extractEmbedSrcFromOEmbed(data.html) : ''; |
| 36 |
}) |
| 37 |
.catch(function () { return ''; }); |
| 38 |
} |
| 39 |
|
| 40 |
function buildSrc(embedSrc, id, opts) { |
| 41 |
var base = embedSrc || ('https://player.vimeo.com/video/' + encodeURIComponent(id)); |
| 42 |
return withParams(base, { |
| 43 |
autoplay: opts.autoplay ? 1 : 0, |
| 44 |
muted: opts.muted ? 1 : 0, |
| 45 |
loop: opts.loop ? 1 : 0, |
| 46 |
dnt: opts.dnt ? 1 : 0, |
| 47 |
title: opts.chrome ? 1 : 0, |
| 48 |
byline: opts.chrome ? 1 : 0, |
| 49 |
portrait: opts.chrome ? 1 : 0 |
| 50 |
}); |
| 51 |
} |
| 52 |
|
| 53 |
function initOne(root) { |
| 54 |
if (!root || root.__bkbgVmpInit) return; |
| 55 |
root.__bkbgVmpInit = true; |
| 56 |
|
| 57 |
var btn = root.querySelector('.bkbg-vmp-play'); |
| 58 |
var mount = root.querySelector('.bkbg-vmp-embed'); |
| 59 |
if (!btn || !mount) return; |
| 60 |
|
| 61 |
var embedSrc = root.getAttribute('data-embed-src') || ''; |
| 62 |
var id = root.getAttribute('data-video-id') || ''; |
| 63 |
var videoUrl = root.getAttribute('data-video-url') || ''; |
| 64 |
|
| 65 |
var opts = { |
| 66 |
dnt: root.getAttribute('data-dnt') === '1', |
| 67 |
autoplay: root.getAttribute('data-autoplay') === '1', |
| 68 |
muted: root.getAttribute('data-muted') === '1', |
| 69 |
loop: root.getAttribute('data-loop') === '1', |
| 70 |
chrome: root.getAttribute('data-chrome') === '1' |
| 71 |
}; |
| 72 |
|
| 73 |
function injectIframe(src) { |
| 74 |
if (!src) return; |
| 75 |
var iframe = document.createElement('iframe'); |
| 76 |
iframe.className = 'bkbg-vmp-iframe'; |
| 77 |
iframe.src = src; |
| 78 |
iframe.title = 'Vimeo video'; |
| 79 |
iframe.allow = 'autoplay; fullscreen; picture-in-picture; clipboard-write; encrypted-media'; |
| 80 |
iframe.referrerPolicy = 'strict-origin-when-cross-origin'; |
| 81 |
iframe.allowFullscreen = true; |
| 82 |
|
| 83 |
mount.innerHTML = ''; |
| 84 |
mount.appendChild(iframe); |
| 85 |
root.classList.add('is-playing'); |
| 86 |
} |
| 87 |
|
| 88 |
function play() { |
| 89 |
if (root.classList.contains('is-playing')) return; |
| 90 |
|
| 91 |
// If we already have embedSrc (with hash), use it directly |
| 92 |
if (embedSrc) { |
| 93 |
injectIframe(buildSrc(embedSrc, id, opts)); |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
// Fallback: fetch oEmbed on the fly to get the proper iframe src with hash |
| 98 |
var urlToFetch = videoUrl || (id ? ('https://vimeo.com/' + id) : ''); |
| 99 |
if (!urlToFetch) return; |
| 100 |
|
| 101 |
btn.disabled = true; |
| 102 |
fetchOEmbedSrc(urlToFetch).then(function (fetchedSrc) { |
| 103 |
btn.disabled = false; |
| 104 |
embedSrc = fetchedSrc; // cache for future |
| 105 |
injectIframe(buildSrc(embedSrc, id, opts)); |
| 106 |
}); |
| 107 |
} |
| 108 |
|
| 109 |
btn.addEventListener('click', function (e) { |
| 110 |
e.preventDefault(); |
| 111 |
play(); |
| 112 |
}); |
| 113 |
|
| 114 |
btn.addEventListener('keydown', function (e) { |
| 115 |
var key = e.key || e.code; |
| 116 |
if (key === 'Enter' || key === ' ' || key === 'Spacebar') { |
| 117 |
e.preventDefault(); |
| 118 |
play(); |
| 119 |
} |
| 120 |
}); |
| 121 |
} |
| 122 |
|
| 123 |
function initAll() { |
| 124 |
var nodes = document.querySelectorAll('.bkbg-vmp-wrap'); |
| 125 |
for (var i = 0; i < nodes.length; i++) initOne(nodes[i]); |
| 126 |
} |
| 127 |
|
| 128 |
if (document.readyState === 'loading') { |
| 129 |
document.addEventListener('DOMContentLoaded', initAll); |
| 130 |
} else { |
| 131 |
initAll(); |
| 132 |
} |
| 133 |
})(); |
| 134 |
|