-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsearch
More file actions
executable file
·72 lines (60 loc) · 1.63 KB
/
Copy pathwebsearch
File metadata and controls
executable file
·72 lines (60 loc) · 1.63 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
#!/bin/sh
browser='qutebrowser-profile --menu=dmenu'
engine='https://duckduckgo.com/?q=%s'
# Check for menu flag
while [[ $# -gt 0 ]]; do
case $1 in
--menu)
MENU_TYPE="$2"
shift 2
;;
*)
shift
;;
esac
done
# Set default if not specified
if [[ -z "$MENU_TYPE" ]]; then
MENU_TYPE="rofi"
fi
if [[ "$MENU_TYPE" == "dmenu" ]]; then
menu_cmd="dmenu -c -l 10 -s -p Search"
elif [[ "$MENU_TYPE" == "wmenu" ]]; then
menu_cmd="wmenu -i -p Search"
else
menu_cmd="rofi -dmenu -i -p Search -l 10"
fi
gotourl() {
$browser "$choice"
}
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
}
tmpfile=$(mktemp /tmp/dmenu_websearch.XXXXXX)
trap 'rm "$tmpfile"' 0 1 15
printf '%s\n%s\n' "$uricur" "$1" > "$tmpfile"
sed -i -E '/^(#|$)/d' "$tmpfile"
choice=$(cat "$tmpfile" | $menu_cmd) || exit 1
# Detect links without protocol (This is WIP)
protocol='^(https?|ftps?|mailto|about|file):///?'
checkurl() {
grep -Fx "$choice" "$tmpfile" &&
choice=$(echo "$choice" | awk '{ print $1 }') && 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