| 1 |
/** |
| 2 |
* Elementor functions for YouTube videos. |
| 3 |
* |
| 4 |
* @since 1.3.5 |
| 5 |
* |
| 6 |
* @author Epiphyt |
| 7 |
* @license GPL2 |
| 8 |
* @package epiphyt\Embed_Privacy |
| 9 |
*/ |
| 10 |
document.addEventListener( 'DOMContentLoaded', function() { |
| 11 |
replaceYouTubeEmbeds(); |
| 12 |
|
| 13 |
/** |
| 14 |
* Replace YouTube video container with the embed iframe. |
| 15 |
*/ |
| 16 |
function replaceYouTubeEmbeds() { |
| 17 |
const youTubeEmbeds = document.querySelectorAll( '.embed-privacy-container.embed-youtube' ); |
| 18 |
|
| 19 |
if ( ! youTubeEmbeds ) { |
| 20 |
return; |
| 21 |
} |
| 22 |
|
| 23 |
// check for changed YouTube embeds |
| 24 |
const observeEmbeds = new MutationObserver( function( records, observer ) { |
| 25 |
const embeds = document.querySelectorAll( '.embed-privacy-container.embed-youtube .embed-privacy-content > .elementor-element' ); |
| 26 |
|
| 27 |
if ( ! embeds.length ) { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
for ( let i = 0; i < embeds.length; i++ ) { |
| 32 |
// get the video element to replace later |
| 33 |
const embedVideo = embeds[ i ].querySelector( 'div.elementor-video' ); |
| 34 |
|
| 35 |
if ( ! embedVideo ) { |
| 36 |
continue; |
| 37 |
} |
| 38 |
|
| 39 |
// get the video settings |
| 40 |
const settings = JSON.parse( embeds[ i ].getAttribute( 'data-settings' ) ); |
| 41 |
|
| 42 |
if ( ! settings ) { |
| 43 |
continue; |
| 44 |
} |
| 45 |
|
| 46 |
const iframe = document.createElement( 'iframe' ); |
| 47 |
const divider = settings.youtube_url.indexOf( '?' ) !== -1 ? '&' : '?'; |
| 48 |
let url = settings.youtube_url.replace( 'watch?v=', 'embed/' ) + divider; |
| 49 |
|
| 50 |
if ( settings.youtube_url.indexOf( 'youtu.be' ) !== -1 ) { |
| 51 |
const urlObject = new URL( settings.youtube_url ); |
| 52 |
|
| 53 |
url = 'https://www.youtube-nocookie.com/embed' + urlObject.pathname + '?'; |
| 54 |
} |
| 55 |
|
| 56 |
const properties = { |
| 57 |
autoplay: settings.autoplay ? 1 : 0, |
| 58 |
cc_load_policy: settings.cc_load_policy ? 1 : 0, |
| 59 |
controls: settings.controls ? 1 : 0, |
| 60 |
end: settings.end, |
| 61 |
playsinline: settings.play_on_mobile ? 1 : 0, |
| 62 |
rel: settings.rel ? 1 : 0, |
| 63 |
start: settings.start, |
| 64 |
}; |
| 65 |
|
| 66 |
for ( const property in properties ) { |
| 67 |
if ( properties[ property ] === undefined ) { |
| 68 |
continue; |
| 69 |
} |
| 70 |
|
| 71 |
url += property + '=' + properties[ property ] + '&'; |
| 72 |
|
| 73 |
if ( property === 'autoplay' && properties[ property ] === 1 && ! properties.playsinline ) { |
| 74 |
url += 'playsinline=1&'; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// build iframe to replace embed video div |
| 79 |
iframe.src = url.replace( /&$/, '' ); |
| 80 |
iframe.allowFullscreen = 1; |
| 81 |
iframe.class = 'elementor-video'; |
| 82 |
iframe.style.height = '100%'; |
| 83 |
iframe.style.width = '100%'; |
| 84 |
|
| 85 |
// replace the video element with the iframe |
| 86 |
embedVideo.parentNode.replaceChild( iframe, embedVideo ); |
| 87 |
} |
| 88 |
} ); |
| 89 |
|
| 90 |
observeEmbeds.observe( document, { |
| 91 |
childList: true, |
| 92 |
subtree: true, |
| 93 |
} ); |
| 94 |
} |
| 95 |
} ); |
| 96 |
|