From 208202db9cba6ddf0713a70e7122c69a53b1f2aa Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sat, 19 Apr 2025 21:29:38 +0530 Subject: Add web.search.get_text and prefill current search text --- README.md | 16 ++++++++-------- config.lua | 13 +++++++------ lua/null-browser/api.lua | 6 ++++++ src/LuaRuntimeApi.hpp | 9 +++++++++ src/WindowActionRouter.cpp | 7 +++++-- src/WindowActionRouter.hpp | 1 + 6 files changed, 36 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 61f87e7..5864d87 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,10 @@ ![null-browser banner](./media/banner.png) # null-browser [WIP] - -A simple, fast web browser based on qtwebengine with a lua runtime. +A simple, fast web browser built using [qtwebengine (chromium)](https://wiki.qt.io/QtWebEngine), that is configured and extended using lua. ## What does it do? -This web browser is just a stack of web views that can be controlled using lua. -Don't expect a full featured browser, expect the opposite. +This web browser is just a stack of web views that can be controlled using lua. Don't expect a full featured web browser, expect the **opposite**. ## What does it not do? How can I do it? - **No url bar** @@ -19,7 +17,9 @@ Don't expect a full featured browser, expect the opposite. - save it in a file on UrlChanged event and show as completion for your url input - same as above but in a sqlite db instead - **No splits** - - use window manager + - use a window manager +- **No tab ui** + - use a window manager - use [tabbed (X11)](https://tools.suckless.org/tabbed/) - **No buttons** - define key bindings @@ -27,12 +27,12 @@ Don't expect a full featured browser, expect the opposite. - filter host-names - **No settings page** - write some lua -- **No chrome extensions** +- **No chromium/web extensions** - inject javascript into pages from your lua config - **No built-in cryptocurrency** - - it's a web browser. Add it if you want it + - add it if you want it - **No built-in AI** - - it's a web browser. Add it if you want it + - add it if you want it Any feature you want, you implement yourself. diff --git a/config.lua b/config.lua index 145d7a7..50f4fe4 100644 --- a/config.lua +++ b/config.lua @@ -1,5 +1,8 @@ print('hello starting up...') +local dmenu = require 'null-browser.extras.dmenu' +local history = require 'null-browser.extras.history' + --- @type table web = web --- @type table @@ -19,9 +22,6 @@ web.set('close_window_when_no_views', true) web.set('user_agent', 'MacOS | Safari - $500 edition') web.set('downloads_dir', os.getenv('HOME') .. '/Downloads/firefox') -local dmenu = require 'null-browser.extras.dmenu' -local history = require 'null-browser.extras.history' - web.event.add_listener('UrlChanged', { callback = function(opts) print('url change', web.inspect(opts)); @@ -41,12 +41,13 @@ local function to_url(url) return "https://" .. trim(url) end -web.keymap.set('n', '', function() web.search.set_text('') end) +-- Search +web.keymap.set('n', '', function() web.search.reset() end) web.keymap.set('n', 'n', function() web.search.next() end) web.keymap.set('n', '', function() web.search.previous() end) web.keymap.set('n', '', function() - dmenu.input({ prompt = 'Search:' }, function(err, input) - if err or not input then return end + dmenu.input({ prompt = 'Search:', input = web.search.get_text() }, function(err, input) + if err then return end web.search.set_text(input) end) end) diff --git a/lua/null-browser/api.lua b/lua/null-browser/api.lua index dfbfe0f..59a702f 100644 --- a/lua/null-browser/api.lua +++ b/lua/null-browser/api.lua @@ -183,6 +183,12 @@ function web.history.forward(count, view_id) return __internals.history_forward( --- ``` function web.search.set_text(text, view_id) return __internals.search_set_text(text, view_id) end +-- TODO: Documentation please +function web.search.reset(view_id) return __internals.search_set_text('', view_id) end + +-- TODO: Documentation please +function web.search.get_text() return __internals.search_get_text() end + -- TODO: Documentation please function web.search.next(view_id) return __internals.search_next(view_id) end diff --git a/src/LuaRuntimeApi.hpp b/src/LuaRuntimeApi.hpp index ff2f46c..3b67f36 100644 --- a/src/LuaRuntimeApi.hpp +++ b/src/LuaRuntimeApi.hpp @@ -4,6 +4,7 @@ #include "LuaRuntime.hpp" #include "WindowActionRouter.hpp" +#include "lua.h" int lua_api_view_set_url(lua_State *state) { const char *url = lua_tostring(state, 1); @@ -216,6 +217,13 @@ int lua_api_view_open_devtools(lua_State *state) { return 1; } +int lua_api_search_get_text(lua_State *state) { + auto &router = WindowActionRouter::instance(); + auto search_text = router.fetch_current_search_text(); + lua_pushstring(state, search_text.toStdString().c_str()); + return 1; +} + // NOLINTNEXTLINE static luaL_Reg internals_api[] = { luaL_Reg{"event_add_listener", &lua_event_register}, @@ -230,6 +238,7 @@ static luaL_Reg internals_api[] = { luaL_Reg{"view_set_url", &lua_api_view_set_url}, luaL_Reg{"history_back", &lua_history_back}, luaL_Reg{"history_forward", &lua_history_forward}, + luaL_Reg{"search_get_text", &lua_api_search_get_text}, luaL_Reg{"search_set_text", &lua_api_search_set_text}, luaL_Reg{"search_previous", &lua_api_search_previous}, luaL_Reg{"search_next", &lua_api_search_next}, diff --git a/src/WindowActionRouter.cpp b/src/WindowActionRouter.cpp index 9cc9f71..05d0218 100644 --- a/src/WindowActionRouter.cpp +++ b/src/WindowActionRouter.cpp @@ -61,8 +61,9 @@ void WindowActionRouter::initialize(Configuration *config) { connect(&runtime, &LuaRuntime::search_requested, this, [this](const QString &text, WebViewId webview_id) { WITH_WEBVIEW_WINDOW(webview_id, window, { - this->current_search_text = text; - win_match.second->mediator()->set_search_text(text, webview_id, true); + auto *mediator = win_match.second->mediator(); + this->current_search_text = text.trimmed(); + mediator->set_search_text(this->current_search_text, webview_id, true); }) }); connect(&runtime, &LuaRuntime::search_next_requested, this, [this](WebViewId webview_id) { @@ -136,3 +137,5 @@ QList WindowActionRouter::fetch_webview_data_list(WindowId win_id) } return {}; } + +QString WindowActionRouter::fetch_current_search_text() const { return current_search_text; } diff --git a/src/WindowActionRouter.hpp b/src/WindowActionRouter.hpp index c1e6ecc..65124ef 100644 --- a/src/WindowActionRouter.hpp +++ b/src/WindowActionRouter.hpp @@ -35,6 +35,7 @@ public: WebViewId fetch_current_view_id(WindowId win_id = 0); QList fetch_webview_data_list(WindowId win_id = 0); QVariant fetch_config_value(const QString &key); + QString fetch_current_search_text() const; DELEGATE((&event_queue), dispatch_event, dispatch_event); DELEGATE((&event_queue), register_event, register_event) -- cgit v1.3.1