1 line
3.1 KiB
JavaScript
1 line
3.1 KiB
JavaScript
(function(){var r={Etag:1,"Last-Modified":1,"Content-Length":1,"Content-Type":1},n={},i={},s={},o={},c=1e3,a=!1,t={html:1,css:1,js:1},e={heartbeat:function(){document.body&&(a||e.loadresources(),e.checkForChanges()),setTimeout(e.heartbeat,c)},loadresources:function(){function h(e){var t=document.location,n=new RegExp("^\\.|^/(?!/)|^[\\w]((?!://).)*$|"+t.protocol+"//"+t.host);return e.match(n)}for(var i,f,g,b,p=document.getElementsByTagName("script"),m=document.getElementsByTagName("link"),r=[],o=0;o<p.length;o++)if(f=p[o],i=f.getAttribute("src"),i&&h(i)&&r.push(i),i&&i.match(/\blive.js#/)){for(g in t)t[g]=i.match("[#,|]"+g)!=null;i.match("notify")&&alert("Live.js is loaded.")}t.js||(r=[]),t.html&&r.push(document.location.href);for(o=0;o<m.length&&t.css;o++){var u=m[o],v=u.getAttribute("rel"),l=u.getAttribute("href",2);l&&v&&v.match(new RegExp("stylesheet","i"))&&h(l)&&(r.push(l),s[l]=u)}for(o=0;o<r.length;o++)b=r[o],e.getHead(b,function(e,t){n[e]=t});var j=document.getElementsByTagName("head")[0],c=document.createElement("style"),d="transition: all .3s ease-out;";css=[".livejs-loading * { ",d," -webkit-",d,"-moz-",d,"-o-",d,"}"].join(""),c.setAttribute("type","text/css"),j.appendChild(c),c.styleSheet?c.styleSheet.cssText=css:c.appendChild(document.createTextNode(css)),a=!0},checkForChanges:function(){for(var t in n){if(i[t])continue;e.getHead(t,function(t,s){var o,i=n[t],a=!1;n[t]=s;for(o in i){var c=i[o],r=s[o],l=s["Content-Type"];switch(o.toLowerCase()){case"etag":if(!r)break;default:a=c!=r;break}if(a){e.refreshResource(t,l);break}}})}},refreshResource:function(t,n){switch(n.toLowerCase()){case"text/css":var a=s[t],r=document.body.parentNode,c=a.parentNode,l=a.nextSibling,i=document.createElement("link");r.className=r.className.replace(/\s*livejs-loading/gi,"")+" livejs-loading",i.setAttribute("type","text/css"),i.setAttribute("rel","stylesheet"),i.setAttribute("href",t+"?now="+new Date*1),l?c.insertBefore(i,l):c.appendChild(i),s[t]=i,o[t]=a,e.removeoldLinkElements();break;case"text/html":if(t!=document.location.href)return;case"text/javascript":case"application/javascript":case"application/x-javascript":document.location.reload()}},removeoldLinkElements:function(){t=0;for(n in o){try{var t,n,i=s[n],a=o[n],r=document.body.parentNode,c=i.sheet||i.styleSheet,l=c.rules||c.cssRules;l.length>=0&&(a.parentNode.removeChild(a),delete o[n],setTimeout(function(){r.className=r.className.replace(/\s*livejs-loading/gi,"")},100))}catch{t++}t&&setTimeout(e.removeoldLinkElements,50)}},getHead:function(e,t){i[e]=!0;var n=window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject("Microsoft.XmlHttp");n.open("HEAD",e,!0),n.onreadystatechange=function(){if(delete i[e],n.readyState==4&&n.status!=304){n.getAllResponseHeaders();var s,o,a={};for(o in r)s=n.getResponseHeader(o),o.toLowerCase()=="etag"&&s&&(s=s.replace(/^W\//,"")),o.toLowerCase()=="content-type"&&s&&(s=s.replace(/^(.*?);.*?$/i,"$1")),a[o]=s;t(e,a)}},n.send()}};document.location.protocol!="file:"?(window.liveJsLoaded||e.heartbeat(),window.liveJsLoaded=!0):window.console&&console.log("Live.js doesn't support the file protocol. It needs http.")})() |