-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresponsive-youtube.min.js
3 lines (2 loc) · 3.15 KB
/
responsive-youtube.min.js
1
2
3
/* Responsive-youtube.js 0.2.1 | Copyright (c) 2020 Guilherme Nascimento ([email protected]) | Released under the MIT license */
!function(e){var t,n,r=[],o=0,i=!1,a=!0,u={},c={},f="undefined"!=typeof window?window:{},s=f.document||{},h=f.Element&&f.Element.prototype,d="[data-ry-video]:not(iframe)",l=",with,height,ignore,video,cover,",y=f.MutationObserver||f.WebKitMutationObserver,v={start:function(e){"object"==typeof e?E(e,c):c={};if(!a)return;if(a=!1,f.YT&&f.YT.Player)return N();f.onYouTubeIframeAPIReady=N;var t=s.createElement("script");t.ansyc=!0,t.src="https://www.youtube.com/iframe_api";var n=s.getElementsByTagName("script")[0];n.parentNode.insertBefore(t,n)},destroy:function(){a=!0,c={};for(var e=r.length-1;e>=0;e--)r[e].destroy();r=[]},supported:!!y,on:function(e,t){O(e,t)},off:function(e,t){O(e,t,!0)}};function g(e){if(!0!==e)t&&clearTimeout(t),t=setTimeout(g,300,!0);else if(r.length)for(var n=0,o=r.length;n<o;n++)m(r[n].getIframe())}function m(e){if(e&&s.body.contains(e)&&!0!==S(e,"ignore")){var t=S(e,"width")||e.clientWidth,n=S(e,"height")||e.clientHeight;e.setAttribute("data-ry-width",t),e.setAttribute("data-ry-height",n),e.width="100%",e.height=t!=n?e.clientWidth/(t/n):e.clientWidth}}function p(e){if(e)return g();clearTimeout(n),n=setTimeout(p,5e3,!0)}function b(e,t,n){n&&"object"==typeof n||(n={}),E(c,n);var o=new YT.Player(e,{videoId:t,playerVars:n,events:{onReady:function(t){m(s.getElementById(e)),M("ready",t,o)},onStateChange:function(e){M("state",e,o)},onPlaybackQualityChange:function(e){M("quality",e,o)},onPlaybackRateChange:function(e){M("rate",e,o)},onError:function(e){M("error",e,o)},onApiChange:function(e){M("api",e,o)}}});r.push(o),M("create",o),p()}function w(e){for(var t=e.attributes,n={},r=t.length-1;r>=0;r--){var o=t[r].nodeName;0===o.indexOf("data-ry-")&&(o=o.substr(8),-1===l.indexOf(","+o+",")&&(n[o]=S(e,o)))}return n}function S(e,t){var n,r=e.getAttribute("data-ry-"+t);if("true"===r||"false"===r)return"true"===r;if(!isNaN(r))return parseFloat(r);var o=r[0],i=r[r.length-1];if("["===o&&"]"===i||"{"===o&&"}"===i)try{n=JSON.parse(r)}catch(e){}return n||r}function T(t){if(!a){for(var n,r=t.addedNodes,o=[],i=[],u=r.length-1;u>=0;u--)1===(n=r[u]).nodeType&&(n.matches(d)&&n.matches?o.push(n):i.push(n));n=e,A(o),function(e){for(var t=0,n=e.length;t<n;t++)A(e[t].querySelectorAll(d))}(i)}}function A(e){for(var t=0,n=e.length;t<n;t++){var r=e[t];r.id||(r.id="responsive-youtube-"+ ++o),b(r.id,S(r,"video"),w(r))}}function E(e,t){for(var n in e)-1===["object","function"].indexOf(typeof e[n])&&(t[n]=e[n])}function N(){if(A(s.querySelectorAll(d)),!i){if(i=!0,f.addEventListener("resize",g),!y)return M("error");new y(function(e){e.forEach(T)}).observe(s.body,{childList:!0}),M("done")}}function M(e,t,n){if(u[e])for(var r=u[e],o=0;o<r.length;o++)r[o](t,n)}function O(e,t,n){"function"==typeof t&&(u[e]||(u[e]=[]),n?u[e]=u[e].filter(function(e){return e!==t}):u[e].push(t))}h&&!h.matches&&(h.matches=h.matchesSelector||h.mozMatchesSelector||h.msMatchesSelector||h.oMatchesSelector||h.webkitMatchesSelector||function(e){for(var t=(this.document||this.ownerDocument).querySelectorAll(e),n=t.length;--n>=0&&t[n]!==this;);return n>-1}),f.ResponsiveYoutube=v}();