Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save peterwmwong/1138772 to your computer and use it in GitHub Desktop.
Save peterwmwong/1138772 to your computer and use it in GitHub Desktop.
livejs 4 patch to track dynamically added JS & CSS
@@ -34,12 +34,12 @@
var Live = {
// performs a cycle per interval
heartbeat: function () {
if (document.body) {
- // make sure all resources are loaded on first activation
- if (!loaded) Live.loadresources();
+ // Look for resources to track
+ Live.loadresources();
Live.checkForChanges();
}
setTimeout(Live.heartbeat, interval);
},
@@ -59,11 +59,11 @@
uris = [];
// track local js urls
for (var i = 0; i < scripts.length; i++) {
var script = scripts[i], src = script.getAttribute("src");
- if (src && isLocal(src))
+ if (src && isLocal(src) && resources[src] == null)
uris.push(src);
if (src && src.match(/\blive.js#/)) {
for (var type in active)
active[type] = src.match("[#,|]" + type) != null
if (src.match("notify"))
@@ -74,11 +74,11 @@
if (active.html) uris.push(document.location.href);
// track local css urls
for (var i = 0; i < links.length && active.css; i++) {
var link = links[i], rel = link.getAttribute("rel"), href = link.getAttribute("href", 2);
- if (href && rel && rel.match(new RegExp("stylesheet", "i")) && isLocal(href)) {
+ if (href && rel && rel.match(new RegExp("stylesheet", "i")) && isLocal(href) && resources[src] == null) {
uris.push(href);
currentLinkElements[href] = link;
}
}
@@ -89,17 +89,19 @@
resources[url] = info;
});
}
// add rule for morphing between old and new css files
- var head = document.getElementsByTagName("head")[0],
- style = document.createElement("style"),
- rule = "transition: all .3s ease-out;"
- css = [".livejs-loading * { ", rule, " -webkit-", rule, "-moz-", rule, "-o-", rule, "}"].join('');
- style.setAttribute("type", "text/css");
- head.appendChild(style);
- style.styleSheet ? style.styleSheet.cssText = css : style.appendChild(document.createTextNode(css));
+ if(!loaded){
+ var head = document.getElementsByTagName("head")[0],
+ style = document.createElement("style"),
+ rule = "transition: all .3s ease-out;"
+ css = [".livejs-loading * { ", rule, " -webkit-", rule, "-moz-", rule, "-o-", rule, "}"].join('');
+ style.setAttribute("type", "text/css");
+ head.appendChild(style);
+ style.styleSheet ? style.styleSheet.cssText = css : style.appendChild(document.createTextNode(css));
+ }
// yep
loaded = true;
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment