-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathweb
More file actions
executable file
·216 lines (191 loc) · 6.59 KB
/
web
File metadata and controls
executable file
·216 lines (191 loc) · 6.59 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/bin/sh
# title: dmenu_websearch <http://efe.kim/dmenu_websearch.html>
# license: CC0
# author: Sunur Efe Vural <efe@efe.kim>
# version: Mar 22, 2019
# dependencies: dmenu, xdotool, hexdump, xprop, setxkbmap, coreutils.
# A browser-independent address bar with bookmark support. When the
# cursor is on a web browser it acts as the address bar of that browser.
# browser='qutebrowser --target tab'
browser="${BROWSER:-helium-browser}"
engine='https://google.com/search?q=%s'
bookmarks="$HOME/projects/notas/pessoal/bookmarks.md"
bulk_actions="$HOME/projects/notas/pessoal/bulk-bookmarks.txt"
# Parse command line flags
bulk_mode=false
case "$1" in
-b|--bulk) bulk_mode=true ;;
esac
gotourl() {
if [ "$nbrowser" = surf ]
then
xprop -id "$winid" -f _SURF_GO 8s -set _SURF_GO "$choice"
elif [ -n "$winid" ] && [ -z "$nbrowser" ]
then
#change layout to us cuz xdotool spasms with non-latin layouts
layout=$(setxkbmap -query | awk '/^layout:/{ print $2 }')
setxkbmap -layout us
xdotool key --clearmodifiers "$shortcut"\
type --clearmodifiers --delay 2 "$choice"
xdotool key --clearmodifiers Return
setxkbmap -layout "$layout"
elif [ "$nbrowser" = eureciclo ]
then
qutebrowser --basedir ~/eureciclo/qutebrowser-work "$choice"
elif [ "$nbrowser" = call ]
then
qutebrowser --basedir ~/eureciclo/web-call "$choice"
elif [ -n "$nbrowser" ]
then
$nbrowser "$choice"
else $browser "$choice"
fi
}
# Open multiple URLs in bulk mode
gotourls() {
for url in "$@"; do
choice="$url"
gotourl
done
}
# Parse bulk actions file and extract URLs for selected action
get_bulk_urls() {
action="$1"
in_action=false
while IFS= read -r line || [ -n "$line" ]; do
# Skip empty lines and comments
[ -z "$line" ] && continue
[ "${line#\#}" != "$line" ] && continue
# Check if line starts with whitespace (URL) or not (action name)
if echo "$line" | grep -q '^[[:space:]]'; then
# This is a URL line
if [ "$in_action" = true ]; then
# Trim leading and trailing whitespace, remove control characters
url=$(printf '%s' "$line" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//' | tr -d '\r\n')
[ -n "$url" ] && printf '%s\n' "$url"
fi
else
# This is an action name line
if [ "$line" = "$action" ]; then
in_action=true
else
in_action=false
fi
fi
done < "$bulk_actions"
}
searchweb() {
#convert search query to percent encoding and insert it into url
choice=$(echo "$choice" | hexdump -v -e '/1 " %02x"')
choice=$(echo "$engine" | sed "s/%s/${choice% 0a}/;s/[[:space:]]/%/g")
gotourl
}
xprop -root | grep '^_NET_ACTIVE_WINDOW' && {
winid=$(xprop -root _NET_ACTIVE_WINDOW | sed 's/.*[[:space:]]//')
class=$(xprop -id "$winid" WM_CLASS | awk -F'\"' '{ print $2 }')
case "$class" in
Firefox) nbrowser='firefox' ;;
Helium) nbrowser='helium-browser' ;;
eureciclo) nbrowser='eureciclo';;
call) nbrowser='call';;
#Firefox) shortcut='ctrl+l' ;; # alternative method, uses xdotool
IceCat) nbrowser='icecat' ;;
Chromium) nbrowser='chromium' ;;
Chrome) nbrowser='chrome' ;;
Opera) nbrowser='opera' ;;
Vivaldi) nbrowser='vivaldi' ;; # not tested
Brave-browser) nbrowser='brave' ;; # not tested
Navigator) nbrowser='zen-browser' ;; # not tested
Thorium-browser) nbrowser='thorium-browser' ;; # not tested
Conkeror) nbrowser='conkeror' ;; # not tested
Palemoon) nbrowser='palemoon' ;; # not tested
Iceweasel) nbrowser='iceweasel' ;; # not tested
qutebrowser) nbrowser='qutebrowser' ;;
Midori) nbrowser='midori' ;; # not that good
Luakit) nbrowser='luakit' ;; # uses the last window instance
Uzbl|Vimb) shortcut='o' ;;
Links) shortcut='g' ;;
Netsurf*|Epiphany|Dillo|Konqueror|Arora) shortcut='ctrl+l' ;;
Surf) nbrowser='surf' ; uricur=$(xprop -id "$winid" _SURF_URI |\
awk -F'\"' '{ print $( NF - 1 ) }') ;;
*) pid=$(xprop -id "$winid" _NET_WM_PID | awk '{ print $3 }')
while pgrep -oP "$pid" >/dev/null
do
pid=$(pgrep -oP "$pid")
done
pname=$(awk '/^Name\:/{ print $NF }' /proc/"$pid"/status) ||
winid="" ;;
esac
[ -n "$pname" ] && case "$pname" in
w3m) shortcut="U" ;;
lynx|elinks|links) shortcut="g" ;;
*) winid="" ;;
esac
}
# Bulk mode: show actions and open multiple URLs
if [ "$bulk_mode" = true ]; then
if [ ! -f "$bulk_actions" ]; then
echo "Error: bulk actions file not found: $bulk_actions" >&2
exit 1
fi
# Extract action names (non-indented lines that aren't comments or empty)
tmpfile=$(mktemp /tmp/dmenu_bulk_actions.XXXXXX)
urlfile=$(mktemp /tmp/dmenu_bulk_urls.XXXXXX)
trap 'rm -f "$tmpfile" "$urlfile"' 0 1 15
while IFS= read -r line; do
[ -z "$line" ] && continue
[ "${line#\#}" != "$line" ] && continue
# Only include lines that don't start with whitespace (action names)
echo "$line" | grep -q '^[[:space:]]' || echo "$line"
done < "$bulk_actions" > "$tmpfile"
[ ! -s "$tmpfile" ] && echo "Error: no actions found in $bulk_actions" >&2 && exit 1
choice=$(dmenu -W 350 -p "Bulk Action:" -l 20 < "$tmpfile") || exit 1
# Get URLs for selected action and store in temp file
get_bulk_urls "$choice" > "$urlfile"
if [ ! -s "$urlfile" ]; then
echo "Error: no URLs found for action: $choice" >&2
exit 1
fi
# Clear winid for bulk mode to ensure each URL opens in a new tab/window
# instead of trying to type into the active window's address bar
winid=""
shortcut=""
# Open all URLs simultaneously in background
while IFS= read -r url || [ -n "$url" ]; do
# Trim all leading and trailing whitespace, including newlines and control characters
url=$(printf '%s' "$url" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//' | tr -d '\r\n')
[ -z "$url" ] && continue
choice="$url"
gotourl &
done < "$urlfile"
# Wait a moment for all browser processes to start
sleep 0.1
exit 0
fi
# Normal mode: single URL/bookmark selection
tmpfile=$(mktemp /tmp/dmenu_websearch.XXXXXX)
trap 'rm "$tmpfile"' 0 1 15
printf '%s\n%s\n' "$uricur" "$1" > "$tmpfile"
cat "$bookmarks" >> "$tmpfile"
sed -i -E '/^(#|$)/d' "$tmpfile"
choice=$(dmenu -W 350 -p "Go:" -l 20 < "$tmpfile") || exit 1
# Detect links without protocol (This is WIP)
protocol='^(https?|ftps?|mailto|about|file):///?'
checkurl() {
grep -Fx "$choice" "$tmpfile" &&
choice=$(echo "$choice" | awk -F '- ' '{ print $2 }') && return 0
[ ${#choice} -lt 4 ] && return 1
echo "$choice" | grep -Z ' ' && return 1
echo "$choice" | grep -EiZ "$protocol" && return 0
echo "$choice" | grep -FZ '..' && return 1
prepath=$(echo "$choice" | sed 's/(\/|#|\?).*//')
echo "$prepath" | grep -FvZ '.' && return 1
echo "$prepath" | grep -EZ '^([[:alnum:]~_:-]+\.?){1,3}' && return 0
}
if checkurl
then
echo "$choice" | grep -EivZ "$protocol" &&
choice="http://$choice"
gotourl
else searchweb
fi