From b41d47d1f5a9b7ae922830c5bdb83e2f182d1b52 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Fri, 8 Aug 2025 11:29:30 +0530 Subject: Make hints generic and handleable via lua --- lua/null-browser/extras/hints.lua | 53 ++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 9 deletions(-) (limited to 'lua') diff --git a/lua/null-browser/extras/hints.lua b/lua/null-browser/extras/hints.lua index 33d4564..d6e32c7 100644 --- a/lua/null-browser/extras/hints.lua +++ b/lua/null-browser/extras/hints.lua @@ -2,8 +2,11 @@ local hints = { config = { mode = 'f', }, + action = {}, } +local state = { action = nil } + local js_setup_code = '' function hints.init(on_ready) @@ -24,15 +27,11 @@ function hints.init(on_ready) end) end -function hints.start(selector, new_view) - local open_in_new_view = new_view and 'true' or 'false' +function hints.start(selector, action) + state.action = action or hints.action.open_in_view web.view.run_js( js_setup_code .. - ";_nullbrowser.hints.start('" .. - selector .. - "', " .. - open_in_new_view .. - ")" + ";_nullbrowser.hints.start('" .. selector .. "')" ) web.schedule(function() -- Trigger f mode after tick to avoid the trigger key (f) to get captured in event @@ -42,8 +41,11 @@ end function hints._filter_key(key) web.view.run_js("_nullbrowser.hints.filterOutByKey('" .. key .. "')", { - on_result = function(end_of_matches) - if end_of_matches then hints.stop() end + on_result = function(find_status) + if find_status ~= nil then hints.stop() end + if find_status and state.action then + state.action() + end end, }) end @@ -66,4 +68,37 @@ function hints._load_hints_js(on_ready) end) end +function hints.action.open_in_new_view() + web.view.run_js [[ + const match = _nullbrowser.hints.currentMatch; + if (match.elem?.href) { + window.open(match.elem.href) + } else { + match.elem?.click() + } + ]] +end + +function hints.action.open_in_view() + web.view.run_js [[ + const match = _nullbrowser.hints.currentMatch; + if (match.elem?.href) { + location.href = match.elem.href + } else { + match.elem?.click() + } + ]] +end + +function hints.action.copy_link() + web.view.run_js([[_nullbrowser.hints.currentMatch.elem?.href]], { + on_result = function(url) + local pipe = io.popen('xclip -selection clipboard', 'w') + if not pipe then return end + pipe:write(url) + pipe:close() + end, + }) +end + return hints -- cgit v1.3.1