webui-dev / webui

Use any web browser or WebView as GUI, with your preferred language in the backend and modern web technologies in the frontend, all in a lightweight portable library.
https://webui.me
MIT License
2.91k stars 167 forks source link

fix: :bug: webui js API is undefined #130

Closed JOTSR closed 1 year ago

JOTSR commented 1 year ago

Issue

image image

Since webui script tag is injected after html, any declared js file in the html payload is parsed before the API. Unless set it "defer" it is executed before the API and cause an error.

I change the html structure order to force js bridge to execute before any user content. Since bridge is loaded in html head I fix possibly undefined listener that crash the app.

Moreover, since webui.c allow to load js via 2 way (embbeded and via a get url) I choose to the the url way to avoid memory management for future improve and simplify the code (and js was truncated after the changes if I keep string concat approach). In bonus it allow to debug js via the source panel in dev console.

Improvements

To force top loading I used the auto correct behaviour of html and then construct bad html to load js bridge before all user content (script is loaded just afet tag that is never close, any added user content will be merge after and js result to be in ). This is more an hot fix and need to be improve by inserting the script tag in html head via an XML/DOM parser.

neroist commented 1 year ago

LGTM