Follow-up e3da2e23: Fix typo in hideanons filter name
[lhc/web/wiklou.git] / resources / src / startup.js
1 /**
2 * Code in this file MUST work on even the most ancient of browsers!
3 *
4 * This file is where we decide whether to initialise the modern run-time.
5 */
6
7 /* global mw, $VARS, $CODE */
8
9 // eslint-disable-next-line no-unused-vars
10 var mediaWikiLoadStart = ( new Date() ).getTime(),
11 mwPerformance = ( window.performance && performance.mark ) ? performance : {
12 mark: function () {}
13 };
14
15 mwPerformance.mark( 'mwLoadStart' );
16
17 /**
18 * See <https://www.mediawiki.org/wiki/Compatibility#Browsers>
19 *
20 * Capabilities required for modern run-time:
21 * - DOM Level 4 & Selectors API Level 1
22 * - HTML5 & Web Storage
23 * - DOM Level 2 Events
24 * - JSON
25 *
26 * Browsers we support in our modern run-time (Grade A):
27 * - Chrome 4+
28 * - IE 9+
29 * - Firefox 3.5+
30 * - Safari 5+
31 * - Opera 10.5+
32 * - Mobile Safari (iOS 4+)
33 * - Android 2.0+
34 *
35 * Browsers we support in our no-javascript run-time (Grade C):
36 * - Chrome 1+
37 * - IE 6+
38 * - Firefox 3+
39 * - Safari 3+
40 * - Opera 10+
41 * - WebOS < 1.5
42 * - PlayStation
43 * - Symbian-based browsers
44 * - NetFront-based browser
45 * - Opera Mini
46 * - Nokia's Ovi Browser
47 * - MeeGo's browser
48 * - Google Glass
49 * - UC Mini (speed mode on)
50 *
51 * Other browsers that pass the check are considered Grade X.
52 *
53 * @param {string} [str] User agent, defaults to navigator.userAgent
54 * @return {boolean} User agent is compatible with MediaWiki JS
55 */
56 function isCompatible( str ) {
57 var ua = str || navigator.userAgent;
58 return !!(
59 // http://caniuse.com/#feat=queryselector
60 'querySelector' in document &&
61
62 // http://caniuse.com/#feat=namevalue-storage
63 // https://developer.blackberry.com/html5/apis/v1_0/localstorage.html
64 // https://blog.whatwg.org/this-week-in-html-5-episode-30
65 'localStorage' in window &&
66
67 // http://caniuse.com/#feat=addeventlistener
68 'addEventListener' in window &&
69
70 // http://caniuse.com/#feat=json
71 // https://phabricator.wikimedia.org/T141344#2784065
72 ( window.JSON && JSON.stringify && JSON.parse ) &&
73
74 // Hardcoded exceptions for browsers that pass the requirement but we don't want to
75 // support in the modern run-time.
76 // Note: Please extend the regex instead of adding new ones
77 !(
78 ua.match( /webOS\/1\.[0-4]|SymbianOS|Series60|NetFront|Opera Mini|S40OviBrowser|MeeGo|Android.+Glass|^Mozilla\/5\.0 .+ Gecko\/$|googleweblight/ ) ||
79 ua.match( /PlayStation/i )
80 )
81 );
82 }
83
84 // Conditional script injection
85 ( function () {
86 var NORLQ, script;
87 if ( !isCompatible() ) {
88 // Undo class swapping in case of an unsupported browser.
89 // See ResourceLoaderClientHtml::getDocumentAttributes().
90 document.documentElement.className = document.documentElement.className
91 .replace( /(^|\s)client-js(\s|$)/, '$1client-nojs$2' );
92
93 NORLQ = window.NORLQ || [];
94 while ( NORLQ.length ) {
95 NORLQ.shift()();
96 }
97 window.NORLQ = {
98 push: function ( fn ) {
99 fn();
100 }
101 };
102
103 // Clear and disable the other queue
104 window.RLQ = {
105 // No-op
106 push: function () {}
107 };
108
109 return;
110 }
111
112 /**
113 * The $CODE and $VARS placeholders are substituted in ResourceLoaderStartUpModule.php.
114 */
115 function startUp() {
116 mw.config = new mw.Map( $VARS.wgLegacyJavaScriptGlobals );
117
118 $CODE.registrations();
119
120 mw.config.set( $VARS.configuration );
121
122 // Must be after mw.config.set because these callbacks may use mw.loader which
123 // needs to have values 'skin', 'debug' etc. from mw.config.
124 // eslint-disable-next-line vars-on-top
125 var RLQ = window.RLQ || [];
126 while ( RLQ.length ) {
127 RLQ.shift()();
128 }
129 window.RLQ = {
130 push: function ( fn ) {
131 fn();
132 }
133 };
134
135 // Clear and disable the other queue
136 window.NORLQ = {
137 // No-op
138 push: function () {}
139 };
140 }
141
142 script = document.createElement( 'script' );
143 script.src = $VARS.baseModulesUri;
144 script.onload = script.onreadystatechange = function () {
145 if ( !script.readyState || /loaded|complete/.test( script.readyState ) ) {
146 // Clean up
147 script.onload = script.onreadystatechange = null;
148 script = null;
149 // Callback
150 startUp();
151 }
152 };
153 document.getElementsByTagName( 'head' )[ 0 ].appendChild( script );
154 }() );