UNPKG

@newswire/frames

Version:

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

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