UNPKG

@newswire/frames

Version:

A minimalistic take on responsive iframes in the spirit of Pym.js.

3 lines (2 loc) 2.12 kB
const t=/xPYMx/;function n(n){function e(e){if(e.source!==n.contentWindow)return;const{data:i}=e;if("amp"===i.sentinel&&"embed-size"===i.type)n.setAttribute("height",i.height);else if("string"==typeof i&&"pym"===i.slice(0,3)){const[,,e,o]=i.split(t);"height"===e&&n.setAttribute("height",o)}}return window.addEventListener("message",e,!1),n.contentWindow&&n.contentWindow.postMessage({sentinel:"amp",type:"frames-init"},"*"),function(){window.removeEventListener("message",e,!1)}}function e(t,{attributes:e,src:i}={}){const o=document.createElement("iframe"),a=n(o);if(i&&o.setAttribute("src",i),o.setAttribute("width","100%"),o.setAttribute("scrolling","no"),o.setAttribute("marginheight","0"),o.setAttribute("frameborder","0"),e)for(let t in e)o.setAttribute(t,e[t]);return t.appendChild(o),{remove(){a(),t.removeChild(o)}}}const i="data-frame-attribute-".length;function o(t){const n={},e=t.attributes,o=e.length;for(let t=0;t<o;t++){const o=e[t].name;"data-frame-attribute-"===o.slice(0,i)&&(n[o.slice(i)]=e[t].value)}return n}function a(){const t=document.querySelectorAll("[data-frame-src]:not([data-frame-auto-initialized])");for(let n=0;n<t.length;n++){const i=t[n],a=i.getAttribute("data-frame-src"),r=o(i);i.setAttribute("data-frame-auto-initialized",""),e(i,{attributes:r,src:a})}}function r(t=function(){return document.documentElement.offsetHeight}()){window.parent.postMessage({sentinel:"amp",type:"embed-size",height:t},"*")}function s(){window.addEventListener("load",function t(){r(),window.removeEventListener("load",t,!1)},!1)}function c(){window.addEventListener("resize",()=>r(),!1)}function f(){window.addEventListener("message",function t(n){const{data:e}=n;"amp"===e.sentinel&&"frames-init"===e.type&&(window.removeEventListener("message",t,!1),r())},!1)}function u(t=300){setInterval(r,t)}function d(){r(),s(),c(),f()}function m(t){d(),u(t)}export{e as Framer,a as autoInitFrames,d as initFrame,m as initFrameAndPoll,n as observeIframe,r as sendFrameHeight,f as sendHeightOnFramerInit,s as sendHeightOnLoad,u as sendHeightOnPoll,c as sendHeightOnResize}; //# sourceMappingURL=frames.modern.js.map