| 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 |
let url = settings.youtube_url.replace( 'watch?v=', 'embed/' ) + '?'; |
| 48 |
|
| 49 |
if ( settings.controls ) { |
| 50 |
url += 'controls=1&'; |
| 51 |
} |
| 52 |
|
| 53 |
if ( settings.autoplay ) { |
| 54 |
url += 'autoplay=1&playsinline=1&'; |
| 55 |
} |
| 56 |
|
| 57 |
// build iframe to replace embed video div |
| 58 |
iframe.src = url; |
| 59 |
iframe.allowFullscreen = 1; |
| 60 |
iframe.class = 'elementor-video'; |
| 61 |
iframe.style.maxHeight = '332px'; |
| 62 |
iframe.style.maxWidth = '100%'; |
| 63 |
iframe.style.height = 360; |
| 64 |
iframe.style.width = 640; |
| 65 |
|
| 66 |
// replace the video element with the iframe |
| 67 |
embedVideo.parentNode.replaceChild( iframe, embedVideo ); |
| 68 |
} |
| 69 |
} ); |
| 70 |
|
| 71 |
observeEmbeds.observe( document, { |
| 72 |
childList: true, |
| 73 |
subtree: true, |
| 74 |
} ); |
| 75 |
} |
| 76 |
} ); |
| 77 |
|