Skip to content

Instantly share code, notes, and snippets.

@CGamesPlay
Created October 4, 2023 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CGamesPlay/b7a2b8f53fc4753edd4213c16781a510 to your computer and use it in GitHub Desktop.
Save CGamesPlay/b7a2b8f53fc4753edd4213c16781a510 to your computer and use it in GitHub Desktop.
Convert em media queries to px
// ==UserScript==
// @name px-media-queries
// @description Convert all media queries to use px instead of em.
// @match *://*/*
// @exclude https://github.com/*
// @exclude https://developer.mozilla.org/*
// @exclude http://localhost:*/*
// ==/UserScript==
// Breaks Dark mode on Github for some reason.
(function() {
'use strict';
function updateSheet(sheet) {
try {
for (const rule of sheet.cssRules) {
if (rule instanceof CSSMediaRule) {
rule.media.mediaText = rule.media.mediaText.replace(/(\d*(\.\d*)?)em/ig, (match, size) => {
return `${parseFloat(size) * 16}px`;
});
}
}
} catch(_) {
}
}
Object.values(document.styleSheets).forEach(sheet => {
updateSheet(sheet);
});
const observer = new MutationObserver((mutationsList) => {
for (let mutation of mutationsList) {
if (mutation.type === 'childList') {
const added = mutation.addedNodes;
added.forEach((node) => {
if (node.tagName === 'LINK' && node.rel === 'stylesheet') {
updateSheet(node.sheet);
} else if (node.tagName === 'STYLE') {
updateSheet(node.sheet);
}
});
}
}
});
observer.observe(document.documentElement, {
childList: true,
subtree: true
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment