Skip to content

Instantly share code, notes, and snippets.

@Pamblam
Created June 12, 2020 18:42
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 Pamblam/99def16e272a2157fa544e5542300c93 to your computer and use it in GitHub Desktop.
Save Pamblam/99def16e272a2157fa544e5542300c93 to your computer and use it in GitHub Desktop.
detect all media queries in use..
(async function(d){
console.clear();
const styles = [];
const stylesheets = d.querySelectorAll("link[rel='stylesheet'][href]");
// get all stylesheet urls
const stylesheet_urls = [...stylesheets].map(s=>s.href);
// fetch the contents of the stylesheets
const ss_promises = stylesheet_urls.map(href=>{
return fetch(href).then(s=>s.text()).then(s=>{
return {
source: href,
styles: s
};
});
});
const ss_promises_resolved = await Promise.all(ss_promises);
styles.push(...ss_promises_resolved);
// get all the inline <style> tag contents
document.querySelectorAll('style').forEach(ele=>styles.push({
source: "inline",
styles: ele.innerText
}));
styles.forEach(stylesheet=>{
var media_queries = Array.from(stylesheet.styles.matchAll(/@media[^{]*/g), m => m[0].trim());
if(!media_queries.length) return;
console.groupCollapsed(stylesheet.source);
[... new Set(media_queries)].forEach(mq=>console.log(mq));
console.groupEnd();
});
})(document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment