diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-08-04 22:36:56 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-08-04 22:36:56 +0530 |
| commit | c7e105ee4254d00f591c4c002f32c223f4c2448a (patch) | |
| tree | 17ebadd5dc1a5e148c4b2f5ed1c70b7c7e54fe1a /src/LuaRuntimeApi.hpp | |
| parent | 0d1258aee3f39c8d5348588a87e804ec8cd27bd5 (diff) | |
| download | null-browser-c7e105ee4254d00f591c4c002f32c223f4c2448a.tar.gz null-browser-c7e105ee4254d00f591c4c002f32c223f4c2448a.zip | |
Add on_result callback for web.view.run_js
Diffstat (limited to 'src/LuaRuntimeApi.hpp')
| -rw-r--r-- | src/LuaRuntimeApi.hpp | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/src/LuaRuntimeApi.hpp b/src/LuaRuntimeApi.hpp index 0af1e9e..d6a78d5 100644 --- a/src/LuaRuntimeApi.hpp +++ b/src/LuaRuntimeApi.hpp @@ -49,9 +49,30 @@ int lua_api_view_set_html(lua_State *state) { int lua_api_view_run_js(lua_State *state) { const char *js_code = lua_tostring(state, 1); - WebViewId view_id = lua_isnoneornil(state, 2) ? 0 : lua_tointeger(state, 2); + + lua_getfield(state, 2, "view"); + WebViewId view_id = lua_isnoneornil(state, 3) ? 0 : lua_tointeger(state, 3); + lua_pop(state, 1); + + lua_getfield(state, 2, "on_result"); + JsOnResultFunc action = [](auto &) {}; + if (lua_isfunction(state, 3)) { + lua_pushvalue(state, 3); + const int function_ref = luaL_ref(state, LUA_REGISTRYINDEX); + action = [state, function_ref](const std::optional<QVariant> &value) { + preserve_top(state, { + lua_rawgeti(state, LUA_REGISTRYINDEX, function_ref); + LuaRuntime::push_qvariant(state, value); + lua_pcall(state, 1, 0, 0); + luaL_unref(state, LUA_REGISTRYINDEX, function_ref); + }) + }; + } + lua_pop(state, 1); + auto &runtime = LuaRuntime::instance(); - emit runtime.webview_js_eval_requested(js_code, view_id); + emit runtime.webview_js_eval_requested(js_code, view_id, action); + return 1; } |
