forked from mikemaccana/outdated-browser-rework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·289 lines (246 loc) · 7.98 KB
/
index.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
288
289
var UserAgentParser = require("ua-parser-js")
var languageMessages = require("./languages.json")
var deepExtend = require("./extend")
var DEFAULTS = {
Chrome: 57, // Includes Chrome for mobile devices
Edge: 39,
Safari: 10,
"Mobile Safari": 10,
Opera: 50,
Firefox: 50,
Vivaldi: 1,
IE: false
}
var EDGEHTML_VS_EDGE_VERSIONS = {
12: 0.1,
13: 21,
14: 31,
15: 39,
16: 41,
17: 42,
18: 44
}
var COLORS = {
salmon: "#f25648",
white: "white"
}
var updateDefaults = function(defaults, updatedValues) {
for (var key in updatedValues) {
defaults[key] = updatedValues[key]
}
return defaults
}
module.exports = function(options) {
var main = function() {
// Despite the docs, UA needs to be provided to constructor explicitly:
// https://github.com/faisalman/ua-parser-js/issues/90
var parsedUserAgent = new UserAgentParser(window.navigator.userAgent).getResult()
// Variable definition (before ajax)
var outdatedUI = document.getElementById("outdated")
options = options || {}
var browserLocale = window.navigator.language || window.navigator.userLanguage // Everyone else, IE
// Set default options
var browserSupport = options.browserSupport ? updateDefaults(DEFAULTS, options.browserSupport) : DEFAULTS
// CSS property to check for. You may also like 'borderSpacing', 'boxShadow', 'transform', 'borderImage';
var requiredCssProperty = options.requiredCssProperty || false
var backgroundColor = options.backgroundColor || COLORS.salmon
var textColor = options.textColor || COLORS.white
var fullscreen = options.fullscreen || false
var language = options.language || browserLocale.slice(0, 2) // Language code
var updateSource = "web" // Other possible values are 'googlePlay' or 'appStore'. Determines where we tell users to go for upgrades.
// Chrome mobile is still Chrome (unlike Safari which is 'Mobile Safari')
var isAndroid = parsedUserAgent.os.name === "Android"
if (isAndroid) {
updateSource = "googlePlay"
}
var isAndroidButNotChrome
if (options.requireChromeOnAndroid) {
isAndroidButNotChrome = isAndroid && parsedUserAgent.browser.name !== "Chrome"
}
if (parsedUserAgent.os.name === "iOS") {
updateSource = "appStore"
}
var done = true
var changeOpacity = function(opacityValue) {
outdatedUI.style.opacity = opacityValue / 100
outdatedUI.style.filter = "alpha(opacity=" + opacityValue + ")"
}
var fadeIn = function(opacityValue) {
changeOpacity(opacityValue)
if (opacityValue === 1) {
outdatedUI.style.display = "table"
}
if (opacityValue === 100) {
done = true
}
}
var parseMinorVersion = function (version) {
return version.replace(/[^\d.]/g,'').split(".")[1];
}
var isBrowserUnsupported = function() {
var browserName = parsedUserAgent.browser.name
var isUnsupported = false
if (!(browserName in browserSupport)) {
if (!options.isUnknownBrowserOK) {
isUnsupported = true
}
} else if (!browserSupport[browserName]) {
isUnsupported = true
}
return isUnsupported;
}
var isBrowserOutOfDate = function() {
var browserName = parsedUserAgent.browser.name
var browserMajorVersion = parsedUserAgent.browser.major
if (browserName === "Edge") {
browserMajorVersion = EDGEHTML_VS_EDGE_VERSIONS[browserMajorVersion]
}
var isOutOfDate = false
if (isBrowserUnsupported()) {
isOutOfDate = true;
} else if (browserName in browserSupport) {
var minVersion = browserSupport[browserName];
if (typeof minVersion == 'object') {
var minMajorVersion = minVersion.major;
var minMinorVersion = minVersion.minor;
if (browserMajorVersion < minMajorVersion) {
isOutOfDate = true
} else if (browserMajorVersion == minMajorVersion) {
var browserMinorVersion = parseMinorVersion(parsedUserAgent.browser.version)
if (browserMinorVersion < minMinorVersion) {
isOutOfDate = true
}
}
} else if (browserMajorVersion < minVersion) {
isOutOfDate = true
}
}
return isOutOfDate
}
// Returns true if a browser supports a css3 property
var isPropertySupported = function(property) {
if (!property) {
return true
}
var div = document.createElement("div")
var vendorPrefixes = ["khtml", "ms", "o", "moz", "webkit"]
var count = vendorPrefixes.length
// Note: HTMLElement.style.hasOwnProperty seems broken in Edge
if (property in div.style) {
return true
}
property = property.replace(/^[a-z]/, function(val) {
return val.toUpperCase()
})
while (count--) {
var prefixedProperty = vendorPrefixes[count] + property
// See comment re: HTMLElement.style.hasOwnProperty above
if (prefixedProperty in div.style) {
return true
}
}
return false
}
var makeFadeInFunction = function(opacityValue) {
return function() {
fadeIn(opacityValue)
}
}
// Style element explicitly - TODO: investigate and delete if not needed
var startStylesAndEvents = function() {
var buttonClose = document.getElementById("buttonCloseUpdateBrowser")
var buttonUpdate = document.getElementById("buttonUpdateBrowser")
//check settings attributes
outdatedUI.style.backgroundColor = backgroundColor
//way too hard to put !important on IE6
outdatedUI.style.color = textColor
outdatedUI.children[0].children[0].style.color = textColor
outdatedUI.children[0].children[1].style.color = textColor
// Update button is desktop only
if (buttonUpdate) {
buttonUpdate.style.color = textColor
if (buttonUpdate.style.borderColor) {
buttonUpdate.style.borderColor = textColor
}
// Override the update button color to match the background color
buttonUpdate.onmouseover = function() {
this.style.color = backgroundColor
this.style.backgroundColor = textColor
}
buttonUpdate.onmouseout = function() {
this.style.color = textColor
this.style.backgroundColor = backgroundColor
}
}
buttonClose.style.color = textColor
buttonClose.onmousedown = function() {
outdatedUI.style.display = "none"
return false
}
}
var getmessage = function(lang) {
var defaultMessages = languageMessages[lang] || languageMessages.en
var customMessages = options.messages && options.messages[lang]
var messages = deepExtend({}, defaultMessages, customMessages)
var updateMessages = {
web:
"<p>" +
messages.update.web +
'<a id="buttonUpdateBrowser" rel="nofollow" href="' +
messages.url +
'">' +
messages.callToAction +
"</a></p>",
googlePlay:
"<p>" +
messages.update.googlePlay +
'<a id="buttonUpdateBrowser" rel="nofollow" href="https://play.google.com/store/apps/details?id=com.android.chrome">' +
messages.callToAction +
"</a></p>",
appStore: "<p>" + messages.update[updateSource] + "</p>"
}
var updateMessage = updateMessages[updateSource]
var browserSupportMessage = messages.outOfDate;
if (isBrowserUnsupported() && messages.unsupported) {
browserSupportMessage = messages.unsupported;
}
return (
'<div class="vertical-center"><h6>' +
browserSupportMessage +
"</h6>" +
updateMessage +
'<p class="last"><a href="#" id="buttonCloseUpdateBrowser" title="' +
messages.close +
'">×</a></p></div>'
)
}
// Check if browser is supported
if (isBrowserOutOfDate() || !isPropertySupported(requiredCssProperty) || isAndroidButNotChrome) {
// This is an outdated browser
if (done && outdatedUI.style.opacity !== "1") {
done = false
for (var opacity = 1; opacity <= 100; opacity++) {
setTimeout(makeFadeInFunction(opacity), opacity * 8)
}
}
var insertContentHere = document.getElementById("outdated")
if (fullscreen) {
insertContentHere.classList.add("fullscreen")
}
insertContentHere.innerHTML = getmessage(language)
startStylesAndEvents()
}
}
// Load main when DOM ready.
var oldOnload = window.onload
if (typeof window.onload !== "function") {
window.onload = main
} else {
window.onload = function() {
if (oldOnload) {
oldOnload()
}
main()
}
}
}