|
> This is what StackOverflow tells me (2020) Web Workers can't directly access the DOM in JavaScript either. This is not a WebAssembly problem. If you want a Web Worker to manipulate your document, you're going to post events back and forth to the main thread, and Web Assembly could call imported functions to do that too. I don't even know what he's on about with Preact/React... Save the following as "ergonomic.html" and you'll see that WebAssembly is manipulating the DOM. <!doctype html><title>Not that hard</title>
<script type="module">
document.addEventListener('DOMContentLoaded', () => {
/* Compile this module with wat2wasm to make the binary below:
(module
(import "env" "easy" (func $easy (param i32)))
(func $run (param) (result)
(call $easy (i32.const 123))
(call $easy (i32.const 456))
)
(memory $mem 1)
(export "run" (func $run))
(export "mem" (memory $mem))
)
*/
const binary = new Uint8Array([
0, 97, 115, 109, 1, 0, 0, 0,
1, 8, 2, 96, 1, 127, 0, 96,
0, 0, 2, 12, 1, 3, 101, 110,
118, 4, 101, 97, 115, 121, 0, 0,
3, 2, 1, 1, 5, 3, 1, 0,
1, 7, 13, 2, 3, 114, 117, 110,
0, 1, 3, 109, 101, 109, 2, 0,
10, 14, 1, 12, 0, 65, 251, 0,
16, 0, 65, 200, 3, 16, 0, 11,
]);
const imports = {
easy(arg) {
const div = document.createElement("div");
div.textContent = "DOM this: " + String(arg);
document.body.appendChild(div);
}
};
const module = new WebAssembly.Module(binary);
const instance = new WebAssembly.Instance(module, { env: imports });
instance.exports.run();
});
</script>
That `easy(arg)` function could do much more elaborate things, and you could pass lots of data in and out using the memory export.I'd like to believe a simple standalone example like this would be enough to get people to shutup about the DOM thing, but I know better. It'll be the same people who think you need to link with all of SDL in an Emscripten project in order to draw a line on a canvas. > This feels like "we can have DOM access at home" meme. And I'm sure somebody (maybe you) will try to move the goal posts and claim some other meme applies. |
Around 10 years ago, I was having lunch in a food court and overheard "Luckily I don't have to use javascript, just jquery".
Around 5 years ago, a co-worker admitted he still had issues distinguishing what functionality was python and what came from Django (web framework), despite having used them both daily for years. He thought it was because he learned both at the same time.
I wouldn't be surprised if this was more of the same, and just getting worse as we pile more and more abstractions on top.