This repository has been archived by the owner on Jan 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
page_performance.js
77 lines (70 loc) · 2.11 KB
/
page_performance.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
/**
* Ghostery Page Performance
*
* This file generates page-level metrics using the
* Web Performance API. It is called via background.js `onNavigationCompleted()`.
*
* https://developer.mozilla.org/en-US/docs/Web/API/Window/performance
*
* Ghostery Safari App Extension
* https://www.ghostery.com/
*
* Copyright 2018 Ghostery, Inc. All rights reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0
*/
var PageInfo = (function(window, document) {
let state = document.readyState;
/**
* Calculate page domain and latency
*/
const analyzePageInfo = function() {
const { host, pathname, protocol } = document.location;
// Note: Safari has limited support for PerformanceTimingAPI properties
const unfixedLatency = Number(performance.timing.domComplete - performance.timing.navigationStart) / 1000;
let pageLatency = 0;
if (unfixedLatency >= 100) { // > 100 no decimal
pageLatency = unfixedLatency.toFixed();
} else if (unfixedLatency >= 10 && unfixedLatency < 100) { // 100 > 10 use one decimal
pageLatency = unfixedLatency.toFixed(1);
} else if (unfixedLatency < 10) { // < 10s use two decimals
pageLatency = unfixedLatency.toFixed(2);
}
console.log('Sending latency from page_performance', pageLatency, host+pathname);
safari.extension.dispatchMessage('recordPageInfo', {
domain: `${protocol}//${host}${pathname}`,
latency: pageLatency
});
};
/**
* Initialize functionality of this script
*/
const _initialize = function() {
// manually check to see if the onLoad event has fired
if (state !== 'complete') {
document.onreadystatechange = function() {
state = document.readyState;
if (state === 'complete') {
analyzePageInfo();
}
};
} else {
analyzePageInfo();
}
};
return {
/**
* Public API
*/
init() {
// only load on top-level domains
if (window !== window.parent || window.frameElement) {
return;
}
_initialize();
}
};
}(window, document));
PageInfo.init();