-
-
Save karbassi/4035565 to your computer and use it in GitHub Desktop.
Update the visible-but-unfocused tab's page, styles, and scripts without ever pressing refresh.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (document) { | |
ArrayForEach = Array.prototype.forEach || function (callback, scope) { for (var i = 0, l = this.length; i < l; ++i) callback.call(scope, this[i], i, this); }; | |
function updateElements() { | |
var skip = document.hidden || document.mozHidden || document.msHidden || document.oHidden || document.webkitHidden || !document.hasFocus || document.hasFocus(); | |
ArrayForEach.call(document.querySelectorAll("html,link[rel],script[src]"), function (element) { | |
if (skip) { | |
return; | |
} | |
var xhr = new XMLHttpRequest; | |
xhr.onreadystatechange = function () { | |
if (xhr.readyState <= 1) { | |
return; | |
} | |
xhr.lastModified = xhr.lastModified || +new Date(xhr.getResponseHeader("Last-Modified")); | |
element.dataset.lastModified = element.dataset.lastModified || xhr.lastModified; | |
if (xhr.lastModified <= element.dataset.lastModified) { | |
xhr.abort(); | |
return; | |
} | |
if (xhr.readyState == 4 && xhr.status == 200) { | |
element.dataset.lastModified = xhr.lastModified; | |
if (element.nodeName == "HTML") { | |
document.open(); | |
document.write(xhr.responseText); | |
document.close(); | |
return skip = true; | |
} | |
for (var clone = document.createElement(element.nodeName), i = 0, attr; attr = element.attributes[i]; ++i) { | |
clone.setAttribute(attr.name, attr.value); | |
} | |
element.parentNode.replaceChild(clone, element); | |
element = clone; | |
} | |
}; | |
xhr.open('GET', element.href || element.src || window.location.pathname, true); | |
xhr.send(null); | |
}); | |
} | |
setInterval(updateElements, 200); updateElements(); | |
})(document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment