catfield

modal-video-on-click

Aug 24th, 2023
1,267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2. var scriptUrl = 'https://www.youtube.com/s/player/21812a9c/www-widgetapi.vflset/www-widgetapi.js';
  3.  
  4. try {
  5.     var ttPolicy = window.trustedTypes.createPolicy("youtube-widget-api", {
  6.         createScriptURL: function (x) {
  7.             return x;
  8.         }
  9.     });
  10.     scriptUrl = ttPolicy.createScriptURL(scriptUrl);
  11. } catch (e) {}
  12.  
  13. var YT = YT || {
  14.     loading: 0,
  15.     loaded: 0
  16. };
  17.  
  18. var YTConfig = window.YTConfig || {
  19.     host: "https://www.youtube.com"
  20. };
  21.  
  22. let player;
  23. let apiReady = false;
  24. let modalOpened = false; // Added flag to track modal state
  25.  
  26. // Load the YouTube API
  27. function loadYouTubeAPI() {
  28.     if (!YT.loading) {
  29.         YT.loading = 1;
  30.         var a = document.createElement("script");
  31.         a.type = "text/javascript";
  32.         a.id = "www-widgetapi-script";
  33.         a.src = scriptUrl;
  34.         a.async = true;
  35.  
  36.         var c = document.currentScript;
  37.         if (c) {
  38.             var n = c.nonce || c.getAttribute("nonce");
  39.             if (n) {
  40.                 a.setAttribute("nonce", n);
  41.             }
  42.         }
  43.  
  44.         var b = document.getElementsByTagName("script")[0];
  45.         b.parentNode.insertBefore(a, b);
  46.     }
  47. }
  48.  
  49. function onYouTubeIframeAPIReady() {
  50.     apiReady = true;
  51.  
  52.     if (modalOpened) {
  53.         ModalFlexAndPlayVideo();
  54.     }
  55. }
  56.  
  57. function resetPlayerAndModal() {
  58.     if (player) {
  59.         player.pauseVideo();
  60.         player.destroy();
  61.     }
  62.     var modalSection = document.querySelector('.fixed-modal-section');
  63.     modalSection.style.display = 'none';
  64.     modalOpened = false;
  65. }
  66.  
  67. function ModalFlexAndPlayVideo() {
  68.     if (apiReady) {
  69.         player = new YT.Player('muteVideoYoutube', {
  70.             events: {
  71.                 'onReady': function (event) {
  72.                     event.target.playVideo();
  73.                     var modalSection = document.querySelector('.fixed-modal-section');
  74.                     modalSection.style.display = 'flex';
  75.                     modalOpened = true;
  76.                     console.log("Video playing");
  77.                 }
  78.             }
  79.         });
  80.     } else {
  81.         console.log("API is not ready yet.");
  82.         loadYouTubeAPI();
  83.         console.log("Loading YouTube API...");
  84.     }
  85. }
  86.  
  87. document.querySelector('.modal-close-btn').addEventListener('click', resetPlayerAndModal);
  88. document.querySelector('.daria-testimonial-photo-block').addEventListener('click', ModalFlexAndPlayVideo);
  89. </script>
Advertisement
Add Comment
Please, Sign In to add comment