Skip to content

Instantly share code, notes, and snippets.

@SethClydesdale
Last active July 18, 2017 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SethClydesdale/855b8a39dbd040a3f22101bb7def6912 to your computer and use it in GitHub Desktop.
Save SethClydesdale/855b8a39dbd040a3f22101bb7def6912 to your computer and use it in GitHub Desktop.
Listens for changes in a stylesheet
// listen for changes in a stylesheet every 100ms (can change to fit needs)
function onStyleChange (stylesheet, callback) {
// gets the entire CSSText of a stylesheet
var getCSSText = function (stylesheet) {
stylesheet = stylesheet.sheet.cssRules;
var i, rules = '';
for (i in stylesheet) {
rules += stylesheet[i].cssText || '';
}
return rules;
},
origin = getCSSText(stylesheet), // source stylesheet
clone = origin; // clone for comparison
// listener
setInterval(function () {
origin = getCSSText(stylesheet);
if (clone != origin) {
callback();
clone = origin; // update clone with new CSS
}
}, 100);
};
/*
// EXAMPLE USAGE
onStyleChange(document.querySelector('link[rel="stylesheet"]'), function () {
console.log('CSS Changed');
});
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment