implement "itch.io support"

This commit is contained in:
2022-01-04 22:34:21 +01:00
parent 68890e62ad
commit f062e545f6
3 changed files with 70 additions and 24 deletions

View File

@@ -12,7 +12,7 @@
<div id="centered"> <div id="centered">
<canvas id="screen" width="320" height="240"></canvas> <canvas id="screen" width="320" height="240"></canvas>
<div id="message"></div> <div id="message"></div>
<button id="cartButton">Load cart...</button> <button id="cartButton" style="visibility:hidden">Load cart...</button>
</div> </div>
<div id="footer"> <div id="footer">
<a href="http://unlicense.org/" ref="license">Unlicense</a> <a href="http://unlicense.org/" ref="license">Unlicense</a>

View File

@@ -221,7 +221,12 @@ async function runModule(data, keepUrl) {
} }
async function runModuleFromURL(url, keepUrl) { async function runModuleFromURL(url, keepUrl) {
runModule(await (await fetch(url)).arrayBuffer(), keepUrl); let response = await fetch(url);
let type = response.headers.get('Content-Type');
if(type && type.includes('html')) {
throw false;
}
runModule(await response.arrayBuffer(), keepUrl);
} }
function runModuleFromHash() { function runModuleFromHash() {
@@ -238,9 +243,11 @@ function runModuleFromHash() {
} }
window.onhashchange = runModuleFromHash; window.onhashchange = runModuleFromHash;
runModuleFromHash();
document.getElementById('cartButton').onclick = () => { let setupLoad = () => {
let loadButton = document.getElementById('cartButton');
loadButton.style = '';
loadButton.onclick = () => {
let fileInput = document.createElement('input'); let fileInput = document.createElement('input');
fileInput.type = 'file'; fileInput.type = 'file';
fileInput.accept = '.wasm,.uw8,application/wasm'; fileInput.accept = '.wasm,.uw8,application/wasm';
@@ -250,16 +257,42 @@ document.getElementById('cartButton').onclick = () => {
} }
}; };
fileInput.click(); fileInput.click();
}; };
screen.ondragover = (e) => { screen.ondragover = (e) => {
e.preventDefault(); e.preventDefault();
}; };
screen.ondrop = (e) => { screen.ondrop = (e) => {
let files = e.dataTransfer && e.dataTransfer.files; let files = e.dataTransfer && e.dataTransfer.files;
if(files && files.length == 1) { if(files && files.length == 1) {
e.preventDefault(); e.preventDefault();
runModuleFromURL(URL.createObjectURL(e.dataTransfer.files[0])); runModuleFromURL(URL.createObjectURL(e.dataTransfer.files[0]));
} }
}
runModuleFromHash();
};
let location = window.location;
if(location.hash.length != 0) {
setupLoad();
} else {
(async () => {
let url = location.href;
if(url.endsWith('.html')) {
url = url.slice(0, url.length - 4) + 'uw8';
} else {
if(!url.endsWith('/')) {
url += '/';
}
url += 'cart.uw8';
}
try {
await runModuleFromURL(url, true);
} catch(e) {
setupLoad();
}
})();
} }

View File

@@ -82,3 +82,16 @@ button:active {
height: 720px; height: 720px;
} }
} }
@media (width:640px) and (height:480px) {
#screen {
width: 640px;
height: 480px;
border: 0;
margin: 0;
}
body {
overflow: hidden;
}
}