sugikit 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +29 -0
- package/README.md +44 -2
- package/dist/api/browser.d.ts +5 -0
- package/dist/api/browser.js +5 -0
- package/dist/api/rules.d.ts +12 -0
- package/dist/api/rules.js +14 -0
- package/dist/auth.d.ts +156 -0
- package/dist/auth.js +547 -0
- package/dist/browser.d.ts +25 -0
- package/dist/browser.js +120 -0
- package/dist/check.d.ts +21 -0
- package/dist/check.js +89 -0
- package/dist/convert-probe.d.ts +21 -0
- package/dist/convert-probe.js +647 -0
- package/dist/fetch.d.ts +6 -0
- package/dist/fetch.js +22 -0
- package/dist/framework.d.ts +2 -0
- package/dist/framework.js +17 -0
- package/dist/gather.d.ts +48 -0
- package/dist/gather.js +211 -0
- package/dist/html.d.ts +17 -0
- package/dist/html.js +43 -0
- package/dist/http.d.ts +36 -0
- package/dist/http.js +57 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +331 -0
- package/dist/links.d.ts +8 -0
- package/dist/links.js +69 -0
- package/dist/polish-run.d.ts +115 -0
- package/dist/polish-run.js +357 -0
- package/dist/probe.d.ts +14 -0
- package/dist/probe.js +457 -0
- package/dist/psi-scores.d.ts +4 -0
- package/dist/psi-scores.js +7 -0
- package/dist/psi.d.ts +56 -0
- package/dist/psi.js +192 -0
- package/dist/release.d.ts +1 -0
- package/dist/release.js +5 -0
- package/dist/render.d.ts +28 -0
- package/dist/render.js +270 -0
- package/dist/report.d.ts +227 -0
- package/dist/report.js +69 -0
- package/dist/rules/convert-render.d.ts +100 -0
- package/dist/rules/convert-render.js +279 -0
- package/dist/rules/convert.d.ts +15 -0
- package/dist/rules/convert.js +322 -0
- package/dist/rules/fixes.d.ts +16 -0
- package/dist/rules/fixes.js +536 -0
- package/dist/rules/geo.d.ts +15 -0
- package/dist/rules/geo.js +112 -0
- package/dist/rules/polish.d.ts +59 -0
- package/dist/rules/polish.js +223 -0
- package/dist/rules/robots.d.ts +14 -0
- package/dist/rules/robots.js +69 -0
- package/dist/rules/seo.d.ts +31 -0
- package/dist/rules/seo.js +208 -0
- package/dist/rules/types.d.ts +55 -0
- package/dist/rules/types.js +78 -0
- package/dist/score.d.ts +6 -0
- package/dist/score.js +23 -0
- package/dist/skills.d.ts +9 -0
- package/dist/skills.js +22 -0
- package/package.json +63 -3
- package/skills/INSTALL_PROMPT.md +9 -0
- package/skills/sugikit/SKILL.md +147 -0
- package/skills/sugikit/references/convert.md +127 -0
- package/skills/sugikit/references/geo.md +97 -0
- package/skills/sugikit/references/next.md +254 -0
- package/skills/sugikit/references/pagespeed.md +94 -0
- package/skills/sugikit/references/polish.md +123 -0
- package/skills/sugikit/references/seo.md +150 -0
- package/skills/sugikit/references/tanstack-start.md +246 -0
package/dist/probe.js
ADDED
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
// These functions run inside the page through page.evaluate, so each one must be self-contained.
|
|
2
|
+
// Every scroll here is instant: a page with CSS scroll-behavior: smooth would otherwise animate each
|
|
3
|
+
// programmatic scroll, and a screenshot or measurement taken mid-animation is wrong. The page is throwaway,
|
|
4
|
+
// so the inline scroll-behavior override is never removed.
|
|
5
|
+
// Scrolls to the top instantly and waits (at most 2 seconds) until the page is there. Returns scrollY.
|
|
6
|
+
export async function scrollToTop() {
|
|
7
|
+
document.documentElement.style.setProperty("scroll-behavior", "auto", "important");
|
|
8
|
+
document.body?.style.setProperty("scroll-behavior", "auto", "important");
|
|
9
|
+
window.scrollTo({ top: 0, left: 0, behavior: "instant" });
|
|
10
|
+
const deadline = Date.now() + 2000;
|
|
11
|
+
while (window.scrollY !== 0 && Date.now() < deadline) {
|
|
12
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
13
|
+
window.scrollTo({ top: 0, left: 0, behavior: "instant" });
|
|
14
|
+
}
|
|
15
|
+
return window.scrollY;
|
|
16
|
+
}
|
|
17
|
+
export async function scrollThrough() {
|
|
18
|
+
const pause = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
19
|
+
document.documentElement.style.setProperty("scroll-behavior", "auto", "important");
|
|
20
|
+
document.body?.style.setProperty("scroll-behavior", "auto", "important");
|
|
21
|
+
const step = Math.max(200, window.innerHeight * 0.8);
|
|
22
|
+
for (let i = 0; i < 40; i += 1) {
|
|
23
|
+
const before = window.scrollY;
|
|
24
|
+
window.scrollBy({ top: step, left: 0, behavior: "instant" });
|
|
25
|
+
await pause(60);
|
|
26
|
+
if (window.scrollY === before)
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
window.scrollTo({ top: 0, left: 0, behavior: "instant" });
|
|
30
|
+
await pause(200);
|
|
31
|
+
const pending = Array.from(document.images)
|
|
32
|
+
.filter((img) => !img.complete)
|
|
33
|
+
.map((img) => new Promise((resolve) => img.addEventListener("load", resolve, { once: true })));
|
|
34
|
+
await Promise.race([Promise.all(pending), pause(3000)]);
|
|
35
|
+
for (const animation of document.getAnimations()) {
|
|
36
|
+
try {
|
|
37
|
+
animation.finish();
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
animation.pause();
|
|
41
|
+
animation.currentTime = 0;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export function probePage() {
|
|
46
|
+
const LIMIT = 1000;
|
|
47
|
+
const root = document.documentElement;
|
|
48
|
+
const body = document.body;
|
|
49
|
+
const describe = (el) => {
|
|
50
|
+
const parts = [];
|
|
51
|
+
for (let node = el; node && node !== root && parts.length < 3; node = node.parentElement) {
|
|
52
|
+
if (node.id) {
|
|
53
|
+
parts.unshift(`#${CSS.escape(node.id)}`);
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
const classes = Array.from(node.classList)
|
|
57
|
+
.filter((c) => c.length <= 30)
|
|
58
|
+
.slice(0, 2)
|
|
59
|
+
.map((c) => `.${CSS.escape(c)}`)
|
|
60
|
+
.join("");
|
|
61
|
+
parts.unshift(`${node.tagName.toLowerCase()}${classes}`);
|
|
62
|
+
if (node === body)
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
return parts.join(" > ");
|
|
66
|
+
};
|
|
67
|
+
const visible = (el) => {
|
|
68
|
+
const rect = el.getBoundingClientRect();
|
|
69
|
+
if (rect.width <= 1 || rect.height <= 1)
|
|
70
|
+
return false;
|
|
71
|
+
const style = getComputedStyle(el);
|
|
72
|
+
return style.visibility !== "hidden" && style.visibility !== "collapse" && style.opacity !== "0";
|
|
73
|
+
};
|
|
74
|
+
const px = (value) => Math.round(parseFloat(value) * 100) / 100;
|
|
75
|
+
const textLength = (el) => (el.textContent ?? "").replace(/\s+/g, " ").trim().length;
|
|
76
|
+
const push = (list, el, detail) => {
|
|
77
|
+
if (list.length < LIMIT)
|
|
78
|
+
list.push(detail ? { selector: describe(el), detail } : { selector: describe(el) });
|
|
79
|
+
};
|
|
80
|
+
const viewport = root.clientWidth;
|
|
81
|
+
const scrollWidth = Math.max(root.scrollWidth, body?.scrollWidth ?? 0);
|
|
82
|
+
// overflow-x: hidden on html or body alone still lets a phone pan the layout viewport, which then grows past
|
|
83
|
+
// clientWidth. Hidden on both, or a desktop viewport with hidden or clip, does not scroll.
|
|
84
|
+
const rootOverflow = getComputedStyle(root).overflowX;
|
|
85
|
+
const viewportOverflow = rootOverflow !== "visible" || !body ? rootOverflow : getComputedStyle(body).overflowX;
|
|
86
|
+
const scrolls = scrollWidth > viewport + 1 && (window.innerWidth > viewport + 1 || (viewportOverflow !== "hidden" && viewportOverflow !== "clip"));
|
|
87
|
+
const overflow = [];
|
|
88
|
+
if (scrolls) {
|
|
89
|
+
const clipped = (el) => {
|
|
90
|
+
for (let p = el.parentElement; p && p !== body && p !== root; p = p.parentElement) {
|
|
91
|
+
if (getComputedStyle(p).overflowX !== "visible")
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
return false;
|
|
95
|
+
};
|
|
96
|
+
const wide = new Set();
|
|
97
|
+
for (const el of Array.from(body?.querySelectorAll("*") ?? [])) {
|
|
98
|
+
const rect = el.getBoundingClientRect();
|
|
99
|
+
if (rect.width > 0 && rect.right > viewport + 1 && getComputedStyle(el).position !== "fixed")
|
|
100
|
+
wide.add(el);
|
|
101
|
+
}
|
|
102
|
+
for (const el of wide) {
|
|
103
|
+
if (el.parentElement && wide.has(el.parentElement))
|
|
104
|
+
continue;
|
|
105
|
+
if (clipped(el))
|
|
106
|
+
continue;
|
|
107
|
+
push(overflow, el, `right edge at ${Math.round(el.getBoundingClientRect().right)} px`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
// polish/tap-targets follows WCAG 2.5.8 Target Size (Minimum): a target under 24x24 px passes when it is an
|
|
111
|
+
// inline link in a line of text, when its size is the browser default of an unstyled native control, or when a
|
|
112
|
+
// 24 px circle centered on it touches no other target and no other undersized target's circle.
|
|
113
|
+
const TARGETS = 'a[href], button, input:not([type="hidden"]), select, summary, [role="button"], [role="link"], [role="tab"], [role="menuitem"]';
|
|
114
|
+
const MIN = 24;
|
|
115
|
+
const targets = [];
|
|
116
|
+
for (const el of Array.from(document.querySelectorAll(TARGETS))) {
|
|
117
|
+
if (!visible(el) || getComputedStyle(el).pointerEvents === "none")
|
|
118
|
+
continue;
|
|
119
|
+
// A control inside another control (a button in a link) is one target: the outer one.
|
|
120
|
+
if (el.parentElement?.closest(TARGETS))
|
|
121
|
+
continue;
|
|
122
|
+
const rect = el.getBoundingClientRect();
|
|
123
|
+
targets.push({ el, rect, cx: rect.left + rect.width / 2, cy: rect.top + rect.height / 2, small: rect.width < MIN || rect.height < MIN });
|
|
124
|
+
}
|
|
125
|
+
const isTargetText = (node) => Boolean(node.parentElement?.closest(TARGETS));
|
|
126
|
+
const textBlock = (el) => {
|
|
127
|
+
for (let p = el.parentElement; p; p = p.parentElement) {
|
|
128
|
+
const display = getComputedStyle(p).display;
|
|
129
|
+
if (!display.startsWith("inline") && display !== "contents")
|
|
130
|
+
return p;
|
|
131
|
+
}
|
|
132
|
+
return body ?? root;
|
|
133
|
+
};
|
|
134
|
+
// An inline link with other (non-link) text on the same line of its text block.
|
|
135
|
+
const inlineInText = (target) => {
|
|
136
|
+
if (target.el.tagName !== "A" || !getComputedStyle(target.el).display.startsWith("inline"))
|
|
137
|
+
return false;
|
|
138
|
+
const { rect } = target;
|
|
139
|
+
const walker = document.createTreeWalker(textBlock(target.el), NodeFilter.SHOW_TEXT);
|
|
140
|
+
let seen = 0;
|
|
141
|
+
for (let node = walker.nextNode(); node && seen < 400; node = walker.nextNode()) {
|
|
142
|
+
seen += 1;
|
|
143
|
+
if (!(node.textContent ?? "").trim() || isTargetText(node))
|
|
144
|
+
continue;
|
|
145
|
+
const range = document.createRange();
|
|
146
|
+
range.selectNodeContents(node);
|
|
147
|
+
for (const line of Array.from(range.getClientRects())) {
|
|
148
|
+
if (line.width < 1)
|
|
149
|
+
continue;
|
|
150
|
+
const overlap = Math.min(line.bottom, rect.bottom) - Math.max(line.top, rect.top);
|
|
151
|
+
if (overlap > Math.min(line.height, rect.height) / 2)
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return false;
|
|
156
|
+
};
|
|
157
|
+
// Measures the same control, unstyled, in a blank frame: equal size means the browser set it.
|
|
158
|
+
// The frame's own about:blank document is used as is: doc.write throws under a Trusted Types CSP.
|
|
159
|
+
let frame;
|
|
160
|
+
let blank;
|
|
161
|
+
const blankDoc = () => {
|
|
162
|
+
if (blank === undefined) {
|
|
163
|
+
blank = null;
|
|
164
|
+
try {
|
|
165
|
+
frame = document.createElement("iframe");
|
|
166
|
+
frame.setAttribute("aria-hidden", "true");
|
|
167
|
+
frame.style.cssText = "position:absolute;left:-10000px;top:0;width:400px;height:400px;border:0;visibility:hidden";
|
|
168
|
+
(body ?? root).appendChild(frame);
|
|
169
|
+
const doc = frame.contentDocument;
|
|
170
|
+
if (doc) {
|
|
171
|
+
const html = doc.documentElement ?? doc.appendChild(doc.createElement("html"));
|
|
172
|
+
if (!doc.body)
|
|
173
|
+
html.appendChild(doc.createElement("body"));
|
|
174
|
+
blank = doc;
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
frame.remove();
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
frame?.remove();
|
|
182
|
+
blank = null;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return blank ?? undefined;
|
|
186
|
+
};
|
|
187
|
+
const userAgentSized = (target) => {
|
|
188
|
+
const { el, rect } = target;
|
|
189
|
+
if (!(el instanceof HTMLInputElement || el instanceof HTMLSelectElement || el instanceof HTMLButtonElement))
|
|
190
|
+
return false;
|
|
191
|
+
if (getComputedStyle(el).appearance === "none")
|
|
192
|
+
return false;
|
|
193
|
+
if (!(el instanceof HTMLSelectElement) && el.children.length > 0)
|
|
194
|
+
return false;
|
|
195
|
+
const doc = blankDoc();
|
|
196
|
+
if (!doc?.body)
|
|
197
|
+
return false;
|
|
198
|
+
const copy = doc.createElement(el.tagName.toLowerCase());
|
|
199
|
+
if (el instanceof HTMLInputElement) {
|
|
200
|
+
copy.setAttribute("type", el.type);
|
|
201
|
+
if (el.value)
|
|
202
|
+
copy.setAttribute("value", el.value);
|
|
203
|
+
if (el.placeholder)
|
|
204
|
+
copy.setAttribute("placeholder", el.placeholder);
|
|
205
|
+
}
|
|
206
|
+
else if (el instanceof HTMLSelectElement) {
|
|
207
|
+
for (const option of Array.from(el.options))
|
|
208
|
+
copy.add(new Option(option.text));
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
copy.textContent = el.textContent;
|
|
212
|
+
}
|
|
213
|
+
doc.body.appendChild(copy);
|
|
214
|
+
const size = copy.getBoundingClientRect();
|
|
215
|
+
copy.remove();
|
|
216
|
+
return Math.abs(size.width - rect.width) <= 1 && Math.abs(size.height - rect.height) <= 1;
|
|
217
|
+
};
|
|
218
|
+
const circleHitsRect = (cx, cy, rect) => {
|
|
219
|
+
const dx = cx - Math.max(rect.left, Math.min(cx, rect.right));
|
|
220
|
+
const dy = cy - Math.max(rect.top, Math.min(cy, rect.bottom));
|
|
221
|
+
return dx * dx + dy * dy < (MIN / 2) * (MIN / 2) - 0.5;
|
|
222
|
+
};
|
|
223
|
+
const crowded = (target) => targets.some((other) => {
|
|
224
|
+
if (other === target || other.el.contains(target.el) || target.el.contains(other.el))
|
|
225
|
+
return false;
|
|
226
|
+
if (Math.abs(other.cy - target.cy) > other.rect.height / 2 + MIN || Math.abs(other.cx - target.cx) > other.rect.width / 2 + MIN) {
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
if (circleHitsRect(target.cx, target.cy, other.rect))
|
|
230
|
+
return true;
|
|
231
|
+
return other.small && Math.hypot(other.cx - target.cx, other.cy - target.cy) < MIN - 0.5;
|
|
232
|
+
});
|
|
233
|
+
const smallTargets = [];
|
|
234
|
+
const bigLabel = (el) => el instanceof HTMLInputElement &&
|
|
235
|
+
Array.from(el.labels ?? []).some((label) => {
|
|
236
|
+
const rect = label.getBoundingClientRect();
|
|
237
|
+
return rect.width >= MIN && rect.height >= MIN;
|
|
238
|
+
});
|
|
239
|
+
try {
|
|
240
|
+
for (const target of targets) {
|
|
241
|
+
if (!target.small || bigLabel(target.el) || inlineInText(target) || userAgentSized(target) || !crowded(target))
|
|
242
|
+
continue;
|
|
243
|
+
push(smallTargets, target.el, `${Math.round(target.rect.width)}x${Math.round(target.rect.height)}`);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
finally {
|
|
247
|
+
frame?.remove();
|
|
248
|
+
}
|
|
249
|
+
// polish/type-scale counts the sizes of visible content text only: paragraphs, list items, headings, link and
|
|
250
|
+
// button labels, quotes and table cells with text of their own. Hidden, aria-hidden, code, SVG and tiny
|
|
251
|
+
// (under 10 px) text is decoration, not the type scale.
|
|
252
|
+
const sizes = new Set();
|
|
253
|
+
const CONTENT = "p, li, h1, h2, h3, h4, h5, h6, a, button, blockquote, td, th";
|
|
254
|
+
const ownText = (el) => Array.from(el.childNodes).some((node) => node.nodeType === Node.TEXT_NODE && (node.textContent ?? "").trim().length >= 2);
|
|
255
|
+
for (const el of Array.from(document.querySelectorAll(CONTENT))) {
|
|
256
|
+
if (!ownText(el) || el.closest('[aria-hidden="true"], code, pre, kbd, samp, svg') || !visible(el))
|
|
257
|
+
continue;
|
|
258
|
+
if (!el.checkVisibility({ opacityProperty: true, visibilityProperty: true }))
|
|
259
|
+
continue;
|
|
260
|
+
const size = parseFloat(getComputedStyle(el).fontSize);
|
|
261
|
+
if (Number.isFinite(size) && size >= 10)
|
|
262
|
+
sizes.add(Math.round(size * 2) / 2);
|
|
263
|
+
}
|
|
264
|
+
const smallText = [];
|
|
265
|
+
// polish/body-text-size: characters of body paragraphs (60+ characters, not in footer, aside, nav and the like)
|
|
266
|
+
// per font size, rounded to 0.5 px. smallText lists the paragraphs under 15 px (MIN_BODY_PX).
|
|
267
|
+
const bodyText = { total: 0, bySize: {} };
|
|
268
|
+
const longLines = [];
|
|
269
|
+
for (const p of Array.from(document.querySelectorAll("p"))) {
|
|
270
|
+
if (!visible(p))
|
|
271
|
+
continue;
|
|
272
|
+
const length = textLength(p);
|
|
273
|
+
const style = getComputedStyle(p);
|
|
274
|
+
const fontSize = parseFloat(style.fontSize);
|
|
275
|
+
if (length >= 60 && !p.closest("footer, aside, small, figcaption, nav")) {
|
|
276
|
+
bodyText.total += length;
|
|
277
|
+
const key = String(Math.round(fontSize * 2) / 2);
|
|
278
|
+
bodyText.bySize[key] = (bodyText.bySize[key] ?? 0) + length;
|
|
279
|
+
if (fontSize < 15)
|
|
280
|
+
push(smallText, p, `${px(style.fontSize)}px`);
|
|
281
|
+
}
|
|
282
|
+
if (length > 85) {
|
|
283
|
+
const rect = p.getBoundingClientRect();
|
|
284
|
+
const range = document.createRange();
|
|
285
|
+
range.selectNodeContents(p);
|
|
286
|
+
const width = Math.min(rect.width - parseFloat(style.paddingLeft) - parseFloat(style.paddingRight), range.getBoundingClientRect().width);
|
|
287
|
+
const chars = width / (fontSize * 0.5);
|
|
288
|
+
if (chars > 85)
|
|
289
|
+
push(longLines, p, `about ${Math.round(chars)} characters per line`);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
const spacing = new Set();
|
|
293
|
+
for (const el of Array.from(document.querySelectorAll("section, header, footer, main, div"))) {
|
|
294
|
+
if ((el.tagName === "DIV" && el.children.length === 0) || !visible(el))
|
|
295
|
+
continue;
|
|
296
|
+
const style = getComputedStyle(el);
|
|
297
|
+
const values = [
|
|
298
|
+
style.paddingTop,
|
|
299
|
+
style.paddingRight,
|
|
300
|
+
style.paddingBottom,
|
|
301
|
+
style.paddingLeft,
|
|
302
|
+
style.marginTop,
|
|
303
|
+
style.marginBottom,
|
|
304
|
+
style.rowGap,
|
|
305
|
+
style.columnGap,
|
|
306
|
+
];
|
|
307
|
+
for (const value of values) {
|
|
308
|
+
const n = Math.abs(px(value));
|
|
309
|
+
if (Number.isFinite(n) && n > 0)
|
|
310
|
+
spacing.add(n);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
// Like Lighthouse unsized-images: a size counts when an attribute, the inline style or a matching CSS rule sets it.
|
|
314
|
+
const sizeRules = [];
|
|
315
|
+
const collect = (rules) => {
|
|
316
|
+
for (const rule of Array.from(rules)) {
|
|
317
|
+
if (rule instanceof CSSStyleRule) {
|
|
318
|
+
if (rule.style.width || rule.style.height)
|
|
319
|
+
sizeRules.push(rule);
|
|
320
|
+
}
|
|
321
|
+
else if (rule instanceof CSSMediaRule) {
|
|
322
|
+
if (window.matchMedia(rule.conditionText).matches)
|
|
323
|
+
collect(rule.cssRules);
|
|
324
|
+
}
|
|
325
|
+
else if (rule instanceof CSSSupportsRule) {
|
|
326
|
+
if (CSS.supports(rule.conditionText))
|
|
327
|
+
collect(rule.cssRules);
|
|
328
|
+
}
|
|
329
|
+
else if (rule instanceof CSSGroupingRule) {
|
|
330
|
+
collect(rule.cssRules);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
for (const sheet of Array.from(document.styleSheets)) {
|
|
335
|
+
try {
|
|
336
|
+
collect(sheet.cssRules);
|
|
337
|
+
}
|
|
338
|
+
catch {
|
|
339
|
+
// Cross-origin stylesheets do not expose their rules.
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
const setSize = (value) => value !== "" && value !== "auto";
|
|
343
|
+
const explicit = (img, prop) => {
|
|
344
|
+
if (setSize(img.style[prop]))
|
|
345
|
+
return true;
|
|
346
|
+
return sizeRules.some((rule) => {
|
|
347
|
+
if (!setSize(rule.style[prop]))
|
|
348
|
+
return false;
|
|
349
|
+
try {
|
|
350
|
+
return img.matches(rule.selectorText);
|
|
351
|
+
}
|
|
352
|
+
catch {
|
|
353
|
+
return false;
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
};
|
|
357
|
+
const unsizedImages = [];
|
|
358
|
+
const oversizedImages = [];
|
|
359
|
+
const dpr = window.devicePixelRatio || 1;
|
|
360
|
+
for (const img of Array.from(document.images)) {
|
|
361
|
+
if (!img.checkVisibility({ visibilityProperty: true }))
|
|
362
|
+
continue;
|
|
363
|
+
const style = getComputedStyle(img);
|
|
364
|
+
const name = (img.currentSrc || img.src).split(/[?#]/)[0]?.split("/").pop() || "image";
|
|
365
|
+
const positioned = style.position === "absolute" || style.position === "fixed";
|
|
366
|
+
const width = img.hasAttribute("width") || explicit(img, "width");
|
|
367
|
+
const height = img.hasAttribute("height") || explicit(img, "height");
|
|
368
|
+
const ratio = style.aspectRatio !== "auto" && (width || height);
|
|
369
|
+
if (!positioned && !(width && height) && !ratio) {
|
|
370
|
+
push(unsizedImages, img, name);
|
|
371
|
+
}
|
|
372
|
+
const rendered = img.getBoundingClientRect().width;
|
|
373
|
+
const svg = /\.svg$/i.test(name) || (img.currentSrc || img.src).startsWith("data:image/svg");
|
|
374
|
+
if (!svg && rendered > 0 && img.complete && img.naturalWidth >= 400 && img.naturalWidth > 2 * rendered * dpr) {
|
|
375
|
+
push(oversizedImages, img, `${img.naturalWidth} px file shown at ${Math.round(rendered)} px`);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
const transparent = (color) => color === "transparent" || /rgba\(.*,\s*0\)$/.test(color);
|
|
379
|
+
const html = getComputedStyle(document.documentElement).backgroundColor;
|
|
380
|
+
const bodyBackground = body ? getComputedStyle(body).backgroundColor : "transparent";
|
|
381
|
+
const firstColor = (selector) => {
|
|
382
|
+
const el = document.querySelector(selector);
|
|
383
|
+
return el ? getComputedStyle(el).color : "";
|
|
384
|
+
};
|
|
385
|
+
const theme = {
|
|
386
|
+
background: [bodyBackground, html].find((color) => !transparent(color)) ?? "canvas",
|
|
387
|
+
text: [body ? getComputedStyle(body).color : "", firstColor("h1"), firstColor("p")].join(" | "),
|
|
388
|
+
};
|
|
389
|
+
return {
|
|
390
|
+
scrolls,
|
|
391
|
+
scrollWidth,
|
|
392
|
+
clientWidth: viewport,
|
|
393
|
+
overflow,
|
|
394
|
+
smallTargets,
|
|
395
|
+
fontSizes: [...sizes].sort((a, b) => a - b),
|
|
396
|
+
smallText,
|
|
397
|
+
bodyText,
|
|
398
|
+
longLines,
|
|
399
|
+
spacing: [...spacing].sort((a, b) => a - b),
|
|
400
|
+
unsizedImages,
|
|
401
|
+
oversizedImages,
|
|
402
|
+
theme,
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
export function runningMotion() {
|
|
406
|
+
const describe = (el) => {
|
|
407
|
+
const id = el.id ? `#${CSS.escape(el.id)}` : "";
|
|
408
|
+
const classes = Array.from(el.classList)
|
|
409
|
+
.filter((c) => c.length <= 30)
|
|
410
|
+
.slice(0, 2)
|
|
411
|
+
.map((c) => `.${CSS.escape(c)}`)
|
|
412
|
+
.join("");
|
|
413
|
+
return `${el.tagName.toLowerCase()}${id}${classes}`;
|
|
414
|
+
};
|
|
415
|
+
const MOTION = /^(transform|opacity|translate|scale|rotate|all)$/;
|
|
416
|
+
const hits = [];
|
|
417
|
+
for (const animation of document.getAnimations()) {
|
|
418
|
+
if (animation.playState !== "running")
|
|
419
|
+
continue;
|
|
420
|
+
const effect = animation.effect;
|
|
421
|
+
if (!(effect instanceof KeyframeEffect) || !effect.target)
|
|
422
|
+
continue;
|
|
423
|
+
const timing = effect.getComputedTiming();
|
|
424
|
+
const duration = typeof timing.duration === "number" ? timing.duration : 0;
|
|
425
|
+
const infinite = timing.iterations === Infinity;
|
|
426
|
+
const active = typeof timing.activeDuration === "number" ? timing.activeDuration : 0;
|
|
427
|
+
let name = "script animation";
|
|
428
|
+
let movesLayer = effect
|
|
429
|
+
.getKeyframes()
|
|
430
|
+
.some((frame) => ["transform", "opacity", "translate", "scale", "rotate"].some((prop) => prop in frame));
|
|
431
|
+
if (animation instanceof CSSTransition) {
|
|
432
|
+
name = `transition ${animation.transitionProperty}`;
|
|
433
|
+
movesLayer = MOTION.test(animation.transitionProperty);
|
|
434
|
+
}
|
|
435
|
+
else if (animation instanceof CSSAnimation) {
|
|
436
|
+
name = `animation ${animation.animationName}`;
|
|
437
|
+
}
|
|
438
|
+
if (infinite || active > 1000 || (movesLayer && duration > 300)) {
|
|
439
|
+
hits.push({ selector: describe(effect.target), detail: `${name}, ${Math.round(duration)} ms${infinite ? ", infinite" : ""}` });
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
return hits;
|
|
443
|
+
}
|
|
444
|
+
// Runs axe-core in the main frame. axe must be injected first (see polish-run.ts). Returns only the fields the
|
|
445
|
+
// polish rules read, so the result crosses the page boundary as plain JSON.
|
|
446
|
+
export async function runAxe(options) {
|
|
447
|
+
const axe = window.axe;
|
|
448
|
+
const { violations } = await axe.run(document, options);
|
|
449
|
+
return violations.map((v) => ({
|
|
450
|
+
id: v.id,
|
|
451
|
+
impact: v.impact ?? null,
|
|
452
|
+
nodes: v.nodes.map((node) => {
|
|
453
|
+
const ratio = node.any[0]?.data?.contrastRatio;
|
|
454
|
+
return { target: node.target, ...(ratio ? { contrastRatio: ratio } : {}) };
|
|
455
|
+
}),
|
|
456
|
+
}));
|
|
457
|
+
}
|
package/dist/psi.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { Framework, PageSpeedScores, StrategyScores } from "./report.js";
|
|
2
|
+
import type { PsiLcp } from "./rules/convert-render.js";
|
|
3
|
+
import { type RuleResult } from "./rules/types.js";
|
|
4
|
+
export { STRATEGIES, minScore, type Strategy } from "./psi-scores.js";
|
|
5
|
+
export declare const PSI_ENDPOINT = "https://www.googleapis.com/pagespeedonline/v5/runPagespeed";
|
|
6
|
+
export declare const PSI_CATEGORIES: readonly ["performance", "accessibility", "best-practices", "seo"];
|
|
7
|
+
export declare const DEFAULT_RUNS = 3;
|
|
8
|
+
export declare const MAX_AUDIT_FINDINGS = 25;
|
|
9
|
+
type PsiCategory = (typeof PSI_CATEGORIES)[number];
|
|
10
|
+
export interface PsiResponse {
|
|
11
|
+
status: number;
|
|
12
|
+
json(): Promise<unknown>;
|
|
13
|
+
}
|
|
14
|
+
export type PsiFetcher = (url: string, init: {
|
|
15
|
+
signal: AbortSignal;
|
|
16
|
+
}) => Promise<PsiResponse>;
|
|
17
|
+
export interface PsiOptions {
|
|
18
|
+
runs?: number;
|
|
19
|
+
framework?: Framework;
|
|
20
|
+
key?: string;
|
|
21
|
+
fetcher?: PsiFetcher;
|
|
22
|
+
retryDelayMs?: number;
|
|
23
|
+
}
|
|
24
|
+
export interface PsiRun {
|
|
25
|
+
results: RuleResult[];
|
|
26
|
+
pagespeed?: PageSpeedScores;
|
|
27
|
+
lcp?: PsiLcp;
|
|
28
|
+
}
|
|
29
|
+
interface Audit {
|
|
30
|
+
id?: string;
|
|
31
|
+
title?: string;
|
|
32
|
+
description?: string;
|
|
33
|
+
score?: number | null;
|
|
34
|
+
scoreDisplayMode?: string;
|
|
35
|
+
displayValue?: string;
|
|
36
|
+
numericValue?: number;
|
|
37
|
+
}
|
|
38
|
+
interface LighthouseResult {
|
|
39
|
+
categories?: Partial<Record<PsiCategory, {
|
|
40
|
+
score?: number | null;
|
|
41
|
+
auditRefs?: {
|
|
42
|
+
id: string;
|
|
43
|
+
}[];
|
|
44
|
+
}>>;
|
|
45
|
+
audits?: Record<string, Audit>;
|
|
46
|
+
}
|
|
47
|
+
interface Run {
|
|
48
|
+
scores: StrategyScores;
|
|
49
|
+
lighthouse: LighthouseResult;
|
|
50
|
+
fieldLcpMs?: number;
|
|
51
|
+
}
|
|
52
|
+
export declare function isPrivateUrl(url: string): boolean;
|
|
53
|
+
export declare function redact(text: string, key: string | undefined): string;
|
|
54
|
+
export declare function bestScores(runs: Run[]): StrategyScores;
|
|
55
|
+
export declare function auditResults(lighthouseRuns: LighthouseResult[], target: string, framework: Framework): RuleResult[];
|
|
56
|
+
export declare function runPsi(target: string, options?: PsiOptions): Promise<PsiRun>;
|