;(function(){varrenderedFrames=[];functionget(url,callback){varreq=newXMLHttpRequest();req.onload=function(){callback(this.responseText);};req.open('get',url,true);req.send();returnreq;}functiontoArray(value){returnArray.prototype.slice.call(value);}functionautoResize(node){varelem=typeofnode==='string'?document.getElementById(node):node;varnewheight=Math.ceil(elem.contentWindow.document.documentElement.scrollHeight);elem.height=(newheight+1)+"px";}functioninit(node){varnodes=toArray(document.querySelectorAll('[data-role="frame"]'));nodes.forEach(function(node){createFrame(node);});}functioncreateFrame(node){varframe=document.createElement('iframe');varstyles=toArray(document.head.querySelectorAll('style, link[type="text/css"]')).reverse();// Can add global variables and function in here ---node.appendChild(frame);frame.contentWindow.Frames=Frames;get(node.dataset.source,function(data){varsrc=frame.contentWindow.document;src.open();src.write(data);src.close();styles.forEach(function(styleElement){varfirstChild=src.head.firstChild;src.head.insertBefore(styleElement.cloneNode(true),firstChild);});autoResize(frame);renderedFrames.push(frame);});}functionresizeAll(){renderedFrames.forEach(function(node){autoResize(node);});}functionFrames(){/* empty for the time being! */}functionextend(key,obj){if(Frames.hasOwnProperty(key)){return;}else{Frames[key]=obj;}}Frames.init=init;Frames.autoResize=autoResize;Frames.resizeAll=resizeAll;Frames.extend=extend;Frames.createFrame=createFrame;window.addEventListener('resize',resizeAll);window.Frames=Frames;}());
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.