-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResource Analyzer v1.js
More file actions
255 lines (223 loc) · 7.24 KB
/
Resource Analyzer v1.js
File metadata and controls
255 lines (223 loc) · 7.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Comprehensive Resource Analyzer
// Run this in the console of the page you want to analyze
// Make sure you're logged-in before you run the analyzer
(async function analyzePageResources() {
console.clear();
console.log("🔍 COMPREHENSIVE PAGE RESOURCE ANALYSIS");
console.log("=".repeat(60));
// 1. Performance API resources
const perfResources = performance.getEntriesByType("resource");
// 2. Better categorization
const categories = {
css: [],
js: [],
fonts: [],
fontCss: [], // Google Fonts CSS links
images: [],
svg: [],
api: [],
other: [],
};
perfResources.forEach((r) => {
const url = r.name.toLowerCase();
if (url.includes("fonts.googleapis.com")) {
categories.fontCss.push(r.name);
} else if (
url.includes("fonts.gstatic.com") ||
url.match(/\.(woff2?|ttf|otf|eot)/)
) {
categories.fonts.push(r.name);
} else if (url.includes(".css")) {
categories.css.push(r.name);
} else if (
url.includes(".js") &&
!url.includes("gtm.js") &&
!url.includes("analytics")
) {
categories.js.push(r.name);
} else if (url.match(/\.(png|jpg|jpeg|gif|webp|ico|avif)(\?|$)/)) {
categories.images.push(r.name);
} else if (url.includes(".svg")) {
categories.svg.push(r.name);
} else if (url.includes("/api/")) {
categories.api.push(r.name);
} else if (!url.includes("gtm.js") && !url.includes("googletagmanager")) {
categories.other.push(r.name);
}
});
// 3. Check document.fonts API for loaded fonts
const loadedFonts = [];
if (document.fonts) {
document.fonts.forEach((font) => {
loadedFonts.push({
family: font.family,
weight: font.weight,
style: font.style,
status: font.status,
});
});
}
// 4. Find fonts from stylesheets
const fontFacesInCSS = [];
try {
for (const sheet of document.styleSheets) {
try {
for (const rule of sheet.cssRules || []) {
if (rule instanceof CSSFontFaceRule) {
fontFacesInCSS.push({
family: rule.style.fontFamily,
src: rule.style.src,
});
}
}
} catch (e) {
/* CORS blocked stylesheet */
}
}
} catch (e) {}
// 5. Find inline SVGs
const inlineSvgs = document.querySelectorAll("svg");
const svgInfo = {
count: inlineSvgs.length,
unique: new Set(),
};
inlineSvgs.forEach((svg) => {
// Get a simplified identifier
const id =
svg.getAttribute("data-slot") ||
svg.getAttribute("class")?.split(" ")[0] ||
svg.innerHTML.substring(0, 50);
svgInfo.unique.add(id);
});
// 6. Find all images in DOM (including background images)
const domImages = new Set();
// Regular img tags
document.querySelectorAll("img").forEach((img) => {
if (img.src) domImages.add(img.src);
if (img.srcset) domImages.add(`srcset: ${img.srcset}`);
});
// Background images in computed styles
document.querySelectorAll("*").forEach((el) => {
const bg = getComputedStyle(el).backgroundImage;
if (bg && bg !== "none" && bg.includes("url(")) {
domImages.add(bg);
}
});
// 7. Check link tags for more resources
const linkTags = {
stylesheets: [],
preconnect: [],
icons: [],
other: [],
};
document.querySelectorAll("link").forEach((link) => {
const href = link.href;
const rel = link.rel;
if (rel === "stylesheet") linkTags.stylesheets.push(href);
else if (rel === "preconnect") linkTags.preconnect.push(href);
else if (rel === "icon" || rel === "apple-touch-icon")
linkTags.icons.push(href);
else if (href) linkTags.other.push({ rel, href });
});
// 8. Parse Google Fonts CSS to find actual font files
let googleFontFiles = [];
for (const fontCssUrl of categories.fontCss) {
try {
const response = await fetch(fontCssUrl);
const css = await response.text();
const fontUrls =
css.match(/url\((https:\/\/fonts\.gstatic\.com[^)]+)\)/g) || [];
googleFontFiles = fontUrls.map((u) =>
u.replace("url(", "").replace(")", ""),
);
} catch (e) {
console.log("Could not fetch Google Fonts CSS:", e);
}
}
// PRINT RESULTS
console.log("\n" + "=".repeat(60));
console.log("📊 NETWORK RESOURCES (from Performance API)");
console.log("=".repeat(60));
console.log(`\n🎨 CSS FILES (${categories.css.length}):`);
categories.css.forEach((u) => console.log(` ✓ ${u}`));
console.log(`\n🔤 GOOGLE FONTS CSS (${categories.fontCss.length}):`);
categories.fontCss.forEach((u) => console.log(` ✓ ${u}`));
console.log(`\n📜 JS FILES (${categories.js.length}):`);
categories.js.forEach((u) => console.log(` ✓ ${u}`));
console.log(`\n🖼️ IMAGES (${categories.images.length}):`);
categories.images.forEach((u) => console.log(` ✓ ${u}`));
console.log(`\n🎯 SVG FILES (${categories.svg.length}):`);
categories.svg.forEach((u) => console.log(` ✓ ${u}`));
console.log(`\n🔗 API CALLS (${categories.api.length}):`);
categories.api.forEach((u) => console.log(` ✓ ${u}`));
console.log("\n" + "=".repeat(60));
console.log("🔤 FONT ANALYSIS");
console.log("=".repeat(60));
console.log(
`\n📥 Font files loaded via network (${categories.fonts.length}):`,
);
categories.fonts.forEach((u) => console.log(` ✓ ${u}`));
console.log(
`\n📥 Font files from Google Fonts CSS (${googleFontFiles.length}):`,
);
googleFontFiles.forEach((u) => console.log(` ✓ ${u}`));
console.log(
`\n🖥️ Fonts registered in document.fonts (${loadedFonts.length}):`,
);
const uniqueFamilies = [...new Set(loadedFonts.map((f) => f.family))];
uniqueFamilies.forEach((family) => {
const variants = loadedFonts.filter((f) => f.family === family);
console.log(` ✓ ${family} (${variants.length} variants)`);
});
console.log("\n" + "=".repeat(60));
console.log("🎨 DOM ANALYSIS");
console.log("=".repeat(60));
console.log(
`\n🔷 Inline SVGs: ${svgInfo.count} total (${svgInfo.unique.size} unique patterns)`,
);
console.log(`\n🖼️ Images in DOM (${domImages.size}):`);
domImages.forEach((u) => console.log(` ✓ ${u}`));
console.log(`\n🔗 Link tags - Icons (${linkTags.icons.length}):`);
linkTags.icons.forEach((u) => console.log(` ✓ ${u}`));
console.log("\n" + "=".repeat(60));
console.log("📋 SUMMARY - WHAT NEEDS TO BE DOWNLOADED");
console.log("=".repeat(60));
const totalAssets = {
"CSS files": categories.css.length,
"Google Fonts CSS": categories.fontCss.length,
"Font files (woff2)": googleFontFiles.length,
"JS files": categories.js.length,
"External images": categories.images.length,
"External SVGs": categories.svg.length,
"Inline SVGs": svgInfo.count,
"Favicons/Icons": linkTags.icons.length,
};
console.log("\n");
Object.entries(totalAssets).forEach(([key, val]) => {
const status = val > 0 ? "✓" : "⚠️";
console.log(` ${status} ${key}: ${val}`);
});
// What extension captured vs what exists
console.log("\n" + "=".repeat(60));
console.log("🔴 EXTENSION GAP ANALYSIS");
console.log("=".repeat(60));
console.log("\nExtension captured:");
console.log(" - 2 CSS files");
console.log(" - 1 JS file");
console.log(" - 2 images");
console.log(" - 0 font files");
console.log("\nMissing:");
console.log(` - ${googleFontFiles.length} font files (woff2)`);
console.log(
` - ${svgInfo.count} inline SVGs (need to be preserved in HTML)`,
);
// Return data for export
return {
categories,
googleFontFiles,
loadedFonts,
inlineSvgCount: svgInfo.count,
domImages: [...domImages],
linkTags,
};
})();