Skip to content

Instantly share code, notes, and snippets.

@samthor
Created March 15, 2018 06:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samthor/561f479d8401bcf271ecd6ab8bde9a17 to your computer and use it in GitHub Desktop.
Save samthor/561f479d8401bcf271ecd6ab8bde9a17 to your computer and use it in GitHub Desktop.
removes all print-specific styles from stylesheets
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