UNPKG

@newswire/frames

Version:

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

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