|
| 1 | +# Script Return Value Schema |
| 2 | + |
| 3 | +All scripts return a structured JSON object as the IIFE return value. This allows agents using `evaluate_script` to read structured data directly from the return value, rather than parsing human-readable console output. |
| 4 | + |
| 5 | +## Base Shape |
| 6 | + |
| 7 | +```typescript |
| 8 | +{ |
| 9 | + script: string; // Script name, e.g. "LCP", "CLS", "INP" |
| 10 | + status: "ok" // Script ran, has data |
| 11 | + | "tracking" // Observer active, data accumulates over time |
| 12 | + | "error" // Failed or no data available |
| 13 | + | "unsupported"; // Browser API not supported |
| 14 | + |
| 15 | + // Metric scripts (LCP, CLS, INP) |
| 16 | + metric?: string; |
| 17 | + value?: number; // Always a number, never a formatted string |
| 18 | + unit?: "ms" | "score" | "count" | "bytes" | "bpp" | "fps"; |
| 19 | + rating?: "good" | "needs-improvement" | "poor"; |
| 20 | + thresholds?: { good: number; needsImprovement: number }; |
| 21 | + |
| 22 | + // Audit scripts |
| 23 | + count?: number; |
| 24 | + items?: object[]; |
| 25 | + details?: object; |
| 26 | + issues?: Array<{ severity: "error" | "warning" | "info"; message: string }>; |
| 27 | + |
| 28 | + // Tracking scripts |
| 29 | + message?: string; |
| 30 | + getDataFn?: string; // window function name: evaluate_script(`${getDataFn}()`) |
| 31 | + |
| 32 | + // Error info |
| 33 | + error?: string; |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +## Agent Workflow |
| 38 | + |
| 39 | +``` |
| 40 | +// Synchronous scripts (LCP, CLS, LCP-Sub-Parts, LCP-Trail, LCP-Image-Entropy, LCP-Video-Candidate) |
| 41 | +result = evaluate_script(scriptCode) |
| 42 | +// → { status: "ok", value: 1240, rating: "good", ... } |
| 43 | +
|
| 44 | +// Tracking scripts (INP) |
| 45 | +result = evaluate_script(INP_js) |
| 46 | +// → { status: "tracking", getDataFn: "getINP" } |
| 47 | +// (user interacts with the page) |
| 48 | +data = evaluate_script("getINP()") |
| 49 | +// → { status: "ok", value: 350, rating: "needs-improvement", ... } |
| 50 | +
|
| 51 | +// CLS (hybrid: returns current value immediately, keeps tracking) |
| 52 | +result = evaluate_script(CLS_js) |
| 53 | +// → { status: "ok", value: 0.05, rating: "good", message: "Call getCLS() for updated value" } |
| 54 | +// (after more page interactions) |
| 55 | +data = evaluate_script("getCLS()") |
| 56 | +// → { status: "ok", value: 0.08, rating: "good", ... } |
| 57 | +``` |
| 58 | + |
| 59 | +## Making Decisions from Return Values |
| 60 | + |
| 61 | +- `rating === "good"` → no action needed for this metric |
| 62 | +- `rating === "needs-improvement"` → investigate, check `details` and `issues` |
| 63 | +- `rating === "poor"` → high priority fix, check `issues` for specific problems |
| 64 | +- `status === "error"` → page may not have loaded yet, or metric has no data |
| 65 | +- `status === "tracking"` → call `evaluate_script(result.getDataFn + "()")` after interaction |
| 66 | + |
| 67 | +## Script-Specific Schemas |
| 68 | + |
| 69 | +### LCP |
| 70 | +```json |
| 71 | +{ |
| 72 | + "script": "LCP", "status": "ok", "metric": "LCP", |
| 73 | + "value": 1240, "unit": "ms", "rating": "good", |
| 74 | + "thresholds": { "good": 2500, "needsImprovement": 4000 }, |
| 75 | + "details": { "element": "img.hero", "elementType": "Image", "url": "hero.jpg", "sizePixels": 756000 } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +### CLS |
| 80 | +```json |
| 81 | +{ |
| 82 | + "script": "CLS", "status": "ok", "metric": "CLS", |
| 83 | + "value": 0.05, "unit": "score", "rating": "good", |
| 84 | + "thresholds": { "good": 0.1, "needsImprovement": 0.25 }, |
| 85 | + "message": "CLS tracking active. Call getCLS() for updated value after page interactions." |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### INP (initial — tracking) |
| 90 | +```json |
| 91 | +{ "script": "INP", "status": "tracking", "message": "INP tracking active. Interact with the page then call getINP() for results.", "getDataFn": "getINP" } |
| 92 | +``` |
| 93 | +`getINP()` returns: |
| 94 | +```json |
| 95 | +{ |
| 96 | + "script": "INP", "status": "ok", "metric": "INP", |
| 97 | + "value": 350, "unit": "ms", "rating": "needs-improvement", |
| 98 | + "thresholds": { "good": 200, "needsImprovement": 500 }, |
| 99 | + "details": { "totalInteractions": 5, "worstEvent": "click → button.submit", "phases": { "inputDelay": 120, "processingTime": 180, "presentationDelay": 50 } } |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +### LCP-Sub-Parts |
| 104 | +```json |
| 105 | +{ |
| 106 | + "script": "LCP-Sub-Parts", "status": "ok", "metric": "LCP", |
| 107 | + "value": 2100, "unit": "ms", "rating": "needs-improvement", |
| 108 | + "thresholds": { "good": 2500, "needsImprovement": 4000 }, |
| 109 | + "details": { |
| 110 | + "element": "img.hero", "url": "hero.jpg", |
| 111 | + "subParts": { |
| 112 | + "ttfb": { "value": 450, "percent": 21, "overTarget": false }, |
| 113 | + "resourceLoadDelay": { "value": 120, "percent": 6, "overTarget": false }, |
| 114 | + "resourceLoadTime": { "value": 1200, "percent": 57, "overTarget": true }, |
| 115 | + "elementRenderDelay": { "value": 330, "percent": 16, "overTarget": true } |
| 116 | + }, |
| 117 | + "slowestPhase": "resourceLoadTime" |
| 118 | + } |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +### LCP-Trail |
| 123 | +```json |
| 124 | +{ |
| 125 | + "script": "LCP-Trail", "status": "ok", "metric": "LCP", |
| 126 | + "value": 1240, "unit": "ms", "rating": "good", |
| 127 | + "thresholds": { "good": 2500, "needsImprovement": 4000 }, |
| 128 | + "details": { |
| 129 | + "candidateCount": 2, "finalElement": "img.hero", |
| 130 | + "candidates": [ |
| 131 | + { "index": 1, "selector": "h1", "time": 800, "elementType": "Text block" }, |
| 132 | + { "index": 2, "selector": "img.hero", "time": 1240, "elementType": "Image", "url": "hero.jpg" } |
| 133 | + ] |
| 134 | + } |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +### LCP-Image-Entropy |
| 139 | +```json |
| 140 | +{ |
| 141 | + "script": "LCP-Image-Entropy", "status": "ok", "count": 5, |
| 142 | + "details": { "totalImages": 5, "lowEntropyCount": 1, "lcpImageEligible": true, "lcpImage": { "url": "hero.jpg", "bpp": 1.65, "isLowEntropy": false } }, |
| 143 | + "items": [ |
| 144 | + { "url": "hero.jpg", "width": 1200, "height": 630, "fileSizeBytes": 156000, "bpp": 1.65, "isLowEntropy": false, "lcpEligible": true, "isLCP": true } |
| 145 | + ], |
| 146 | + "issues": [] |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +### LCP-Video-Candidate |
| 151 | +```json |
| 152 | +{ |
| 153 | + "script": "LCP-Video-Candidate", "status": "ok", "metric": "LCP", |
| 154 | + "value": 1800, "unit": "ms", "rating": "good", |
| 155 | + "thresholds": { "good": 2500, "needsImprovement": 4000 }, |
| 156 | + "details": { |
| 157 | + "isVideo": true, "posterUrl": "https://example.com/hero.avif", "posterFormat": "avif", |
| 158 | + "posterPreloaded": true, "fetchpriorityOnPreload": "high", "isCrossOrigin": false, |
| 159 | + "videoAttributes": { "autoplay": true, "muted": true, "playsinline": true, "preload": "auto" } |
| 160 | + }, |
| 161 | + "issues": [] |
| 162 | +} |
| 163 | +``` |
0 commit comments