forked from davidjbradshaw/iframe-resizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iframeResizer.min.js
9 lines (8 loc) · 12.4 KB
/
iframeResizer.min.js
1
2
3
4
5
6
7
8
9
/*! iFrame Resizer (iframeSizer.min.js ) - v3.6.10 - 2021-11-18
* Desc: Force cross domain iframes to size to content.
* Requires: iframeResizer.contentWindow.min.js to be loaded into the target frame.
* Copyright: (c) 2021 David J. Bradshaw - [email protected]
* License: MIT
*/
!function(l){"use strict";if("undefined"!=typeof window){var e,u=0,f=!1,n=!1,p="message".length,b="[iFrameSizer]",y=b.length,v=null,r=window.requestAnimationFrame,m={max:1,scroll:1,bodyScroll:1,documentElementScroll:1},k={},i=null,g={autoResize:!0,bodyBackground:null,bodyMargin:null,bodyMarginV1:8,bodyPadding:null,checkOrigin:!0,inPageLinks:!1,enablePublicMethods:!0,heightCalculationMethod:"bodyOffset",id:"iFrameResizer",interval:32,log:!1,maxHeight:1/0,maxWidth:1/0,minHeight:0,minWidth:0,resizeFrom:"parent",scrolling:!1,sizeHeight:!0,sizeWidth:!1,warningTimeout:5e3,tolerance:0,widthCalculationMethod:"scroll",closedCallback:function(){},initCallback:function(){},messageCallback:function(){R("MessageCallback function not defined")},resizedCallback:function(){},scrollCallback:function(){return!0}},F={};window.jQuery&&((e=window.jQuery).fn?e.fn.iFrameResize||(e.fn.iFrameResize=function(i){return this.filter("iframe").each(function(e,n){d(n,i)}).end()}):z("","Unable to bind to jQuery, it is not fully loaded.")),"function"==typeof define&&define.amd?define([],B):"object"==typeof module&&"object"==typeof module.exports&&(module.exports=B()),window.iFrameResize=window.iFrameResize||B()}function w(){return window.MutationObserver||window.WebKitMutationObserver||window.MozMutationObserver}function I(e,n,i){"addEventListener"in window?e.addEventListener(n,i,!1):"attachEvent"in window&&e.attachEvent("on"+n,i)}function M(e,n,i){"removeEventListener"in window?e.removeEventListener(n,i,!1):"detachEvent"in window&&e.detachEvent("on"+n,i)}function o(e){return b+"["+(i="Host page: "+(n=e),window.top!==window.self&&(i=window.parentIFrame&&window.parentIFrame.getId?window.parentIFrame.getId()+": "+n:"Nested host page: "+n),i)+"]";var n,i}function t(e){return k[e]?k[e].log:f}function x(e,n){a("log",e,n,t(e))}function z(e,n){a("info",e,n,t(e))}function R(e,n){a("warn",e,n,!0)}function a(e,n,i,t){!0===t&&"object"==typeof window.console&&console[e](o(n),i)}function s(n){function a(){e("Height"),e("Width"),S(function(){P(w),T(h),f("resizedCallback",w)},w,"init")}function e(e){var n=Number(k[h]["max"+e]),i=Number(k[h]["min"+e]),t=e.toLowerCase(),o=Number(w[t]);x(h,"Checking "+t+" is in range "+i+"-"+n),o<i&&(o=i,x(h,"Set "+t+" to min value")),n<o&&(o=n,x(h,"Set "+t+" to max value")),w[t]=""+o}function s(e){return g.substr(g.indexOf(":")+p+e)}function d(i,t){var e,n,o;e=function(){var e,n;H("Send Page Info","pageInfo:"+(e=document.body.getBoundingClientRect(),n=w.iframe.getBoundingClientRect(),JSON.stringify({iframeHeight:n.height,iframeWidth:n.width,clientHeight:Math.max(document.documentElement.clientHeight,window.innerHeight||0),clientWidth:Math.max(document.documentElement.clientWidth,window.innerWidth||0),offsetTop:parseInt(n.top-e.top,10),offsetLeft:parseInt(n.left-e.left,10),scrollTop:window.pageYOffset,scrollLeft:window.pageXOffset})),i,t)},n=32,F[o=t]||(F[o]=setTimeout(function(){F[o]=null,e()},n))}function c(e){var n=e.getBoundingClientRect();return O(h),{x:Math.floor(Number(n.left)+Number(v.x)),y:Math.floor(Number(n.top)+Number(v.y))}}function l(e){var n=e?c(w.iframe):{x:0,y:0},i={x:Number(w.width)+n.x,y:Number(w.height)+n.y};x(h,"Reposition requested from iFrame (offset x:"+n.x+" y:"+n.y+")"),window.top!==window.self?window.parentIFrame?window.parentIFrame["scrollTo"+(e?"Offset":"")](i.x,i.y):R(h,"Unable to scroll to requested position, window.parentIFrame not found"):(v=i,u(),x(h,"--"))}function u(){!1!==f("scrollCallback",v)?T(h):N()}function f(e,n){return E(h,e,n)}var i,t,o,r,m,g=n.data,w={},h=null;"[iFrameResizerChild]Ready"===g?function(){for(var e in k)H("iFrame requested init",A(e),document.getElementById(e),e)}():b===(""+g).substr(0,y)&&g.substr(y).split(":")[0]in k?(m=g.substr(y).split(":"),w={iframe:k[m[0]]&&k[m[0]].iframe,id:m[0],height:m[1],width:m[2],type:m[3]},h=w.id,k[h]&&(k[h].loaded=!0),(r=w.type in{true:1,false:1,undefined:1})&&x(h,"Ignoring init message from meta parent page"),!r&&(o=!0,k[t=h]||(o=!1,R(w.type+" No settings for "+t+". Message was: "+g)),o)&&(x(h,"Received: "+g),i=!0,null===w.iframe&&(R(h,"IFrame ("+w.id+") not found"),i=!1),i&&function(){var e,i=n.origin,t=k[h]&&k[h].checkOrigin;if(t&&""+i!="null"&&!(t.constructor===Array?function(){var e=0,n=!1;for(x(h,"Checking connection is from allowed list of origins: "+t);e<t.length;e++)if(t[e]===i){n=!0;break}return n}():(e=k[h]&&k[h].remoteHost,x(h,"Checking connection is from: "+e),i===e)))throw new Error("Unexpected message received from: "+i+" for "+w.iframe.id+". Message was: "+n.data+". This error can be disabled by setting the checkOrigin: false option or by providing of array of trusted domains.");return!0}()&&function(){switch(k[h]&&k[h].firstRun&&k[h]&&(k[h].firstRun=!1),w.type){case"close":k[h].closeRequestCallback?E(h,"closeRequestCallback",k[h].iframe):C(w.iframe);break;case"message":r=s(6),x(h,"MessageCallback passed: {iframe: "+w.iframe.id+", message: "+r+"}"),f("messageCallback",{iframe:w.iframe,message:JSON.parse(r)}),x(h,"--");break;case"scrollTo":l(!1);break;case"scrollToOffset":l(!0);break;case"pageInfo":d(k[h]&&k[h].iframe,h),function(){function e(n,i){function t(){k[r]?d(k[r].iframe,r):o()}["scroll","resize"].forEach(function(e){x(r,n+e+" listener for sendPageInfo"),i(window,e,t)})}function o(){e("Remove ",M)}var r=h;e("Add ",I),k[r]&&(k[r].stopPageInfo=o)}();break;case"pageInfoStop":k[h]&&k[h].stopPageInfo&&(k[h].stopPageInfo(),delete k[h].stopPageInfo);break;case"inPageLink":e=s(9),i=e.split("#")[1]||"",t=decodeURIComponent(i),(o=document.getElementById(t)||document.getElementsByName(t)[0])?(n=c(o),x(h,"Moving to in page link (#"+i+") at x: "+n.x+" y: "+n.y),v={x:n.x,y:n.y},u(),x(h,"--")):window.top!==window.self?window.parentIFrame?window.parentIFrame.moveToAnchor(i):x(h,"In page link #"+i+" not found and window.parentIFrame not found"):x(h,"In page link #"+i+" not found");break;case"reset":W(w);break;case"init":a(),f("initCallback",w.iframe);break;default:a()}var e,n,i,t,o,r}())):z(h,"Ignored: "+g)}function E(e,n,i){var t=null,o=null;if(k[e]){if("function"!=typeof(t=k[e][n]))throw new TypeError(n+" on iFrame["+e+"] is not a function");o=t(i)}return o}function h(e){var n=e.id;delete k[n]}function C(e){var n=e.id;x(n,"Removing iFrame: "+n);try{e.parentNode&&e.parentNode.removeChild(e)}catch(e){}E(n,"closedCallback",n),x(n,"--"),h(e)}function O(e){null===v&&x(e,"Get page position: "+(v={x:window.pageXOffset!==l?window.pageXOffset:document.documentElement.scrollLeft,y:window.pageYOffset!==l?window.pageYOffset:document.documentElement.scrollTop}).x+","+v.y)}function T(e){null!==v&&(window.scrollTo(v.x,v.y),x(e,"Set page position: "+v.x+","+v.y),N())}function N(){v=null}function W(e){x(e.id,"Size reset requested by "+("init"===e.type?"host page":"iFrame")),O(e.id),S(function(){P(e),H("reset","reset",e.iframe,e.id)},e,"reset")}function P(i){function t(e){n||"0"!==i[e]||(n=!0,x(o,"Hidden iFrame detected, creating visibility listener"),function(){function n(){function e(n){function e(e){return"0px"===(k[n]&&k[n].iframe.style[e])}k[n]&&null!==k[n].iframe.offsetParent&&(e("height")||e("width"))&&H("Visibility change","resize",k[n].iframe,n)}for(var n in k)e(n)}function e(e){x("window","Mutation observed: "+e[0].target+" "+e[0].type),c(n,16)}var i=w();i&&(t=document.querySelector("body"),o={attributes:!0,attributeOldValue:!1,characterData:!0,characterDataOldValue:!1,childList:!0,subtree:!0},new i(e).observe(t,o));var t,o}())}function e(e){var n;n=e,i.id?(i.iframe.style[n]=i[n]+"px",x(i.id,"IFrame ("+o+") "+n+" set to "+i[n]+"px")):x("undefined","messageData id not set"),t(e)}var o=i.iframe.id;k[o]&&(k[o].sizeHeight&&e("height"),k[o].sizeWidth&&e("width"))}function S(e,n,i){i!==n.type&&r?(x(n.id,"Requesting animation frame"),r(e)):e()}function H(e,n,i,t,o){var r,a=!1;t=t||i.id,k[t]&&(i&&"contentWindow"in i&&null!==i.contentWindow?(r=k[t]&&k[t].targetOrigin,x(t,"["+e+"] Sending msg to iframe["+t+"] ("+n+") targetOrigin: "+r),i.contentWindow.postMessage(b+n,r)):R(t,"["+e+"] IFrame("+t+") not found"),o&&k[t]&&k[t].warningTimeout&&(k[t].msgTimeout=setTimeout(function(){!k[t]||k[t].loaded||a||(a=!0,R(t,"IFrame has not responded within "+k[t].warningTimeout/1e3+" seconds. Check iFrameResizer.contentWindow.js has been loaded in iFrame. This message can be ignored if everything is working, or you can set the warningTimeout option to a higher value or zero to suppress this warning."))},k[t].warningTimeout)))}function A(e){return e+":"+k[e].bodyMarginV1+":"+k[e].sizeWidth+":"+k[e].log+":"+k[e].interval+":"+k[e].enablePublicMethods+":"+k[e].autoResize+":"+k[e].bodyMargin+":"+k[e].heightCalculationMethod+":"+k[e].bodyBackground+":"+k[e].bodyPadding+":"+k[e].tolerance+":"+k[e].inPageLinks+":"+k[e].resizeFrom+":"+k[e].widthCalculationMethod}function d(i,e){var n,t,o,r,a,s,d,c=(n=i.id,""===n&&(i.id=(t=e&&e.id||g.id+u++,null!==document.getElementById(t)&&(t+=u++),n=t),f=(e||{}).log,x(n,"Added missing iframe ID: "+n+" ("+i.src+")")),n);c in k&&"iFrameResizer"in i?R(c,"Ignored iFrame, already setup."):(s=(s=e)||{},k[c]={firstRun:!0,iframe:i,remoteHost:i.src&&i.src.split("/").slice(0,3).join("/")},function(e){if("object"!=typeof e)throw new TypeError("Options is not an object")}(s),function(e){for(var n in g)g.hasOwnProperty(n)&&(k[c][n]=e.hasOwnProperty(n)?e[n]:g[n])}(s),k[c]&&(k[c].targetOrigin=!0===k[c].checkOrigin?""===(d=k[c].remoteHost)||"file://"===d?"*":d:"*"),function(){switch(x(c,"IFrame scrolling "+(k[c]&&k[c].scrolling?"enabled":"disabled")+" for "+c),i.style.overflow=!1===(k[c]&&k[c].scrolling)?"hidden":"auto",k[c]&&k[c].scrolling){case"omit":break;case!0:i.scrolling="yes";break;case!1:i.scrolling="no";break;default:i.scrolling=k[c]?k[c].scrolling:"no"}}(),function(){function e(e){1/0!==k[c][e]&&0!==k[c][e]&&(i.style[e]=k[c][e]+"px",x(c,"Set "+e+" = "+k[c][e]+"px"))}function n(e){if(k[c]["min"+e]>k[c]["max"+e])throw new Error("Value for min"+e+" can not be greater than max"+e)}n("Height"),n("Width"),e("maxHeight"),e("minHeight"),e("maxWidth"),e("minWidth")}(),"number"!=typeof(k[c]&&k[c].bodyMargin)&&"0"!==(k[c]&&k[c].bodyMargin)||(k[c].bodyMarginV1=k[c].bodyMargin,k[c].bodyMargin=k[c].bodyMargin+"px"),o=A(c),(a=w())&&(r=a,i.parentNode&&new r(function(e){e.forEach(function(e){Array.prototype.slice.call(e.removedNodes).forEach(function(e){e===i&&C(i)})})}).observe(i.parentNode,{childList:!0})),I(i,"load",function(){var e,n;H("iFrame.onload",o,i,l,!0),e=k[c]&&k[c].firstRun,n=k[c]&&k[c].heightCalculationMethod in m,!e&&n&&W({iframe:i,height:0,width:0,type:"init"})}),H("init",o,i,l,!0),Function.prototype.bind&&k[c]&&(k[c].iframe.iFrameResizer={close:C.bind(null,k[c].iframe),removeListeners:h.bind(null,k[c].iframe),resize:H.bind(null,"Window resize","resize",k[c].iframe),moveToAnchor:function(e){H("Move to anchor","moveToAnchor:"+e,k[c].iframe,c)},sendMessage:function(e){H("Send Message","message:"+(e=JSON.stringify(e)),k[c].iframe,c)}}))}function c(e,n){null===i&&(i=setTimeout(function(){i=null,e()},n))}function L(e){x("window","Trigger event: "+e),c(function(){q("Window "+e,"resize")},16)}function j(){"hidden"!==document.visibilityState&&(x("document","Trigger event: Visiblity change"),c(function(){q("Tab Visable","resize")},16))}function q(e,n){for(var i in k)k[t=i]&&"parent"===k[t].resizeFrom&&k[t].autoResize&&!k[t].firstRun&&H(e,n,document.getElementById(i),i);var t}function B(){function t(e,n){n&&(!function(){if(!n.tagName)throw new TypeError("Object is not a valid DOM element");if("IFRAME"!==n.tagName.toUpperCase())throw new TypeError("Expected <IFRAME> tag, found <"+n.tagName+">")}(),d(n,e),o.push(n))}var o;return function(){var e,n=["moz","webkit","o","ms"];for(e=0;e<n.length&&!r;e+=1)r=window[n[e]+"RequestAnimationFrame"];r||x("setup","RequestAnimationFrame not supported")}(),I(window,"message",s),I(window,"resize",function(){L("resize")}),I(document,"visibilitychange",j),I(document,"-webkit-visibilitychange",j),I(window,"focusin",function(){L("focus")}),I(window,"focus",function(){L("focus")}),function(e,n){var i;switch(o=[],(i=e)&&i.enablePublicMethods&&R("enablePublicMethods option has been removed, public methods are now always available in the iFrame"),typeof n){case"undefined":case"string":Array.prototype.forEach.call(document.querySelectorAll(n||"iframe"),t.bind(l,e));break;case"object":t(e,n);break;default:throw new TypeError("Unexpected data type ("+typeof n+")")}return o}}}();
//# sourceMappingURL=iframeResizer.map