mirror of
https://github.com/exoticorn/microw8.git
synced 2026-01-20 11:16:42 +01:00
implement "itch.io support"
This commit is contained in:
@@ -12,7 +12,7 @@
|
||||
<div id="centered">
|
||||
<canvas id="screen" width="320" height="240"></canvas>
|
||||
<div id="message"></div>
|
||||
<button id="cartButton">Load cart...</button>
|
||||
<button id="cartButton" style="visibility:hidden">Load cart...</button>
|
||||
</div>
|
||||
<div id="footer">
|
||||
<a href="http://unlicense.org/" ref="license">Unlicense</a>
|
||||
|
||||
@@ -221,7 +221,12 @@ async function runModule(data, 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() {
|
||||
@@ -238,28 +243,56 @@ function runModuleFromHash() {
|
||||
}
|
||||
|
||||
window.onhashchange = runModuleFromHash;
|
||||
runModuleFromHash();
|
||||
|
||||
document.getElementById('cartButton').onclick = () => {
|
||||
let fileInput = document.createElement('input');
|
||||
fileInput.type = 'file';
|
||||
fileInput.accept = '.wasm,.uw8,application/wasm';
|
||||
fileInput.onchange = () => {
|
||||
if (fileInput.files.length > 0) {
|
||||
runModuleFromURL(URL.createObjectURL(fileInput.files[0]));
|
||||
}
|
||||
let setupLoad = () => {
|
||||
let loadButton = document.getElementById('cartButton');
|
||||
loadButton.style = '';
|
||||
loadButton.onclick = () => {
|
||||
let fileInput = document.createElement('input');
|
||||
fileInput.type = 'file';
|
||||
fileInput.accept = '.wasm,.uw8,application/wasm';
|
||||
fileInput.onchange = () => {
|
||||
if (fileInput.files.length > 0) {
|
||||
runModuleFromURL(URL.createObjectURL(fileInput.files[0]));
|
||||
}
|
||||
};
|
||||
fileInput.click();
|
||||
};
|
||||
fileInput.click();
|
||||
};
|
||||
|
||||
screen.ondragover = (e) => {
|
||||
e.preventDefault();
|
||||
};
|
||||
|
||||
screen.ondrop = (e) => {
|
||||
let files = e.dataTransfer && e.dataTransfer.files;
|
||||
if(files && files.length == 1) {
|
||||
screen.ondragover = (e) => {
|
||||
e.preventDefault();
|
||||
runModuleFromURL(URL.createObjectURL(e.dataTransfer.files[0]));
|
||||
};
|
||||
|
||||
screen.ondrop = (e) => {
|
||||
let files = e.dataTransfer && e.dataTransfer.files;
|
||||
if(files && files.length == 1) {
|
||||
e.preventDefault();
|
||||
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();
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
|
||||
@@ -82,3 +82,16 @@ button:active {
|
||||
height: 720px;
|
||||
}
|
||||
}
|
||||
|
||||
@media (width:640px) and (height:480px) {
|
||||
#screen {
|
||||
width: 640px;
|
||||
height: 480px;
|
||||
border: 0;
|
||||
margin: 0;
|
||||
}
|
||||
body {
|
||||
overflow: hidden;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user