-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsg.js
70 lines (61 loc) · 1.9 KB
/
sg.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
"use strict";
var Sg;
(function(Sg) {
var win = window;
function createIframe(target, uri, token, callbacks) {
let game = uri + "/" + token;
fetch(game).then(function(response) {
if (response.ok) {
return response.text();
} else {
throw new Error("Failed to fetch iframe: " + response.status);
}
})
.then(function(content) {
var iframeUrl = new URL(game);
var iframe = document.createElement("iframe");
iframe.src = iframeUrl.toString();
iframe.setAttribute("allowfullscreen", "true");
target.appendChild(iframe);
callbacks.success();
})
.catch(function(error) {
callbacks.error(error);
});
}
function startGame(options, sc, ec) {
var callbacks = {
success: function() {
if (sc) {
sc();
}
},
error: function(e) {
if (ec) {
ec(e);
}
}
};
var target = document.getElementById(options.target_element);
if (!target) {
callbacks.error("No element with id '".concat(options.target_element, "' found"));
return;
}
var lo = options.launch_options;
switch (lo.strategy) {
case "redirect":
win.location = lo.game_url;
break;
case "iframe":
createIframe(target, lo.game_url, lo.token, callbacks);
break;
default:
throw new Error("Unexpected launch strategy");
}
}
Sg.startGame = startGame;
Sg.createIframe = createIframe;
win.sg = {
launch: startGame
};
})(Sg || (Sg = {}));