-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
127 lines (110 loc) · 4.44 KB
/
javascript.js
File metadata and controls
127 lines (110 loc) · 4.44 KB
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
const Scratch2API = {
_ready: false,
_queued: [],
_swf: null,
attach(swfEl) { this._swf = swfEl; return this; },
markReady() { this._ready = true; this._flush(); },
_callAS(name, ...args) {
const invoke = () => {
const fn = this._swf && this._swf[name];
if (typeof fn === 'function') {
try { return fn.apply(this._swf, args); }
catch (e) { console.error("[AS call failed]", name, e); }
} else {
console.warn("[AS not exposed]", name);
}
};
if (!this._ready || !this._swf) { this._queued.push(invoke); return undefined; }
return invoke();
},
_flush() { const q = this._queued.splice(0); q.forEach(fn => fn()); },
_getInitialProject() {
const qs = new URLSearchParams(location.search).get('project');
if (qs) return String(qs).trim();
const obj = document.getElementById('scratch');
return obj ? obj.getAttribute('data-project') : null;
},
// Choose ONE project loader and make sure the SWF actually exposes it:
// Option A: one-stop
loadProject(spec) { this._callAS('ASloadProject', String(spec).trim()); },
// Player/editor + presentation
setPresentationMode(on) { this._callAS('ASsetPresentationMode', !!on); },
setEditMode(isEditor) { this._callAS('ASsetEditMode', !!isEditor); },
setLoginUser(username) { this._callAS('ASsetLoginUser', String(username)); },
setScratcher(isScratcher) { this._callAS('ASsetScratcher', !!isScratcher); },
start() { this._callAS('ASstartRunning'); },
stop() { this._callAS('ASstopRunning'); },
// Optional getters
isEditMode() { return this._callAS('ASisEditMode'); },
getVersion() { return this._callAS('ASversion'); }
};
// --- Global callbacks called BY the SWF ---
window.JSeditorReady = function(info) {
// Mark the JS side ready and flush queued calls
Scratch2API.markReady();
const swf = document.getElementById('scratch');
const exposed = Object.keys(swf || {}).filter(k => /^AS/.test(k));
console.log("[AS functions]", exposed);
// Discover exposed AS* methods to verify names
//const swf = document.getElementById('scratch');
const asFns = Object.keys(swf || {}).filter(k => /^AS/.test(k));
console.log("[AS exposed]", asFns.sort().join(", "));
// Kick off initial project load if present
const proj = Scratch2API._getInitialProject();
//if (proj) Scratch2API.loadProject(proj);
return true;
};
window.JSsetPresentationMode = on => {
document.body.classList.toggle('presentation', !!on);
return true;
};
window.JSsetEditorMode = mode => {
document.body.classList.toggle('editor', mode === 'editor');
document.body.classList.toggle('player', mode === 'player');
return true;
};
window.JSloadProgress = p => console.log("Load:", Math.round(p * 100) + "%");
window.JSerror = msg => console.error("[SWF]", msg);
window.JSupdateTitle = title => { document.title = title ? (title + " — Scratch 2.0") : "Scratch 2.0"; };
window.JSrequestProject = () => {
const proj = Scratch2API._getInitialProject();
if (proj) Scratch2API.loadProject(proj);
};
// --- Embed SWF ---
document.addEventListener('DOMContentLoaded', function () {
const flashvars = {
autostart: 'false',
cdnToken: '9087G67ugu7678ugknGnjf3w5',
urlOverrides: encodeURIComponent(JSON.stringify({
sitePrefix: 'http://127.0.0.1:5500/webapp/',
siteCdnPrefix: 'http://127.0.0.1:5500/webapp/cdn/',
assetPrefix: 'http://127.0.0.1:5500/webapp/assets/',
assetCdnPrefix: 'http://127.0.0.1:5500/webapp/cdn.assets/',
projectPrefix: 'http://127.0.0.1:5500/webapp/projects/',
projectCdnPrefix: 'http://127.0.0.1:5500/webapp/cdn.projects/',
internalAPI: 'internalapi/',
siteAPI: 'site-api/',
staticFiles: 'scratchr2/static/'
})),
project_title: "Untitled-1",
project_isPrivate: true,
project_isNew: true
};
const params = {
allowscriptaccess: 'always',
allowfullscreen: 'true',
wmode: 'direct',
menu: 'false',
flashvars: Object.keys(flashvars).map(k => k + '=' + flashvars[k]).join('&')
};
// Provide a real project spec via attribute or just rely on ?project=
const attributes = {
data: 'http://127.0.0.1:5500/build/11.6/Scratch.swf',
width: '100%',
height: '100%',
id: 'scratch', // set id up front so callbacks can find it
'data-project': '/webapp/projects/000000000.sb2' // or remove and use ?project=
};
const swfEl = swfobject.createSWF(attributes, params, 'scratch');
Scratch2API.attach(swfEl || document.getElementById('scratch'));
});