browser-checker.js
2.52 KB
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
class RocketBrowserCompatibilityChecker {
constructor( options ) {
this.passiveSupported = false;
this._checkPassiveOption( this );
this.options = this.passiveSupported ? options : false;
}
/**
* Initializes browser check for addEventListener passive option.
*
* @link https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Safely_detecting_option_support
* @private
*
* @param self Instance of this object.
* @returns {boolean}
*/
_checkPassiveOption( self ) {
try {
const options = {
// This function will be called when the browser attempts to access the passive property.
get passive() {
self.passiveSupported = true;
return false;
}
};
window.addEventListener( 'test', null, options );
window.removeEventListener( 'test', null, options );
} catch ( err ) {
self.passiveSupported = false;
}
}
/**
* Checks if the browser supports requestIdleCallback and cancelIdleCallback. If no, shims its behavior with a polyfills.
*
* @link @link https://developers.google.com/web/updates/2015/08/using-requestidlecallback
*/
initRequestIdleCallback() {
if ( ! 'requestIdleCallback' in window ) {
window.requestIdleCallback = ( cb ) => {
const start = Date.now();
return setTimeout( () => {
cb( {
didTimeout: false,
timeRemaining: function timeRemaining() {
return Math.max( 0, 50 - ( Date.now() - start ) );
}
} );
}, 1 );
};
}
if ( ! 'cancelIdleCallback' in window ) {
window.cancelIdleCallback = ( id ) => clearTimeout( id );
}
}
/**
* Detects if data saver mode is on.
*
* @link https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/save-data/#detecting_the_save-data_setting
*
* @returns {boolean|boolean}
*/
isDataSaverModeOn() {
return (
'connection' in navigator
&&
true === navigator.connection.saveData
);
}
/**
* Checks if the browser supports link prefetch.
*
* @returns {boolean|boolean}
*/
supportsLinkPrefetch() {
const elem = document.createElement( 'link' );
return (
elem.relList
&&
elem.relList.supports
&&
elem.relList.supports( 'prefetch' )
&&
window.IntersectionObserver
&&
'isIntersecting' in IntersectionObserverEntry.prototype
);
}
isSlowConnection() {
return (
'connection' in navigator
&&
'effectiveType' in navigator.connection
&&
(
'2g' === navigator.connection.effectiveType
||
'slow-2g' === navigator.connection.effectiveType
)
)
}
}