website example

This commit is contained in:
Jorijn van der Graaf 2025-11-12 17:50:58 +01:00
commit c4f764e4ce
6 changed files with 198 additions and 182 deletions

View file

@ -42,10 +42,29 @@ function writeStringToWasm(str) {
return ptr;
}
function getElementById(id, idLenght) {
const obj = document.getElementById(decoder.decode(new Int8Array(window.crafter_webbuild_wasi.instance.exports.memory.buffer, id, idLenght)));
jsmemory.set(++memorycounter, obj);
return memorycounter;
function getElementById(id, idLength) {
try {
// Decode the id from WASM memory
const elementId = decoder.decode(
new Int8Array(window.crafter_webbuild_wasi.instance.exports.memory.buffer, id, idLength)
);
// Attempt to get the element
const obj = document.getElementById(elementId);
// Check if element exists
if (!obj) {
throw new Error(`Element with id "${elementId}" does not exist.`);
}
// Store in jsmemory
jsmemory.set(++memorycounter, obj);
return memorycounter;
} catch (err) {
console.error(err);
return 0;
}
}
function setInnerHTML(ptr, html, htmlLenght) {