aboutsummaryrefslogtreecommitdiff
path: root/lua
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2025-08-08 11:29:30 +0530
committerAkshay Nair <phenax5@gmail.com>2025-08-08 11:29:30 +0530
commitb41d47d1f5a9b7ae922830c5bdb83e2f182d1b52 (patch)
treee6bf5749730d9c7a8c55a8be2f54c1c0876e90c9 /lua
parentf9cb560af761e10803128a06599f99bc2292b4be (diff)
downloadnull-browser-b41d47d1f5a9b7ae922830c5bdb83e2f182d1b52.tar.gz
null-browser-b41d47d1f5a9b7ae922830c5bdb83e2f182d1b52.zip
Make hints generic and handleable via lua
Diffstat (limited to 'lua')
-rw-r--r--lua/null-browser/extras/hints.lua53
1 files changed, 44 insertions, 9 deletions
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