forked from GoogleChrome/web-vitals-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_worker.js
executable file
·287 lines (265 loc) · 7.66 KB
/
service_worker.js
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
Copyright 2020 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Core Web Vitals thresholds
const LCP_THRESHOLD = 2500;
const FID_THRESHOLD = 100;
const INP_THRESHOLD = 200;
const CLS_THRESHOLD = 0.1;
/**
* Hash the URL and return a numeric hash as a String to be used as the key
* @param {String} str
* @return {String} hash
*/
function hashCode(str) {
let hash = 0;
if (str.length === 0) {
return '';
}
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
hash = (hash << 5) - hash + char;
hash = hash & hash; // Convert to 32bit integer
}
return hash.toString();
}
/**
* Call vitals.js to begin collecting local WebVitals metrics.
* This will cause the content script to emit an event that kicks off the badging flow.
* @param {Number} tabId
*/
function getWebVitals(tabId) {
chrome.scripting.executeScript({
target: { tabId: tabId },
files: ['src/browser_action/vitals.js'],
}, (result) => {
// Catch errors such as "This page cannot be scripted due
// to an ExtensionsSettings policy."
});
}
// User has navigated to a new URL in a tab
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
const tabIdKey = tabId.toString();
if (tab.active) {
chrome.storage.local.set({[tabIdKey]: false});
} else {
chrome.storage.local.set({[tabIdKey]: true}); // tab was loaded in background
}
if (
changeInfo.status == 'complete' &&
tab.url.startsWith('http') &&
tab.active
) {
getWebVitals(tabId);
}
});
// User has made a new or existing tab visible
chrome.tabs.onActivated.addListener(({tabId, windowId}) => {
getWebVitals(tabId);
});
/**
*
* Update the badge icon based on the overall WebVitals
* pass rate (i.e good = green icon, poor = red icon)
* @param {String} badgeCategory - GOOD or POOR
* @param {Number} tabid
*/
function badgeOverallPerf(badgeCategory, tabid) {
chrome.tabs.query({
active: true,
currentWindow: true,
}, function(tabs) {
const currentTab = tabid || tabs[0].id;
switch (badgeCategory) {
case 'POOR':
chrome.action.setIcon({
path: '../../icons/slow128w.png',
tabId: currentTab,
});
chrome.action.setBadgeText({
text: '',
tabId: currentTab,
});
break;
case 'GOOD':
chrome.action.setIcon({
path: '../../icons/fast128w.png',
tabId: currentTab,
});
break;
default:
chrome.action.setIcon({
path: '../../icons/default128w.png',
tabId: currentTab,
});
chrome.action.setBadgeText({
text: '',
tabId: currentTab,
});
break;
}
});
}
/**
*
* Badge the icon for a specific metric
* @param {String} metric
* @param {Number} value
* @param {Number} tabid
*/
function badgeMetric(metric, value, tabid) {
chrome.tabs.query({
active: true,
currentWindow: true,
}, function(tabs) {
const currentTab = tabid || tabs[0].id;
const bgColor = '#000';
// If URL is overall failing the thresholds, only show
// a red badge for metrics actually failing (issues/22)
if (metric === 'cls' && value <= CLS_THRESHOLD) {
return;
}
if (metric === 'lcp' && value <= LCP_THRESHOLD) {
return;
}
if (metric === 'fid' && value <= FID_THRESHOLD) {
return;
}
switch (metric) {
case 'lcp':
chrome.action.setIcon({
path: '../../icons/slow128w-lcp.png',
tabId: currentTab,
});
chrome.action.setBadgeBackgroundColor({
color: bgColor,
tabId: currentTab,
});
chrome.action.setBadgeText({
text: (value / 1000).toFixed(2),
tabId: currentTab,
});
break;
case 'cls':
chrome.action.setIcon({
path: '../../icons/slow128w-cls.png',
tabId: currentTab,
});
chrome.action.setBadgeBackgroundColor({
color: bgColor,
tabId: currentTab,
});
chrome.action.setBadgeText({
text: (value).toFixed(2),
tabId: currentTab,
});
break;
case 'fid':
chrome.action.setIcon({
path: '../../icons/slow128w-fid.png',
tabId: currentTab,
});
chrome.action.setBadgeBackgroundColor({
color: bgColor,
tabId: currentTab,
});
chrome.action.setBadgeText({
text: value.toFixed(2),
tabId: currentTab,
});
break;
default:
chrome.action.setIcon({
path: '../../icons/default128w.png',
tabId: currentTab,
});
chrome.action.setBadgeBackgroundColor({
color: '',
tabId: currentTab,
});
chrome.action.setBadgeText({
text: '',
tabId: currentTab,
});
break;
}
});
}
/**
* Wait ms milliseconds
*
* @param {Number} ms
* @return {Promise}
*/
function wait(ms) {
return new Promise((r) => setTimeout(r, ms));
}
/**
* @param {number} tabId
* @return {Promise<boolean>}
*/
function doesTabExist(tabId) {
return new Promise((resolve) => {
chrome.tabs.get(tabId, () => resolve(!chrome.runtime.lastError));
});
}
/** @type {number} */
let globalAnimationId = 0;
/** @type {Map<number, number>} */
const animationsByTabId = new Map();
/**
* Animate badges between pass/fail -> each failing metric.
* We track each animation by tabId so that we can handle "cancellation" of the animation on new information.
* @param {Object} request
* @param {Number} tabId
*/
async function animateBadges(request, tabId) {
const animationId = globalAnimationId;
animationsByTabId.set(tabId, animationId);
globalAnimationId++;
const delay = 2000;
// First badge overall perf
badgeOverallPerf(request.passesAllThresholds, tabId);
// If perf is poor, animate the sequence
if (request.passesAllThresholds === 'POOR') {
await wait(delay);
if (animationsByTabId.get(tabId) !== animationId) return;
badgeMetric('lcp', request.metrics.lcp.value, tabId);
await wait(delay);
if (animationsByTabId.get(tabId) !== animationId) return;
badgeMetric('fid', request.metrics.fid.value, tabId);
await wait(delay);
if (animationsByTabId.get(tabId) !== animationId) return;
badgeMetric('cls', request.metrics.cls.value, tabId);
// Loop the animation if no new information came in while we animated.
await wait(delay);
if (animationsByTabId.get(tabId) !== animationId) return;
// Stop animating if the tab is gone
if (!(await doesTabExist(tabId))) return;
animateBadges(request, tabId);
}
}
// message from content script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.passesAllThresholds !== undefined) {
// e.g passesAllThresholds === 'GOOD' => green badge
animateBadges(request, sender.tab.id);
// Store latest metrics locally only.
// The popup will load the metric values from this storage.
if (sender.tab.url) {
const key = hashCode(sender.tab.url);
chrome.storage.local.set({[key]: request.metrics});
}
// send TabId to content script
sendResponse({tabId: sender.tab.id});
}
});