-
Notifications
You must be signed in to change notification settings - Fork 3
/
widget.js
227 lines (187 loc) · 7.42 KB
/
widget.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
(function() {
'use strict'
var DOM_ID = 'EARTH_DAY_LIVE'
var CLOSED_COOKIE = '_EARTH_DAY_LIVE_WIDGET_CLOSED_'
var NOW = new Date().getTime()
var MS_PER_DAY = 86400000
// user-configurable options
var options = window.EARTH_DAY_LIVE_OPTIONS || {}
var iframeHost = options.iframeHost !== undefined ? options.iframeHost : 'https://widget.earthdaylive2020.org'
var websiteName = options.websiteName || null
var partnerReferrer = options.partnerReferrer || null
var footerDisplayStartDate = options.footerDisplayStartDate || new Date(2019, 12, 1) // January 1st, 2020 - arbitrary date in the past
var fullPageDisplayStartDate = options.fullPageDisplayStartDate || new Date(2020, 3, 22) // April 22nd, 2020
var forceFullPageWidget = !!options.forceFullPageWidget
var cookieExpirationDays = parseFloat(options.cookieExpirationDays || 1)
var alwaysShowWidget = !!(options.alwaysShowWidget || window.location.hash.indexOf('ALWAYS_SHOW_EARTH_DAY_LIVE') !== -1)
var disableGoogleAnalytics = !!options.disableGoogleAnalytics
var showCloseButtonOnFullPageWidget = !!options.showCloseButtonOnFullPageWidget
var language = getLanguage()
function getIframeSrc() {
var src = iframeHost
src += language === 'en' ? '/index.html?' : '/index-' + language + '.html?'
var urlParams = [
['hostname', window.location.host],
['fullPageDisplayStartDate', fullPageDisplayStartDate.toISOString()],
['language', language]
]
forceFullPageWidget && urlParams.push(['forceFullPageWidget', 'true'])
showCloseButtonOnFullPageWidget && urlParams.push(['showCloseButtonOnFullPageWidget', 'true'])
disableGoogleAnalytics && urlParams.push(['googleAnalytics', 'false'])
websiteName && urlParams.push(['websiteName', encodeURI(websiteName)])
partnerReferrer && urlParams.push(['partnerReferrer', partnerReferrer])
var params = urlParams.map(function(el) {
return el.join('=')
})
return src + params.join('&')
}
function createIframe() {
var wrapper = document.createElement('div')
wrapper.id = DOM_ID
var iframe = document.createElement('iframe')
iframe.src = getIframeSrc()
iframe.frameBorder = 0
iframe.allowTransparency = true
wrapper.appendChild(iframe)
document.body.appendChild(wrapper)
iframe.contentWindow.focus()
return wrapper
}
function getLanguage() {
var language = 'en'
// Spanish is specified or no language is set and browser is set to spanish
if (options.language === 'es' || (!options.language && navigator && navigator.language.match(/^es/))) {
language = 'es'
}
// German is specified or no language is set and browser is set to German
if (options.language === 'de' || (!options.language && navigator && navigator.language.match(/^de/))) {
language = 'de'
}
// Czech is specified or no language is set and browser is set to German
if (options.language === 'cs' || (!options.language && navigator && navigator.language.match(/^cs/))) {
language = 'cs'
}
// French is specified or no language is set and browser is set to French
if (options.language === 'fr' || (!options.language && navigator && navigator.language.match(/^fr/))) {
language = 'fr'
}
// Dutch is specified or no language is set and browser is set to Dutch
if (options.language === 'nl' || (!options.language && navigator && navigator.language.match(/^nl/))) {
language = 'nl'
}
// Turkish is specified or no language is set and browser is set to Turkish
if (options.language === 'tr' || (!options.language && navigator && navigator.language.match(/^tr/))) {
language = 'tr'
}
// Portuguese is specified or no language is set and browser is set to Portuguese
if (options.language === 'pt' || (!options.language && navigator && navigator.language.match(/^pt/))) {
language = 'pt'
}
// Italian is specified or no language is set and browser is set to Italian
if (options.language === 'it' || (!options.language && navigator && navigator.language.match(/^it/))) {
language = 'it'
}
return language
}
function maximize() {
document.getElementById(DOM_ID).style.width = '100%'
document.getElementById(DOM_ID).style.height = '100%'
}
function closeWindow() {
var wrapper = document.getElementById(DOM_ID)
wrapper.parentNode.removeChild(wrapper)
window.removeEventListener('message', receiveMessage)
setCookie(CLOSED_COOKIE, 'true', cookieExpirationDays)
}
function navigateToLink(linkUrl) {
document.location = linkUrl
}
function injectCSS(id, css) {
var style = document.createElement('style')
style.type = 'text/css'
style.id = id
if (style.styleSheet) {
style.styleSheet.cssText = css
}
else {
style.appendChild(document.createTextNode(css))
}
document.head.appendChild(style)
}
function setCookie(name, value, expirationDays) {
var d = new Date()
d.setTime(d.getTime()+(expirationDays * MS_PER_DAY))
var expires = 'expires='+d.toGMTString()
document.cookie = name + '=' + value + '; ' + expires + '; path=/'
}
function getCookie(cookieName) {
var name = cookieName + '='
var ca = document.cookie.split(';')
var c
for(var i = 0; i < ca.length; i++) {
c = ca[i].trim()
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length)
}
}
return ''
}
function receiveMessage(event) {
if (!event.data.EARTH_DAY_LIVE) return
if (event.origin.lastIndexOf(iframeHost, 0) !== 0) return
switch (event.data.action) {
case 'maximize':
return maximize()
case 'closeButtonClicked':
return closeWindow()
case 'buttonClicked':
if (event.data.linkUrl.lastIndexOf('http', 0) !== 0) return
return navigateToLink(event.data.linkUrl)
}
}
/**
* There are a few circumstances when the iFrame should not be shown:
* 1. When the CLOSED_COOKIE has been set on that device
* 2. We haven't reached either display start date
* 3. We're past the date to display the full screen widget.
* 4. We haven't set alwaysShowWidget to be true in the config.
*/
function iFrameShouldNotBeShown() {
if (alwaysShowWidget) return false
return (footerDisplayStartDate.getTime() > NOW && fullPageDisplayStartDate.getTime() > NOW)
|| new Date(fullPageDisplayStartDate.getTime() + MS_PER_DAY) < NOW
|| !!getCookie(CLOSED_COOKIE)
}
function initializeInterface() {
if (iFrameShouldNotBeShown()) {
return
}
createIframe()
var iFrameHeight = getIframeHeight()
injectCSS('EARTH_DAY_LIVE_CSS',
'#' + DOM_ID + ' { position: fixed; right: 0; left: 0; bottom: 0px; width: 100%; height: ' + iFrameHeight + '; z-index: 20000; -webkit-overflow-scrolling: touch; overflow: hidden; } ' +
'#' + DOM_ID + ' iframe { width: 100%; height: 100%; }'
)
// listen for messages from iframe
window.addEventListener('message', receiveMessage)
document.removeEventListener('DOMContentLoaded', initializeInterface)
}
function getIframeHeight() {
var isProbablyMobile = window.innerWidth < 600
if (isProbablyMobile) {
return '200px'
} else {
return '145px'
}
}
// Wait for DOM content to load.
switch(document.readyState) {
case 'complete':
case 'loaded':
case 'interactive':
initializeInterface()
break
default:
document.addEventListener('DOMContentLoaded', initializeInterface)
}
})()