Last active
July 18, 2017 16:22
-
-
Save SethClydesdale/855b8a39dbd040a3f22101bb7def6912 to your computer and use it in GitHub Desktop.
Listens for changes in a stylesheet
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
// 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