Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add this code to your site. Open your console. Click everything interactive. Copy CSS classes into UnCSS ignore list.
(function(){
'use strict';
// Feature Detection
var MutationObserver = (function () {
var prefixes = ['WebKit', 'Moz', 'O', 'Ms', ''];
for(var i=0; i < prefixes.length; i++) {
if(prefixes[i] + 'MutationObserver' in window) {
return window[prefixes[i] + 'MutationObserver'];
}
}
return false;
}());
if (!MutationObserver) {return;}
// List all used CSS Classes
var elements = document.getElementsByTagName('*');
var unique = function (list, x) {
if (x !== '' && list.indexOf(x) === -1) {
list.push(x);
}
return list;
};
var trim = function (x) { return x.trim(); };
var pageClasses = [].reduce.call(elements, function (acc, e) {
return e.className.split(' ').map(trim).reduce(unique, acc);
}, []);
//Watch for new CSS classes
var dynamicClasses = [];
function observerChanges(mutationRecord) {
mutationRecord.forEach(function(mutation) {
if (mutation.attributeName) {
var classes = mutation.target.className.split(' ');
for (var i = 0; i < classes.length; i++) {
if (pageClasses.indexOf(classes[i]) === -1 && dynamicClasses.indexOf(classes[i]) === -1) {
dynamicClasses.push(classes[i]);
var classString = '';
dynamicClasses.forEach(function(cls){
classString+= '".'+cls+'",';
});
console.log('UnCSS ignore classes: ');
console.log(classString.substr(0, classString.length -1));
}
}
}
});
}
var observer = new MutationObserver(observerChanges);
observer.observe(document.body, {
attributes: true,
subtree: true,
childList: true,
characterData: true
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.