Created
October 4, 2023 22:58
-
-
Save CGamesPlay/b7a2b8f53fc4753edd4213c16781a510 to your computer and use it in GitHub Desktop.
Convert em media queries to px
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
// ==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