-
Notifications
You must be signed in to change notification settings - Fork 13
/
url-handler
executable file
·172 lines (158 loc) · 4.54 KB
/
url-handler
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/bin/bash
#
# Based on url_handler.sh for SuSE Linux
#
# Copyright (c) 2000 SuSE GmbH Nuernberg, Germany.
#
# Author: Werner Fink <werner@suse.de>
#
# Subject to the same license it is distributed with by SUSE.
#
#logger -t $0 "$@" / $DISPLAY
exec >&/tmp/url_handler.log
here=$(dirname $0)
# Allow hooks to manipulate "$1" prior to parsing.
HOOK_DIR=".url_handler.d"
source "$ZDOTDIR/lib/libhooks.sh"
while read hook; do
if source "$hook"; then
break
fi
done < <(source $ZDOT_FIND_HOOKS "$HOOK_DIR")
# The while read; do ... done < <() is necessary to
# avoid the while's body running in a subshell.
if [ -z "$1" ]; then
exit 0
fi
url="$1"
method="${1%%:*}"
if test "$url" = "$method" ; then
# no method was specified, try to guess one
case "${url%%.*}" in
www|web|w3) method=http ;;
mail|mailx) method=mailto ;;
gopher) method=gopher ;;
*)
case "${url}" in
*@*)
method=mailto
;;
*)
if [ -e "$url" ]; then
method=file
url="`$here/abs \"$url\"`"
fi
;;
esac
;;
esac
case "$method" in
mailto|file) url="${method}:$url" ;;
*) url="${method}://$url" ;;
esac
fi
### an alternative method of handling "news:*" URL's
#
# if test "$method" = "news" ; then
# url="http://www.deja.com/[ST_rn=if]/topics_if.xp?search=topic&group=${url#news:}"
# method=http
# fi
shift
firefox_handler () {
wmctrl -a firefox
ff_version="`firefox -version`"
echo "ff_version $ff_version"
case "$ff_version" in
*1.5.0.8)
# tested on FC6
firefox -remote "openURL($url,new-tab)" && exit 0 ;;
*1.5.0.*)
firefox "$url" & exit 0 ;;
*2.0*)
firefox -new-tab "$url" && exit 0 ;;
*)
# unset of DESKTOP_STARTUP_ID needed for
# launching browser tabs within skype - go
# figure...
DESKTOP_STARTUP_ID= firefox -remote "openURL($url,new-tab)" && exit 0 ;;
esac
}
# Checks for a named process running as me
process_running_my_uid () {
proc="$1"
pgrep -u "`id -un`" "$@" >/dev/null
}
chrome_handler () {
if process_running_my_uid google-chrome; then
browser=google-chrome
elif process_running_my_uid chrome; then
browser=google-chrome
switch_to=chrome
elif process_running_my_uid brave-browser; then
browser=brave-browser
elif process_running_my_uid chromium; then
browser=chromium
else
browser=chromium
#browser=google-chrome
fi
: ${switch_to:=$browser}
if [ -z "$SSH_CONNECTION" ]; then
if pgrep fluxbox >&/dev/null; then
fluxbox-remote "MacroCmd {NextWindow (Name=$browser) (Head=1)} {Exec focus-active-window}"
elif which wmctrl >&/dev/null; then
# -x searches by WM_CLASS
wmctrl -x -a "$switch_to"
else
echo "WARNING: Didn't find wmctrl on \$PATH" >&2
fi
fi
"$browser" "$1"
}
case "$method" in
ftp)
ftp=ftp
if type -p lftp >& /dev/null ; then
ftp=lftp
elif type -p ncftp >& /dev/null ; then
ftp=ncftp
else
url="${url#ftp://}"
echo "=====> Paste this command by mouse:"
echo cd "/${url#*/}"
url="${url%%/*}"
fi
exec $ftp "$url"
;;
file|http|https|gopher)
if test -n "$DISPLAY"; then
if type -p google-chrome >& /dev/null; then
chrome_handler "$url" && exit 0
fi
if type -p firefox >& /dev/null; then
firefox_handler "$url" && exit 0
fi
# if type -p mozilla >& /dev/null; then
# # FIXME - which mozilla versions?
# mozilla -remote "openURL($url,new-tab)" && exit 0
# fi
fi
type -p links >& /dev/null && links "$url" && exit 0
type -p w3m >& /dev/null && w3m "$url" && exit 0
type -p lynx >& /dev/null && lynx "$url" && exit 0
echo "No HTTP browser found."
read -p "Press return to continue: "
exit 1
;;
mailto)
mailto-handler "$url"
;;
textedit)
lilypond-invoke-editor "$url"
exit 0
;;
*)
echo >&2 "URL type \"$method\" not known"
exit 1
;;
esac