-
Notifications
You must be signed in to change notification settings - Fork 10
/
common.js
149 lines (134 loc) · 3.82 KB
/
common.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
'use strict';
// Common code shared between index.html, app.html, embed.html etc.
// This should be loaded after phosphorus.dist.js
/** @type {any} */
var P;
// @ts-ignore
var Common = (function() {
var DEFAULT_OPTIONS = P.player.Player.DEFAULT_OPTIONS;
// "truthy" values
var TRUE = ['true', 'yes', 'on', '1'];
// "falsey" values
var FALSE = ['false', 'no', 'off', '0'];
function parseSearch(handler) {
location.search.substr(1).split('&').forEach(function(p) {
var parts = p.split('=');
if (parts.length < 1) {
return;
}
handler(parts[0], parts[1] || '');
});
}
var playerOptions = {};
var projectId = null;
parseSearch(function(key, value) {
function setPlayerOption(name, value) {
// Check that this option exists
if (!DEFAULT_OPTIONS.hasOwnProperty(name)) {
throw new Error('Unknown option: ' + name);
}
// Get the default value and type
var defaultValue = DEFAULT_OPTIONS[name];
var expectedType = typeof defaultValue;
// Convert the input value to the correct type
if (expectedType === 'number') {
value = +value;
if (Number.isNaN(value)) {
console.warn('Value for ' + name + ' is an invalid number, skipping.');
return;
}
}
if (expectedType === 'boolean') {
value = value.toLowerCase();
if (TRUE.indexOf(value) > -1) {
value = true;
} else if (FALSE.indexOf(value) > -1) {
value = false;
} else {
console.warn('Value for ' + name + ' is an invalid boolean(-like), skipping.');
return;
}
}
playerOptions[name] = value;
}
function setPlayerFlag(name, value) {
setPlayerOption(name, value || 'true');
}
function setPlayerEnum(name, value, values) {
if (values.indexOf(value) > -1) {
setPlayerOption(name, value);
} else {
console.warn(value, 'is not one of', values.join(', '));
}
}
switch (key) {
// Player options
case 'fps':
setPlayerOption('fps', value);
break;
case 'username':
setPlayerOption('username', value);
break;
case 'turbo':
setPlayerFlag('turbo', value);
break;
case 'imageSmoothing':
setPlayerFlag('imageSmoothing', value);
break;
case 'fencing':
setPlayerFlag('spriteFencing', value);
break;
case 'limits':
setPlayerFlag('removeLimits', value);
break;
case 'cloud':
setPlayerEnum('cloudVariables', value, ['off', 'ws', 'localStorage']);
break;
case 'chost':
setPlayerFlag('cloudHost', value);
break;
case 'phost':
// Legacy mode was removed by Scratch. We will just ignore it.
if (value !== 'legacy') {
setPlayerFlag('projectHost', value);
}
break;
case 'chhost':
setPlayerFlag('cloudHistoryHost', value);
break;
// Project ID
case 'id':
// projectId is also read from location.hash if not found here.
projectId = value;
break;
// Feature flags
case 'webgl':
P.config.useWebGL = true;
break;
case 'debug':
P.config.debug = true;
break;
case 'video':
P.config.supportVideoSensing = true;
break;
case 'opt':
P.config.experimentalOptimizations = true;
break;
case 'svgr':
P.config.allowRasterizeVectors = false;
break;
}
});
// Check hash for project ID if not specified in search string
if (projectId === null) {
var hash = location.hash.substr(1);
if (/^\d+$/.test(hash)) {
projectId = hash;
}
}
return {
parseSearch: parseSearch,
playerOptions: playerOptions,
projectId: projectId,
};
}());