-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplayer.js
106 lines (95 loc) · 2.87 KB
/
player.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
{
// DOM
const script = document.currentScript;
let canvas = document.getElementById('canvas');
if (!canvas) {
canvas = document.createElement('CANVAS');
canvas.id = 'canvas';
script.parentNode.insertBefore(canvas, script);
}
canvas.oncontextmenu = () => event.preventDefault();
let spinner = document.getElementById('spinner');
if (!spinner) {
spinner = document.createElement('DIV');
spinner.id = 'spinner';
script.parentNode.after(spinner, script);
}
spinner.className = 'pending';
// Parse arguments from the URL address
let url = new URL(script.src);
if (!url.searchParams.has('g'))
url = new URL(window.location.href);
let arg = url.searchParams.get('arg');
let uri = url.searchParams.get('g');
let compat = url.searchParams.get('c');
let version = url.searchParams.get('v');
if (uri == null)
uri = 'nogame.love';
if (arg) {
try {
arg = JSON.parse(arg);
if (!Array.isArray(arg))
arg = [arg];
} catch (error) {
arg = null;
console.log(error);
}
}
import('./game.js')
.then((imported) => {
const load = imported.default;
// Runs the requested package
window.runLove = () => {
//state = 'loading';
spinner.className = 'loading';
load(canvas, uri, arg, version, compat)
.then((res) => {
canvas.style.display = 'block';
canvas.focus();
//state = 'playing';
spinner.className = '';
})
.catch((err) => {
console.log(err);
if (uri != 'nogame.love') {
uri = 'nogame.love';
arg = null;
window.runLove();
}
});
}
// Handling errors
//window.alert = window.onerror = (msg) => {
window.onerror = (msg) => {
console.log(msg);
if (spinner.className != '') {
canvas.style.display = 'none';
spinner.className = 'error';
}
};
// Focus when running inside an iFrame
window.onload = window.focus.bind(window);
// Handle touch and mouse input
window.onclick = (e) => {
window.focus();
};
// Disable window scrolling using the arrow keys
const prevent = [37, 38, 39, 40, 13];
window.onkeydown = (e) => {
if (prevent.indexOf(e.keyCode) > -1)
e.preventDefault();
}
// Fixes a persistence bug when using the back and forward buttons
window.onpageshow = (event) => {
canvas.style.display = 'none';
if (event.persisted)
window.location.reload();
};
if (!window.SharedArrayBuffer) {
//alert('The Cross-Origin Policy is not configured properly');
throw new Error('The Cross-Origin Policy is not configured properly');
return;
}
window.runLove();
});
};