Skip to content

Commit dd507eb

Browse files
committed
Option for special behaviours for certain cloud variables (resolves #6)
1 parent bfbbf57 commit dd507eb

File tree

3 files changed

+74
-27
lines changed

3 files changed

+74
-27
lines changed

hacky-file-getter.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ function downloadAsHTML(projectSrc, {
104104
log = console.log,
105105
monitorColour = null,
106106
cloudServer = false,
107+
specialCloud = false,
107108
projectId = null,
108109
noVM = false,
109110
width = 480,
@@ -230,8 +231,14 @@ function downloadAsHTML(projectSrc, {
230231
if (stretch) template = removePercentSection(template, 'fit');
231232
else template = removePercentSection(template, 'stretch');
232233
if (!noCursor) template = removePercentSection(template, 'no-cursor');
234+
if (!specialCloud) {
235+
template = removePercentSection(template, 'special-cloud');
236+
}
233237
if (cloudServer) {
234-
template = removePercentSection(template, 'cloud-localstorage')
238+
if (!specialCloud) {
239+
template = removePercentSection(template, 'cloud-localstorage');
240+
}
241+
template = removePercentSection(template, 'cloud-localstorage-provider')
235242
.replace(/\{CLOUD_HOST\}/g, () => JSON.stringify(cloudServer));
236243
} else {
237244
template = removePercentSection(template, 'cloud-ws');

index.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ <h3><em>Convert a Scratch project to HTML</em></h3>
7878
<legend>Cloud variable source</legend>
7979
<p><label><input type="radio" name="cloud-provider" value="localstorage" checked> Save cloud variables locally using localStorage</label><a id="ref-1" href="#note-1"><sup>1</sup></a></p>
8080
<p><label><input type="radio" name="cloud-provider" value="ws"> Use a <a href="https://github.com/SheepTester/primitive-cloud-server">custom server</a>: <input type="url" id="cloud-ws" placeholder="ws://localhost:3000/"></label></p>
81+
<p><label><input type="checkbox" id="special-cloud"> Give certain cloud variables special behaviours depending on the name?</label><a id="ref-4" href="#note-4"><sup>4</sup></a></p>
8182
</fieldset>
8283
<fieldset>
8384
<legend><a href="https://sheeptester.github.io/scratch-gui/">E羊icques</a> (modded) options</legend>
@@ -94,6 +95,7 @@ <h3><em>Convert a Scratch project to HTML</em></h3>
9495
<p id="note-1"><a href="#ref-1"><sup>1</sup></a>You may have to deal with privacy laws around cookies outside of Scratch.</p>
9596
<p id="note-2"><a href="#ref-2"><sup>2</sup></a>Compatibility mode forces projects to run at 30 FPS, like in Scratch 2.0. Turning this off allows the project to run at a higher framerate (usually 60 FPS, depending on the computer screen's refresh rate).</p>
9697
<p id="note-3"><a href="#ref-3"><sup>3</sup></a>I think the implementation of this is poor. Maybe instead of setting mouse x/y, it can set a cloud variable with a certain name. You can leave feedback and suggestions on <a href="https://scratch.mit.edu/users/Sheep_maker/">my profile</a>.</p>
98+
<p id="note-4"><a href="#ref-4"><sup>4</sup></a>If a cloud variable with the name "☁ eval" is set, it'll run the variable value as JavaScript and store it in a cloud variable named "☁ eval output"; if there is an error, it'll be stored in "☁ eval error." If you're using a custom cloud server, then cloud variables whose names start with "☁ local storage" will store their values in localStorage instead of in the server.</p>
9799
<h2>Update history</h2>
98100
<p>See the code and previous versions on <a href="https://github.com/SheepTester/htmlifier/">Github</a>.</p>
99101
<h3>2020-05-01</h3>
@@ -188,6 +190,7 @@ <h3>2019-02-09</h3>
188190
const pointerLock = document.getElementById('pointer-lock');
189191
const stretch = document.getElementById('stretch');
190192
const noCursor = document.getElementById('no-cursor');
193+
const specialCloud = document.getElementById('special-cloud');
191194
let scroll = false
192195
function log(text, style) {
193196
const entry = document.createElement('div')
@@ -226,7 +229,8 @@ <h3>2019-02-09</h3>
226229
noLimits: noLimits.checked,
227230
pointerLock: pointerLock.checked,
228231
stretch: stretch.checked,
229-
noCursor: noCursor.checked
232+
noCursor: noCursor.checked,
233+
specialCloud: specialCloud.checked
230234
})
231235
.then(html => {
232236
log('Attempting to download HTML file...', 'status')

template.html

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,8 @@
450450

451451
const Scratch = window.Scratch = window.Scratch || {};
452452

453+
const CLOUD_PREFIX = '\u2601 ';
454+
453455
const runBenchmark = async function () {
454456
const vm = new window.NotVirtualMachine(% custom-ratio % WIDTH, HEIGHT % /custom-ratio %);
455457
Scratch.vm = vm;
@@ -497,25 +499,6 @@
497499
vm.setVideoProvider(videoProvider);
498500

499501
const noop = () => null;
500-
let cloudProvider;
501-
502-
% cloud-localstorage %
503-
cloudProvider = {
504-
updateVariable(name, value) {
505-
try {
506-
localStorage.setItem('[s3] ' + name, value);
507-
} catch (e) {
508-
console.error('Cannot use localStorage?', e);
509-
}
510-
},
511-
createVariable: noop,
512-
renameVariable: noop,
513-
deleteVariable: noop,
514-
requestCloseConnection: noop
515-
};
516-
% /cloud-localstorage %
517-
518-
% cloud-ws %
519502
let ws;
520503
function openConnection() {
521504
try {
@@ -528,7 +511,6 @@
528511
ws.onopen = onOpen;
529512
ws.onclose = onClose;
530513
}
531-
openConnection();
532514
function onMessage(e) {
533515
e.data.split('\n').forEach(message => {
534516
if (message) {
@@ -550,21 +532,61 @@
550532
function onClose() {
551533
setTimeout(openConnection, 500);
552534
}
553-
cloudProvider = {
535+
const cloudProvider = {
554536
updateVariable(name, value) {
555-
sendData({method: 'set', name, value});
537+
% special-cloud %
538+
if (name === CLOUD_PREFIX + 'eval') {
539+
try {
540+
vm.postIOData('cloud', {
541+
varUpdate: {
542+
name: CLOUD_PREFIX + 'eval output',
543+
value: eval(value)
544+
}
545+
});
546+
} catch (err) {
547+
vm.postIOData('cloud', {
548+
varUpdate: {
549+
name: CLOUD_PREFIX + 'eval error',
550+
value: err.toString()
551+
}
552+
});
553+
}
554+
} else % cloud-ws % if (name.startsWith(CLOUD_PREFIX + 'local storage')) % /cloud-ws % {
555+
% /special-cloud %
556+
% cloud-localstorage %
557+
try {
558+
localStorage.setItem('[s3] ' + name, value);
559+
} catch (e) {
560+
console.error('Cannot use localStorage?', e);
561+
}
562+
% /cloud-localstorage %
563+
% special-cloud %
564+
% cloud-ws %
565+
} else {
566+
% /cloud-ws %
567+
% /special-cloud %
568+
% cloud-ws %
569+
sendData({method: 'set', name, value});
570+
% /cloud-ws %
571+
% special-cloud %
572+
}
573+
% /special-cloud %
556574
},
557575
createVariable: noop,
558576
renameVariable: noop,
559577
deleteVariable: noop,
578+
% cloud-ws %
560579
requestCloseConnection() {
561580
if (ws && ws.readyState !== WebSocket.CLOSING && ws.readyState !== WebSocket.CLOSED) {
562581
ws.onclose = noop;
563582
ws.close();
564583
}
565584
}
585+
% /cloud-ws %
586+
% cloud-localstorage-provider %
587+
requestCloseConnection: noop
588+
% /cloud-localstorage-provider %
566589
};
567-
% /cloud-ws %
568590

569591
vm.setCompatibilityMode(COMPAT);
570592
vm.setTurboMode(TURBO);
@@ -910,27 +932,41 @@
910932
}
911933
% /loading-image %
912934

913-
if (cloudProvider) vm.setCloudProvider(cloudProvider);
935+
vm.setCloudProvider(cloudProvider);
914936
% cloud-localstorage %
915937
const stageVariables = vm.runtime.getTargetForStage().variables;
916938
for (const { name, isCloud } of Object.values(stageVariables)) {
917939
if (isCloud) {
940+
% special-cloud %
941+
% cloud-ws %
942+
if (!name.startsWith(CLOUD_PREFIX + 'local storage')) continue;
943+
% /cloud-ws %
944+
% /special-cloud %
918945
const value = localStorage.getItem('[s3] ' + name);
919946
if (value === null) continue;
920947
vm.postIOData('cloud', { varUpdate: { name, value } });
921948
}
922949
}
923950
window.addEventListener('storage', e => {
924951
if (e.storageArea === localStorage && e.key.slice(0, 5) === '[s3] ') {
952+
const name = e.key.slice(5);
953+
% special-cloud %
954+
% cloud-ws %
955+
if (!name.startsWith(`[s3] ${CLOUD_PREFIX}local storage`)) return;
956+
% /cloud-ws %
957+
% /special-cloud %
925958
vm.postIOData('cloud', {
926959
varUpdate: {
927-
name: e.key.slice(5),
960+
name,
928961
value: e.newValue
929962
}
930963
});
931964
}
932965
});
933966
% /cloud-localstorage %
967+
% cloud-ws %
968+
openConnection();
969+
% /cloud-ws %
934970

935971
vm.greenFlag();
936972
};

0 commit comments

Comments
 (0)