Created
July 13, 2018 15:21
-
-
Save DevWouter/45a2587be0ae3992fcc1bf5b0c477297 to your computer and use it in GitHub Desktop.
A bit of code to check if a certain css rule is being use by the site
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
rules = []; | |
website = "https://www.yoursite.com/"; | |
Array.from(document.styleSheets) | |
.filter(x => (x.href || "").startsWith(website)) | |
.forEach(sheet => { | |
Array.from(sheet.rules) | |
.filter(x => x.conditionText === undefined) | |
.filter(x => x.selectorText !== undefined) | |
.filter(x => !x.selectorText.includes(":")) | |
.filter(x => ($$(x.selectorText) || []).length == 0) | |
.forEach(x => { | |
let rulePath = x.selectorText.split(" ").filter(x => x.length); | |
let rule = ""; | |
for(let i = 0; i < rulePath.length; ++i){ | |
if(i < rulePath.length){ | |
rule += " " + rulePath[i]; | |
rule = rule.trim(); | |
} | |
if(rules.indexOf(rule) !== -1){ | |
// Already listed. | |
return; | |
} | |
} | |
rules.push(rule); | |
}); | |
}); | |
// Remove the rules that are too | |
rules = rules.sort(); | |
rules = rules.filter(x => { | |
let parts = x.split(" "); | |
let rule = ""; | |
for(let i = 0; i < (parts.length - 1); ++i){ | |
rule = (rule + " " + parts[i]).trim(); | |
if(rules.indexOf(rule) !== -1){ | |
// Already listed. | |
return false; | |
} | |
} | |
return true | |
}) | |
// Check if a rule is used on another page | |
usedOnSomePage = []; | |
rules_1.filter(a => rules_2.indexOf(a) === -1).forEach(a => usedOnSomePage.push(a)); | |
rules_2.filter(a => rules_1.indexOf(a) === -1).forEach(a => usedOnSomePage.push(a)); | |
b = [...rules_1, ...rules_2]; | |
b = b.filter(a => usedOnSomePage.indexOf(a) === -1); | |
usedOnSomePage = usedOnSomePage.filter(onlyUnique); | |
b = b.filter(onlyUnique) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment