Last active
August 29, 2015 14:07
-
-
Save llaughlin/201f778e72f340110466 to your computer and use it in GitHub Desktop.
Chrome Snippet - Constantly reload page css
This file contains hidden or 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
// css-constant-reload.js | |
// adapted from https://github.com/bgrins/devtools-snippets | |
// Removes then reloads all the CSS files in the current page every 5 seconds | |
(function () { | |
function insertAfter(newElement, targetElement) { | |
var parent = targetElement.parentNode; | |
if (parent.lastChild == targetElement) { | |
parent.appendChild(newElement); | |
} else { | |
parent.insertBefore(newElement, targetElement.nextSibling); | |
} | |
} | |
function reloadStyleSheet(stylesheet) { | |
var element = stylesheet.ownerNode; | |
var clone = element.cloneNode(false); | |
clone.href = addRandomToUrl(clone.href); | |
clone.addEventListener("load", function() { | |
if (element.parentNode) { | |
element.parentNode.removeChild(element); | |
} | |
}); | |
insertAfter(clone, element); | |
} | |
function addRandomToUrl(input) { | |
// prevent CSS caching | |
var hasRnd = /([?&])_=[^&]*/, | |
hasQueryString = /\?/, | |
hasHash = /(.+)#(.+)/, | |
hash = null, | |
rnd = Math.random(); | |
var hashMatches = input.match(hasHash); | |
if (hashMatches) { | |
input = hashMatches[1]; | |
hash = hashMatches[2]; | |
} | |
url = hasRnd.test(input) ? | |
input.replace(hasRnd, "$1_=" + rnd) : | |
input + (hasQueryString.test(input) ? "&" : "?") + "_=" + rnd; | |
if (hash) url += '#' + hash; | |
return url; | |
} | |
window.setInterval(function(){ | |
[].forEach.call(document.styleSheets, function(styleSheet) { | |
if (!styleSheet.href) return; | |
console.log('reload ' + styleSheet.href); | |
reloadStyleSheet(styleSheet); | |
}); | |
}, 5000); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment