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.63k stars 157 forks source link

`webui_exit` does not work when using a link to navigate a page #228

Closed ttytm closed 10 months ago

ttytm commented 10 months ago

webui_exit won't work after changing a page through a link, e.g. in the serve a folder example. The window will stay open. It works when programatically changing the page via the backend.

fibodevy commented 10 months ago

Thats because webui.js is not loaded there. When changing the page the websocket connection drops, on the new page if there is no <script src="/webui.js"> then it wont reconnect. WebUI cant control the browser and cannot close it, at this point webui_wait() returns and browser window remains open.

ttytm commented 10 months ago

Tho with the second.html in the mentioned example, webui.js is included on the page.

https://github.com/webui-dev/webui/blob/main/examples/C/serve_a_folder/second.html

ttytm commented 10 months ago

One solution would be to state it as a requirement that page navigation inside the webui application requires a programmatic approach from the backend and adjust the example.

fibodevy commented 10 months ago

Right, in simple link exit doesnt work.

I think this has something to do with bindings. Both buttons call webui_show() which I think re-binds to the DOM elements. If direct link is clicked new page is opened directly by the browser and no re-binding by WebUI is done. Events such as clicks are still captured, but #Exit element is not binded.

fibodevy commented 10 months ago

Found the problem. Bindings are ok, that would be weird coz they are in webui.js..

The problem is that the window cant be closed by script if that script didnt open that window. Security measures. And in this case the script (webui.js) didnt open the window, you did by clicking the link.

m35SevnWWq

Its a bug in the example, not WebUI.

One solution would be to state it as a requirement that page navigation inside the webui application requires a programmatic approach from the backend and adjust the example.

Agree. I always had in mind WebUI is a solution that "loads the page once", and all things must be done dynamically.

ttytm commented 10 months ago

Found the problem. Bindings are ok, that would be weird coz they are in webui.js..

The problem is that the window cant be closed by script if that script didnt open that window. Security measures. And in this case the script (webui.js) didnt open the window, you did by clicking the link.

m35SevnWWq

Good find!

WebUI cant control the browser and cannot close it, at this point webui_wait() returns and browser window remains open.

Hmm, thinking about it, maybe it's possible to control it. WebUI already provides an api to get the process ids, so ending the process on webui_exit should theoretically be possible.

hassandraga commented 10 months ago

Thank you for the report. Fixing this issue led to the creation of a new API.

Now, when using all-events webui_bind(MyWindow, "", events) WebUI then will block all href requests, then the user will receive the event and decide to call webui_navigate() or not.

If no all-events is used, then everything will work as normal.

fibodevy commented 10 months ago

@hassandraga can you check if this new feature, catching all hrefs, works on Firefox? In my case it doesnt, on Chrome & Edge it works.

hassandraga commented 10 months ago

I did not test it in Firefox. I will double-check.