-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
112 lines (82 loc) · 2.49 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
(function() {
//------------------------------
// Constants
//------------------------------
var RE_LOCALHOST = /^localhost|(127|192)[\.\d+]+$/i;
var RE_DOMAIN = /^http(?:s)?:\/\/([\w\.-]+)/i;
var RE_TRUSTED = /^moonlander.scalingo.io$/i;
var READY_MESSAGE = 'moonlander:ready';
var GAME_REMOTE = 'https://moonlander.scalingo.io';
var GAME_LOCAL = '//' + window.location.hostname + ':3000';
//------------------------------
// Variables
//------------------------------
var local = RE_LOCALHOST.test(window.location.hostname);
var gameURL = local ? GAME_LOCAL : GAME_REMOTE;
var loader = document.getElementById('loader');
var frame = document.createElement('iframe');
var logo = document.getElementById('logo');
var game = document.getElementById('game');
var animationReady = false;
var frameReady = false;
//------------------------------
// Methods
//------------------------------
function init() {
listenForMessage();
setTimeout(function() {
animationReady = true;
embedGame();
}, 2000);
setTimeout(function() {
loader.classList.add('show');
logo.classList.add('show');
}, 250);
}
function checkReady() {
if (animationReady && frameReady) {
loader.classList.remove('show');
logo.classList.remove('show');
game.classList.add('show');
setTimeout(function() {
if (loader.parentNode) {
loader.parentNode.removeChild(loader);
}
}, 3000);
}
}
function listenForMessage() {
window.addEventListener('message', function(event) {
var message = event.data;
var origin = event.origin;
var domain = origin.match(RE_DOMAIN);
if (domain) {
var trusted =
RE_LOCALHOST.test(domain[1]) ||
RE_TRUSTED.test(domain[1]);
if (trusted && message === READY_MESSAGE) {
console.log('iframe ready');
frameReady = true;
checkReady();
}
}
}, false);
}
function embedGame() {
console.log('embed game');
game.appendChild(frame);
frame.classList.add('frame', 'fill');
frame.height = '100%';
frame.width = '100%';
frame.onload = function(event) {
console.log('iframe loaded');
ga('send', 'event', 'embed', 'loaded', gameURL);
};
ga('send', 'event', 'embed', 'load', gameURL);
frame.src = gameURL;
}
//------------------------------
// Bootstrap
//------------------------------
init();
})();