Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing DOM from EMSCRIPTEN

is there a way to access the DOM from a EMSCRIPTEN C++ application?

I'd like e.g., to read / set the value of an html textarea and receive html buttons onclick events.

Can someone provide a C++ snippet?

Thanks.

like image 308
Daniele Pallastrelli Avatar asked Nov 20 '17 15:11

Daniele Pallastrelli


1 Answers

I'll try to answer my own question with the only method I found by now:

this is the html snippet:

<!-- html file -->
...
<input type="text" id="my_textbox" value="...">
...
<input type="submit" value="Submit" onclick="_onBtnPressed()">
...

and this is the C++ code:

// C++ file

// callback for button event
extern "C"
{
    void onBtnPressed() { std::cout << "Btn pressed\n"; }
}

...

// change text of a text box:
emscripten_run_script("document.getElementById('my_textbox').value = 'Hello, emscripten world!'");

Compiled with the flag:

emcc -s EXPORTED_FUNCTIONS="['_onBtnPressed']" ...

This method works. However, I'd expect some explicit emscripten API to directly manipulate the DOM.

like image 96
Daniele Pallastrelli Avatar answered Oct 20 '22 09:10

Daniele Pallastrelli