add support for load from url

This commit is contained in:
2021-12-19 00:12:26 +01:00
parent 0d0ecdc344
commit a41f9ebe4d
2 changed files with 61 additions and 57 deletions

File diff suppressed because one or more lines are too long

View File

@@ -50,8 +50,7 @@ let keyHandler = (e) => {
mask = 128; mask = 128;
break; break;
case 'KeyR': case 'KeyR':
if(isKeyDown) if (isKeyDown) {
{
runModule(currentData); runModule(currentData);
} }
break; break;
@@ -66,7 +65,7 @@ let keyHandler = (e) => {
window.onkeydown = keyHandler; window.onkeydown = keyHandler;
window.onkeyup = keyHandler; window.onkeyup = keyHandler;
async function runModule(data) { async function runModule(data, keepUrl) {
if (cancelFunction) { if (cancelFunction) {
cancelFunction(); cancelFunction();
cancelFunction = null; cancelFunction = null;
@@ -82,16 +81,17 @@ async function runModule(data) {
currentData = data; currentData = data;
let newURL = window.location.pathname; let newURL = window.location.pathname;
if (cartridgeSize <= 1024) { if (cartridgeSize <= 1024 && !keepUrl) {
let dataString = ''; let dataString = '';
for (let byte of U8(data)) { for (let byte of U8(data)) {
dataString += String.fromCharCode(byte); dataString += String.fromCharCode(byte);
} }
newURL += '#' + btoa(dataString); newURL += '#' + btoa(dataString);
}
if (newURL != window.location.pathname + window.location.hash) { if (newURL != window.location.pathname + window.location.hash) {
history.pushState(null, null, newURL); history.pushState(null, null, newURL);
} }
}
screen.width = screen.width; screen.width = screen.width;
@@ -220,14 +220,18 @@ async function runModule(data) {
} }
} }
async function runModuleFromURL(url) { async function runModuleFromURL(url, keepUrl) {
runModule(await (await fetch(url)).arrayBuffer()); runModule(await (await fetch(url)).arrayBuffer(), keepUrl);
} }
function runModuleFromHash() { function runModuleFromHash() {
let base64Data = window.location.hash.slice(1); let hash = window.location.hash.slice(1);
if (base64Data.length > 0) { if (hash.length > 0) {
runModuleFromURL('data:;base64,' + base64Data); if (hash.startsWith("url=")) {
runModuleFromURL(hash.slice(4), true);
} else {
runModuleFromURL('data:;base64,' + hash);
}
} else { } else {
runModule(new ArrayBuffer(0)); runModule(new ArrayBuffer(0));
} }