blob: 0f2e6dd6f331a74d92f9f6ec6e8d974be1776648 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
#!/bin/bash
set -eu
# shellcheck disable=2034
description="url handler"
FUZL_PROMPT=urlopen
. libfuzl.sh
main() {
case "$#" in
1)
fuzl_action_add "open" "" "open"
fuzl_action_add "ask" "" "ask"
fuzl_action_add "clipboard" "" "clipboard"
fuzl_action_add "primary" "" "primary"
fuzl_action_menu "$(get_text "$*")" || open_default "$(get_text "$*")"
;;
2)
local action
action=${1:?missing action}
shift
eval "urlopen_$action" "'$(get_text "$*")'"
;;
esac
}
#
# helpers
#
DEFAULT_BROWSER=librewolf.desktop
get_text() {
local text="$*"
[ -z "$text" ] && read -r text
printf '%s' "$text"
}
get_mimetype() {
local text=${1:?missing url}
case "$text" in
http*) printf %s 'x-scheme-handler/http' ;;
*) xdg-mime query filetype "$text" 2>/dev/null || file -bE --mime-type "$text" ;;
esac
}
get_x-scheme-handler_override() {
case "$1" in
slack.com) printf %s 'workfox.desktop' ;;
*) return 1 ;;
esac
}
open_default() {
local text=${1:?missing url}
local application
case "$(get_mimetype "$text")" in
x-scheme-handler/http*) application=$DEFAULT_BROWSER ;;
esac
application_override=$(get_x-scheme-handler_override "$text" || :)
if [ -n "${application_override}" ]; then
application=$application_override
fi
if [ -n "${application:-}" ]; then
desktop_file=$(rg -Lil -g "$application" . -- /usr/share/applications/ /var/lib/flatpak/exports/share/applications/ ~/.local/share/applications/ | head -1)
exec gio launch "$desktop_file" "$text"
fi
exec gio open "$text"
}
#
# actions
#
urlopen_ask() {
local text=${1:?missing url}
env porta open-uri "$text" ask
}
urlopen_open() {
local text=${1:?missing url}
open_default "$text"
}
urlopen_clipboard() {
local text=${1:?missing url}
wl-copy -n "$text"
# fuzl_notification "copied to clipboard" "$text"
}
urlopen_primary() {
local text=${1:?missing url}
wl-copy -np "$text"
# fuzl_notification "copied to primary" "$text"
}
main $@
|