diff options
Diffstat (limited to 'surf.sh')
| -rwxr-xr-x | surf.sh | 135 |
1 files changed, 135 insertions, 0 deletions
@@ -0,0 +1,135 @@ +#!/bin/bash + +################################# +# This is /home/miguel/bin/surf # +################################# + +# TODO: bookmarks with %s to fill in selection from xclip +# TODO: add bookmark with _NET_WM_NAME + +SURF=/home/miguel/git/surf/surf +TABBED=/home/miguel/git/tabbed/tabbed +XDOTOOL=/usr/bin/xdotool +DMENU=/usr/bin/dmenu +BOOKMARKS=/home/miguel/doc/BOOKMARKS +SEARCH_DUCKDUCK="https://www.duckduckgo.com?q=%s" +SEARCH_STARTPAGE="https://www.startpage.com/do/search?query=%s" +SEARCH_GOOGLE="https://www.google.com/search?q=%s" + +BCK="#333" +FOR="#FFF" +SELBCK="#38F" +SELFOR="#FFF" + +SEARCH=$SEARCH_DUCKDUCK + +WM_CLASSNAME=surf-tabbed + +# 4k adjutsment +export GDK_SCALE=2 +export GDK_DPI_SCALE=0.5 + +# try to find a running tabbed with our classname. +# if nothing found, start a new instance and return its id. +get_tabbed_id() +{ + local ID=$($XDOTOOL search -classname ^${WM_CLASSNAME}$) + + if [ -z "$ID" ] + then + ID=$($TABBED -p +999 -t $SELBCK -T $SELFOR -u $BCK -U $FOR -dcn ${WM_CLASSNAME}) + fi + + echo $ID +} + +# get query from dmenu/bookmarks, automatically decide between search or url +# $1 - prompt +# $2 - url (optional) +get_query() +{ + + query_nocut=$(echo -e "$2\n - - - - - - - -" | cat - $BOOKMARKS | $DMENU -i -l 30 -p $1) + query=$(echo $query_nocut | cut -s -f 2 -d '*') + + if [ -z "$query" ] + then + query=$query_nocut + fi + + if [ -z "$query" ] + then + exit 0 + fi + + [[ "$query" =~ \.|/ ]] || query=$(echo $SEARCH | sed "s/%s/${query}/" | tr " " "+" ) + + echo "$query" +} + +#open surf: +# $1 window_id +# $2 OLD - open in existing tab/window +# NEW - open in new tab/indow +# $3 url to open (optional, otherwise dmenu prompt shown to type url) +# +open_surf() +{ + window_id=$1 + target=$2 + open_url=$3 + + if [ "$window_id" -gt 0 ]; then + url=$(xprop -id $window_id _SURF_URI | sed 's/.*\"\(.*\)\".*/\1/') + fi + + if [ $# -eq 2 ]; then + if [ "$target" == "OLD" ]; then + query=$(get_query "surf>" $url) + else + query=$(get_query "SURF>" $url) + fi + elif [ $# -eq 3 ]; then + query=$open_url + else + echo "illegal number of parameters!" + exit -1 + fi + + # TODO! + #query=$(echo $query | sed "s/%s/$(xclip -out)/" | tr " " "+" ) # current url too + + if [ -z "$query" ] + then + exit 0 + fi + + tabbed_id=$(get_tabbed_id) + + echo Opening: [$query] in tabbed [$tabbed_id] with window id [$window_id] + + if [ "$target" == "OLD" ]; then + xprop -id $window_id -f _SURF_GO 8s -set _SURF_GO "${query}" + else + $SURF -N -e $tabbed_id "${query}" > /dev/null 2>&1 & + wmctrl -a $tabbed_id -i + fi +} + +main() +{ + if [ $# -eq 2 ]; then + open_surf $1 $2 + elif [ $# -eq 1 ]; then + open_surf 0 NEW $1 + elif [ $# -eq 0 ]; then + open_surf 0 NEW + else + echo "illegal number of parameters!" + exit -1 + fi +} + +# call our main with all paramters +main $@ + |
