|
1 | | -(function() { |
| 1 | +/* global ga */ |
| 2 | + |
2 | 3 | 'use strict'; |
3 | 4 |
|
4 | | -const input = document.querySelector('#url'); |
5 | | -const logger = document.querySelector('#logger'); |
6 | | -const scoreEl = document.querySelector('#lighthouse-score'); |
7 | | -const reportLink = document.querySelector('#reportLink'); |
8 | | -const background = document.querySelector('#background'); |
9 | | -const logo = document.querySelector('.logo-section'); |
10 | | -const searchArrow = document.querySelector('.search-arrow'); |
11 | | -const startOver = document.querySelector('#startover'); |
12 | | - |
13 | | -// const params = new URLSearchParams(location.search); |
14 | | -// let setTimeoutId_; |
15 | | -const ENDPOINT_ORIGIN = location.hostname === 'localhost' ? |
| 5 | +(function() { |
| 6 | + const input = document.querySelector('#url'); |
| 7 | + const logger = document.querySelector('#logger'); |
| 8 | + const scoreEl = document.querySelector('#lighthouse-score'); |
| 9 | + const reportLink = document.querySelector('#reportLink'); |
| 10 | + const background = document.querySelector('#background'); |
| 11 | + const logo = document.querySelector('.logo-section'); |
| 12 | + const searchArrow = document.querySelector('.search-arrow'); |
| 13 | + const startOver = document.querySelector('#startover'); |
| 14 | + |
| 15 | + // const params = new URLSearchParams(location.search); |
| 16 | + // let setTimeoutId_; |
| 17 | + const ENDPOINT_ORIGIN = location.hostname === 'localhost' ? |
16 | 18 | 'http://localhost:8080' : 'https://builder-dot-lighthouse-ci.appspot.com'; |
17 | 19 |
|
18 | | -// /** |
19 | | -// * @param {number} score |
20 | | -// * @return {string} |
21 | | -// */ |
22 | | -// function calculateRating(score) { |
23 | | -// let rating = 'poor'; |
24 | | -// if (score > 45) { |
25 | | -// rating = 'average'; |
26 | | -// } |
27 | | -// if (score > 75) { |
28 | | -// rating = 'good'; |
29 | | -// } |
30 | | -// return rating; |
31 | | -// } |
32 | | - |
33 | | -// /** |
34 | | -// * @param {(number|string)} score |
35 | | -// */ |
36 | | -// function setScore(score) { |
37 | | -// score = Number(score); |
38 | | - |
39 | | -// const rating = calculateRating(score); |
40 | | - |
41 | | -// scoreEl.textContent = score; |
42 | | -// scoreEl.classList.add(rating); |
43 | | -// document.body.classList.add(rating, 'done'); |
44 | | -// document.body.classList.remove('running'); |
45 | | -// } |
46 | | - |
47 | | -function startNewRun() { |
48 | | - resetUI(false); |
49 | | - document.body.classList.add('running'); |
50 | | -} |
51 | | - |
52 | | -/** |
53 | | - * @param {string} url |
54 | | - */ |
55 | | -function finalizeRun(url) { |
56 | | - // const match = logger.value.match(/.*LIGHTHOUSE SCORE:\s+(.*)/); |
57 | | - // if (match) { |
58 | | - // let score = Number(match[1]); |
59 | | - // score = score.toLocaleString(undefined, {maximumFractionDigits: 1}); |
60 | | - |
61 | | - // setScore(score); |
62 | | - startOver.tabIndex = 0; |
63 | | - reportLink.tabIndex = 0; |
64 | | - reportLink.href = url; |
65 | | - // reportLink.setAttribute('href', url); |
66 | | - reportLink.focus(); |
67 | | - |
68 | | - ga('send', 'event', 'Lighthouse', 'finish run'); |
69 | | - // } else { |
70 | | - // const split = logger.value.split('\n'); |
71 | | - // ga('send', 'event', 'Lighthouse', 'error', split[split.length - 2]); |
| 20 | + // /** |
| 21 | + // * @param {number} score |
| 22 | + // * @return {string} |
| 23 | + // */ |
| 24 | + // function calculateRating(score) { |
| 25 | + // let rating = 'poor'; |
| 26 | + // if (score > 45) { |
| 27 | + // rating = 'average'; |
| 28 | + // } |
| 29 | + // if (score > 75) { |
| 30 | + // rating = 'good'; |
| 31 | + // } |
| 32 | + // return rating; |
72 | 33 | // } |
73 | 34 |
|
74 | | - document.body.classList.remove('running'); |
75 | | - document.body.classList.add('done'); |
76 | | -} |
| 35 | + // /** |
| 36 | + // * @param {(number|string)} score |
| 37 | + // */ |
| 38 | + // function setScore(score) { |
| 39 | + // score = Number(score); |
77 | 40 |
|
78 | | -function updateLog(data) { |
79 | | - logger.value += data.replace(/.*GMT\s/, '') + '\n'; |
80 | | - logger.scrollTop = logger.scrollHeight; |
81 | | -} |
| 41 | + // const rating = calculateRating(score); |
82 | 42 |
|
83 | | -function resetUI(clearInput=true) { |
84 | | - if (clearInput) { |
85 | | - input.value = null; |
86 | | - } |
87 | | - logger.value = ''; |
88 | | - document.body.className = ''; |
89 | | - reportLink.tabIndex = -1; |
90 | | - reportLink.href = '#'; |
91 | | - startOver.tabIndex = -1; |
92 | | - scoreEl.textContent = ''; |
93 | | - scoreEl.className = ''; |
94 | | -} |
95 | | - |
96 | | -/** |
97 | | - * @param {string} url URL to test in Lighthouse. |
98 | | - */ |
99 | | -function runLighthouse(url = '') { |
100 | | - // If user inputs domain, make it a full URL. |
101 | | - if (!url.match(/^https?:\/\//) && url.length) { |
102 | | - url = `http://${url}`; |
103 | | - input.value = url; |
| 43 | + // scoreEl.textContent = score; |
| 44 | + // scoreEl.classList.add(rating); |
| 45 | + // document.body.classList.add(rating, 'done'); |
| 46 | + // document.body.classList.remove('running'); |
| 47 | + // } |
| 48 | + |
| 49 | + function startNewRun() { |
| 50 | + resetUI(false); |
| 51 | + document.body.classList.add('running'); |
104 | 52 | } |
105 | 53 |
|
106 | | - if (!input.validity.valid) { |
107 | | - alert('URL is not valid. Please make sure to use a full URL (e.g. https://www.example.com).'); |
108 | | - return; |
| 54 | + /** |
| 55 | + * @param {string} url |
| 56 | + */ |
| 57 | + function finalizeRun(url) { |
| 58 | + // const match = logger.value.match(/.*LIGHTHOUSE SCORE:\s+(.*)/); |
| 59 | + // if (match) { |
| 60 | + // let score = Number(match[1]); |
| 61 | + // score = score.toLocaleString(undefined, {maximumFractionDigits: 1}); |
| 62 | + |
| 63 | + // setScore(score); |
| 64 | + startOver.tabIndex = 0; |
| 65 | + reportLink.tabIndex = 0; |
| 66 | + reportLink.href = url; |
| 67 | + // reportLink.setAttribute('href', url); |
| 68 | + reportLink.focus(); |
| 69 | + |
| 70 | + ga('send', 'event', 'Lighthouse', 'finish run'); |
| 71 | + // } else { |
| 72 | + // const split = logger.value.split('\n'); |
| 73 | + // ga('send', 'event', 'Lighthouse', 'error', split[split.length - 2]); |
| 74 | + // } |
| 75 | + |
| 76 | + document.body.classList.remove('running'); |
| 77 | + document.body.classList.add('done'); |
109 | 78 | } |
110 | 79 |
|
111 | | - let endpoint = `${ENDPOINT_ORIGIN}/stream?url=${url}`; |
112 | | - if (document.querySelector('#useheadless').checked) { |
113 | | - endpoint += '&headless=true'; |
| 80 | + function updateLog(data) { |
| 81 | + logger.value += data.replace(/.*GMT\s/, '') + '\n'; |
| 82 | + logger.scrollTop = logger.scrollHeight; |
114 | 83 | } |
115 | 84 |
|
116 | | - const source = new EventSource(endpoint); |
| 85 | + function resetUI(clearInput=true) { |
| 86 | + if (clearInput) { |
| 87 | + input.value = null; |
| 88 | + } |
| 89 | + logger.value = ''; |
| 90 | + document.body.className = ''; |
| 91 | + reportLink.tabIndex = -1; |
| 92 | + reportLink.href = '#'; |
| 93 | + startOver.tabIndex = -1; |
| 94 | + scoreEl.textContent = ''; |
| 95 | + scoreEl.className = ''; |
| 96 | + } |
117 | 97 |
|
118 | | - source.addEventListener('message', e => { |
119 | | - if (e.data.startsWith('done')) { |
120 | | - source.close(); |
| 98 | + /** |
| 99 | + * @param {string} url URL to test in Lighthouse. |
| 100 | + */ |
| 101 | + function runLighthouse(url = '') { |
| 102 | + // If user inputs domain, make it a full URL. |
| 103 | + if (!url.match(/^https?:\/\//) && url.length) { |
| 104 | + url = `http://${url}`; |
| 105 | + input.value = url; |
| 106 | + } |
121 | 107 |
|
122 | | - let url = e.data.split(' ')[1]; |
123 | | - if (url.includes('localhost')) { |
124 | | - url = url.replace('https://localhost', ENDPOINT_ORIGIN); |
125 | | - } |
126 | | - finalizeRun(url); |
| 108 | + if (!input.validity.valid) { |
| 109 | + alert('URL is not valid. Please make sure to use a full URL (e.g. https://www.example.com).'); |
127 | 110 | return; |
128 | 111 | } |
129 | 112 |
|
130 | | - updateLog(e.data); |
131 | | - }) |
| 113 | + let endpoint = `${ENDPOINT_ORIGIN}/stream?url=${url}`; |
| 114 | + if (document.querySelector('#useheadless').checked) { |
| 115 | + endpoint += '&headless=true'; |
| 116 | + } |
132 | 117 |
|
133 | | - source.addEventListener('open', e => { |
134 | | - startNewRun(); |
135 | | - ga('send', 'event', 'Lighthouse', 'start run'); |
136 | | - }); |
| 118 | + const source = new EventSource(endpoint); |
137 | 119 |
|
138 | | - source.addEventListener('error', e => { |
139 | | - if (e.readyState === EventSource.CLOSED) { |
140 | | - source.close(); |
141 | | - } |
142 | | - }); |
143 | | -} |
| 120 | + source.addEventListener('message', e => { |
| 121 | + if (e.data.startsWith('done')) { |
| 122 | + source.close(); |
144 | 123 |
|
145 | | -function attachEventListeners() { |
146 | | - input.addEventListener('keydown', e => { |
147 | | - if (e.keyCode === 13) { // Enter |
148 | | - runLighthouse(e.target.value); |
149 | | - } |
150 | | - }); |
| 124 | + let url = e.data.split(' ')[1]; |
| 125 | + if (url.includes('localhost')) { |
| 126 | + url = url.replace('https://localhost', ENDPOINT_ORIGIN); |
| 127 | + } |
| 128 | + finalizeRun(url); |
| 129 | + return; |
| 130 | + } |
151 | 131 |
|
152 | | - document.addEventListener('click', e => input.focus()); |
| 132 | + updateLog(e.data); |
| 133 | + }); |
153 | 134 |
|
154 | | - logo.addEventListener('click', e => { |
155 | | - if (document.body.classList.contains('done')) { |
156 | | - fetch('/reset'); |
157 | | - resetUI(); |
158 | | - document.querySelector('#useheadless').checked = false; |
159 | | - } |
160 | | - }); |
| 135 | + source.addEventListener('open', e => { |
| 136 | + startNewRun(); |
| 137 | + ga('send', 'event', 'Lighthouse', 'start run'); |
| 138 | + }); |
161 | 139 |
|
162 | | - searchArrow.addEventListener('click', e => { |
163 | | - runLighthouse(input.value); |
164 | | - }); |
| 140 | + source.addEventListener('error', e => { |
| 141 | + if (e.readyState === EventSource.CLOSED) { |
| 142 | + source.close(); |
| 143 | + } |
| 144 | + }); |
| 145 | + } |
165 | 146 |
|
166 | | - startOver.addEventListener('click', e => { |
167 | | - e.preventDefault(); |
168 | | - resetUI(); |
169 | | - document.querySelector('#useheadless').checked = false; |
170 | | - }); |
| 147 | + function attachEventListeners() { |
| 148 | + input.addEventListener('keydown', e => { |
| 149 | + if (e.keyCode === 13) { // Enter |
| 150 | + runLighthouse(e.target.value); |
| 151 | + } |
| 152 | + }); |
171 | 153 |
|
172 | | - reportLink.addEventListener('click', e => { |
173 | | - ga('send', 'event', 'Lighthouse', 'open report'); |
174 | | - }); |
175 | | -} |
| 154 | + document.addEventListener('click', e => input.focus()); |
176 | 155 |
|
177 | | -attachEventListeners(); |
178 | | -input.focus(); |
| 156 | + logo.addEventListener('click', e => { |
| 157 | + if (document.body.classList.contains('done')) { |
| 158 | + fetch('/reset'); |
| 159 | + resetUI(); |
| 160 | + document.querySelector('#useheadless').checked = false; |
| 161 | + } |
| 162 | + }); |
| 163 | + |
| 164 | + searchArrow.addEventListener('click', e => { |
| 165 | + runLighthouse(input.value); |
| 166 | + }); |
| 167 | + |
| 168 | + startOver.addEventListener('click', e => { |
| 169 | + e.preventDefault(); |
| 170 | + resetUI(); |
| 171 | + document.querySelector('#useheadless').checked = false; |
| 172 | + }); |
| 173 | + |
| 174 | + reportLink.addEventListener('click', e => { |
| 175 | + ga('send', 'event', 'Lighthouse', 'open report'); |
| 176 | + }); |
| 177 | + } |
179 | 178 |
|
| 179 | + attachEventListeners(); |
| 180 | + input.focus(); |
180 | 181 | })(); |
0 commit comments