Created
March 15, 2018 06:02
-
-
Save samthor/561f479d8401bcf271ecd6ab8bde9a17 to your computer and use it in GitHub Desktop.
removes all print-specific styles from stylesheets
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
Array.from(document.styleSheets).forEach((ss) => { | |
const toDelete = []; | |
const toReplace = new Map(); | |
for (let i = 0; i < ss.rules.length; ++i) { | |
const rule = ss.rules[i]; | |
if (!(rule instanceof CSSMediaRule)) { | |
continue; | |
} | |
if (rule.conditionText.includes('print')) { | |
toReplace.set(i, '* {}'); | |
continue; | |
} | |
if (rule.conditionText.includes('screen')) { | |
let t = rule.cssText; | |
const start = t.indexOf('{'); | |
let prefix = t.substr(0, start); | |
prefix = prefix.replace(/(only |)screen/g, '(min-width: 1px)'); | |
t = prefix + t.substr(start); | |
toReplace.set(i, t); | |
continue; | |
} | |
} | |
console.info('replacing', toReplace.size, 'rules to remove print-specific styles'); | |
toReplace.forEach((text, index) => { | |
ss.deleteRule(index); | |
ss.insertRule(text, index); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment