@@ -26,11 +26,12 @@ public static async Task<string> GenerateAsync(IReadOnlyCollection<string> urls)
26
26
await page . SetViewportSizeAsync ( viewport . Width , viewport . Height ) ;
27
27
28
28
var usedCss = await page . EvaluateAsync < string [ ] > (
29
- """
29
+ """
30
30
async () => {
31
31
const styleSheets = Array.from(document.styleSheets);
32
32
const usedRules = new Set();
33
33
const processedUrls = new Set();
34
+ const mediaQueryRules = new Set();
34
35
35
36
const viewportHeight = window.innerHeight;
36
37
const elements = document.querySelectorAll('*');
@@ -39,40 +40,6 @@ public static async Task<string> GenerateAsync(IReadOnlyCollection<string> urls)
39
40
return rect.top < viewportHeight;
40
41
});
41
42
42
- async function fetchExternalStylesheet(url) {
43
- if (processedUrls.has(url)) return;
44
- processedUrls.add(url);
45
-
46
- try {
47
- const response = await fetch(url);
48
- const text = await response.text();
49
- const blob = new Blob([text], { type: 'text/css' });
50
- const styleSheet = new CSSStyleSheet();
51
- await styleSheet.replace(text);
52
- return styleSheet;
53
- } catch (e) {
54
- console.error('Failed to fetch:', url, e);
55
- return null;
56
- }
57
- }
58
-
59
- async function processStyleSheet(sheet) {
60
- try {
61
- if (sheet.href) {
62
- const externalSheet = await fetchExternalStylesheet(sheet.href);
63
- if (externalSheet) {
64
- Array.from(externalSheet.cssRules).forEach(processRule);
65
- }
66
- }
67
-
68
- Array.from(sheet.cssRules).forEach(processRule);
69
- } catch (e) {
70
- if (sheet.href) {
71
- console.error('CORS issue with:', sheet.href);
72
- }
73
- }
74
- }
75
-
76
43
function processRule(rule) {
77
44
switch (rule.type) {
78
45
case CSSRule.STYLE_RULE:
@@ -85,9 +52,8 @@ function processRule(rule) {
85
52
});
86
53
break;
87
54
case CSSRule.MEDIA_RULE:
88
- if (window.matchMedia(rule.conditionText).matches) {
89
- Array.from(rule.cssRules).forEach(processRule);
90
- }
55
+ // Always include the complete media query block
56
+ mediaQueryRules.add(rule.cssText);
91
57
break;
92
58
case CSSRule.IMPORT_RULE:
93
59
processStyleSheet(rule.styleSheet);
@@ -99,11 +65,45 @@ function processRule(rule) {
99
65
}
100
66
}
101
67
68
+ async function processStyleSheet(sheet) {
69
+ try {
70
+ if (sheet.href) {
71
+ const externalSheet = await fetchExternalStylesheet(sheet.href);
72
+ if (externalSheet) {
73
+ Array.from(externalSheet.cssRules).forEach(processRule);
74
+ }
75
+ }
76
+ Array.from(sheet.cssRules).forEach(processRule);
77
+ } catch (e) {
78
+ if (sheet.href) {
79
+ console.error('CORS issue with:', sheet.href);
80
+ }
81
+ }
82
+ }
83
+
84
+ async function fetchExternalStylesheet(url) {
85
+ if (processedUrls.has(url)) return;
86
+ processedUrls.add(url);
87
+
88
+ try {
89
+ const response = await fetch(url);
90
+ const text = await response.text();
91
+ const blob = new Blob([text], { type: 'text/css' });
92
+ const styleSheet = new CSSStyleSheet();
93
+ await styleSheet.replace(text);
94
+ return styleSheet;
95
+ } catch (e) {
96
+ console.error('Failed to fetch:', url, e);
97
+ return null;
98
+ }
99
+ }
100
+
102
101
for (const sheet of styleSheets) {
103
102
await processStyleSheet(sheet);
104
103
}
105
104
106
- return Array.from(usedRules);
105
+ // Combine regular rules and media queries
106
+ return [...Array.from(usedRules), ...Array.from(mediaQueryRules)];
107
107
}
108
108
""" ) ;
109
109
0 commit comments