forked from giomatfois62/rofi-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrofi-web-search.sh
executable file
·65 lines (48 loc) · 1.68 KB
/
rofi-web-search.sh
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
#!/usr/bin/env bash
SCRIPT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
ROFI_CMD="rofi -dmenu -i -matching fuzzy"
API=$1
case "$API" in
"google") api_url="https://www.google.com/search?q=" ;;
"youtube") api_url="https://www.youtube.com/results?search_query=" ;;
"wikipedia") api_url="https://en.wikipedia.org/?curid=" ;;
"archwiki") api_url="https://wiki.archlinux.org/index.php?search=" ;;
*) echo "unrecognized API" && exit 1 ;;
esac
# TODO: implement other APIs for suggestions
#SEARCH_URL="https://www.google.com/search?q="
#SEARCH_URL=https://en.wikipedia.org/w/index.php?search=
#SEARCH_URL=https://www.youtube.com/results?search_query=
# detect rofi-blocks and integrate suggestions
have_blocks=`rofi -dump-config | grep blocks`
if [ ${#have_blocks} -gt 0 ]; then
logfile="$HOME/.cache/suggestions.tmp"
blockfile="$SCRIPT_PATH/rofi-web-suggestions.sh"
mkdir -p "${logfile%suggestions.tmp}"
printf "$API" > "$logfile"
rofi -modi blocks -show blocks -blocks-wrap $blockfile -display-blocks $API 2>/dev/null
[ -f $logfile ] && query="$(cat "$logfile")" || exit 1
rm $logfile
if [ ${#query} -gt 0 ]; then
# extract wikipedia page id from string
if [ "$API" = "wikipedia" ]; then
word_count=$(echo "$query" | wc -w)
if [ $word_count -gt 1 ]; then
query=$(echo "$query" | awk '{print $NF}')
else
api_url="https://en.wikipedia.org/w/index.php?fulltext=1&search="
fi
fi
url=$api_url$query
xdg-open "$url"
exit 0
fi
else
query=$((echo) | $ROFI_CMD -p $API);
if [ ${#query} -gt 0 ]; then
url=$api_url$query
xdg-open "$url"
exit 0
fi
fi
exit 1