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.96k stars 172 forks source link

calling a backend function onload results in an uncaught referenceerror #316

Closed sdicker8 closed 8 months ago

sdicker8 commented 8 months ago

Calling a cpp function with window.onload or in a script at the end of the html doc results in an error. This can be fixed with a 500ms delay (setTimeout). Not sure if this is known or expected, but just wanted to let you know.

Nice library - thanks :)

jinzhongjia commented 8 months ago

Are you referring to the error that the websocket is not connected yet?

jinzhongjia commented 8 months ago

ws needs to wait for the connection, and executing it immediately or onload will cause an error Maybe the later js library can add an event queue and execute it after the ws connection is established. @hassandraga

sdicker8 commented 8 months ago

Sorry - two different errors - yes, with window.onload i'm getting websocket is not connected and with the script at the end of the html doc i'm getting uncaught referenceerror: is not defined.

I'm trying to populate the html page with data from a local sqlite database when the app starts - it would be nice if that could happen as quickly as possible - thanks -

jinzhongjia commented 8 months ago

The second mistake you meet is "Uncaught ReferenceError: webui is not defined at...." right?

This is related to the rendering process of the browser. By default, the inline script in HTML will be executed immediately after the HTML is loaded, but webui.js has not been executed yet, which results in the webui variable not being available in the global scope.

jinzhongjia commented 8 months ago

you can use defer for <script>, note that this is only valid for external js files, and the execution order of these js files maintains their relative order, just like a regular script

AlbertShown commented 8 months ago

This is fixed in a commit, it's available in the nightly build. More info in this discussion.

document.addEventListener('DOMContentLoaded', function() {
    // DOM is loaded, and `webui` object should be available.
    webui.setEventCallback((e) => {
        if (e == webui.event.CONNECTED) {
            // Connection to the backend is established
            console.log('Connected.');
        } else if (e == webui.event.DISCONNECTED) {
            // Connection to the backend is lost
            console.log('Disconnected.');
        }
    });
});
sdicker8 commented 8 months ago

@jinzhongjia - if using webui.call then yes, webui is not defined -- i was using the function name. i see now that webui isn't loaded until DOMContentLoaded so this all makes sense. i guess there is no way to establish the bridge before before the page/body is loaded...

@AlbertShown - thanks -- i'll check that out. I guess your event listener must fire right after the one referenced above -