You are not logged in.

#1 2017-08-01 16:33:27

beppo
Member
Registered: 2017-08-01
Posts: 26

websearch with dmenu

Hi guys this is my first post after playing with Arch Linux for about a week.

I'm currently running the i3wm and have a question about  dmenu. To be precise I'd like to include a websearch to the dmenu.

I came across this post from 2010 where Docbroke posted a promising script.

#!/bin/bash

    ## requires surfraw, add below to your .bashrc
    ## PATH=/usr/lib/surfraw:${PATH}
    ## default script uses rofi, if you use dmenu just replace "rofi -dmenu" with "dmenu", you can simply mask that line and unmask the next line with dmenu
    ## put your browser command in BROWSER=<-->, defaults to chromium
    ## put your bookmarks & history file in appropriate place, defaults to w3m bookmarks and history file
    ## EXAMPLE
    ## "youtube movie trailers" to search movie trailers ay youtube.com
    ## "youtube.com" to open default youtube page
    ## similarly "google world war 2" , "wiki world war", "aur telegram-bin", "awiki mutt", "pubmed giant cell tumor" etc.

    BROWSER=chromium
    BOOKMARKS=~/.w3m/bookmark.html
    HISTORY=~/.w3m/history
    
    grep -o 'http[^"]*' $BOOKMARKS > /tmp/bookmarks
    grep -o 'http[^"]*' $HISTORY >> /tmp/bookmarks
    cat /tmp/bookmarks | rofi -dmenu -location 6 -lines 10 -width 100 -p $BROWSER: > /tmp/x
    # cat /tmp/bookmarks | dmenu -p $BROWSER: > /tmp/x
    
    check=$(wc -l < /tmp/x)
    (( check == 0)) && exit

    awk '{ print $1 }' /tmp/x > /tmp/y
    awk '{ print $2, $3, $4, $5, $6, $7, $8 }' /tmp/x > /tmp/z
    
    grep -o http /tmp/x && $BROWSER  $(cat /tmp/x) && exit
    grep -o "www\." /tmp/x && $BROWSER $(cat /tmp/x) && exit
    grep -o "\.com" /tmp/x && $BROWSER $(cat /tmp/x) && exit
    grep -o "\.in" /tmp/x && $BROWSER $(cat /tmp/x) && exit
    grep -o "\.org" /tmp/x && $BROWSER $(cat /tmp/x) && exit

    grep -o awiki /tmp/y && archwiki -browser=$BROWSER $(< /tmp/z) && exit
    grep -o wiki /tmp/y && wikipedia -browser=$BROWSER  $(< /tmp/z) && exit
    grep -o imdb /tmp/y && imdb -browser=$BROWSER $(< /tmp/z) && exit
    grep -o aur /tmp/y && aur -browser=$BROWSER $(< /tmp/z) && exit
    grep -o pkg /tmp/y && archpkg -browser=$BROWSER $(< /tmp/z) && exit
    grep -o google /tmp/y && google -browser=$BROWSER $(< /tmp/z) && exit
    grep -o dict /tmp/y && webster -browser=$BROWSER $(< /tmp/z) && exit
    grep -o dictu /tmp/y && urban -browser=$BROWSER $(< /tmp/z) && exit
    grep -o torrent /tmp/y && piratebay -browser=$BROWSER $(< /tmp/z) && exit
    grep -o pubmed /tmp/y && pubmed -browser=$BROWSER $(< /tmp/z) && exit
    grep -o youtube /tmp/y && youtube -browser=$BROWSER $(< /tmp/z) && exit

    S -browser=$BROWSER $(cat /tmp/x)

So I installed surfraw, added the path into .bashrc, replaced rofi -dmenu with dmenu and changed the browser to firefox, now my main problem is: Where do I put the script?

Greetings!

Offline

#2 2017-08-01 17:19:16

null
Member
Registered: 2009-05-06
Posts: 393

Re: websearch with dmenu

Where ever you want. If you want it to be executable 'chmod +x' it, if you want to execute it just by entering its name somewhere put it in some folder in you $PATH (or rather into ~/.bin or whatever it put this folder in your $PATH variable). If you want to start it by some key combination just put 'bash /path/to/the/script' somewhere into your i3wm config.

Offline

#3 2017-08-02 09:46:14

beppo
Member
Registered: 2017-08-01
Posts: 26

Re: websearch with dmenu

For dmenu to reognize the script the solution was to create a symlink into /usr/bin.

Offline

#4 2017-08-02 10:10:42

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,372

Re: websearch with dmenu

Dmenu don't need to recognize the script, it is called in when you run the script. Just follow the suggestions given above by Null. The script you have posted is actually very early and poor version, I had posted the update somewhere later on, (after getting inputs from Escwartz, Trilby, Alad, Awebb etc.,). For ease I am just reposting it here,

#!/bin/bash

INFO() {
    cat <<EOF>&2
### DEPENDS ON ###
     "surfraw"-for keyword based search, you will need /usr/lib/surfraw in your path, so add below line to your .bashrc (uncommented)
     PATH=/usr/lib/surfraw:${PATH}
### CONFIGURATION ###
     place your browser in BROWSER=? line
     only if not using w3m 
	# you can use your bookmarks text file with 1 url/line, put it in second "BOOKMARKS=?" field replacing /tmp/bookmarks, comment out first "BOOKMARKS=?" line & grep -o line
     w3m users do not need to change BOOKMARKS=?
     default script uses rofi, to use dmenu uncomment the line starting with dmenu and comment out previous line starting with rofi
### USE ###
     enter text in rofi/dmenu field, it will be matched against bookmarks, <enter> to open selected bookmark
     to search specific sites use keyword as first argument ( like go for google, wi for wikipedia, aw for archwiki etc.) > read the script to find out all keywords
     if no keyword or url is used, entire argument will be searched with duckduckgo
     to use enterd text directly (not the matching bookmark) use <C-enter> (rofi only)
     to edit selected bookmark use <C-space> (rofi only)
EOF
}

while getopts h: help; do
    case $help in
    h|\?) INFO && exit ;;
esac
done

shopt -s lastpipe

#echo -e "w3m\nfirefox" | rofi -dmenu -location 1 -l 10 -width 100 -font "Inconsolata 14" -p "which browser?"| read BROWSER

#[[ ! $BROWSER ]] && exit
#if [[ $BROWSER = w3m ]]; then st w3menu && exit ; fi

BROWSER=firefox
# only for w3m bookmarks/ comment out if not using w3m
# BOOKMARKS=~/.w3m/bookmark.html
BOOKMARKS=~/.config/lariza/bookmarks
#HISTORY=~/.config/lariza/history

# convert .w3m/bookmarks to simple text with 1 url/line; comment out if not using w3m
#grep -o 'http[^"]*' $BOOKMARKS > /tmp/bookmarks
cat $BOOKMARKS > /tmp/bookmarks
# add w3m history to the list of bookmarks
# grep -o 'http[^"]*' $HISTORY >> /tmp/bookmarks
#cat $HISTORY >> /tmp/bookmarks
# add your plain text bookmarks here (do not edit or comment out if using w3m bookmarks), you can add tag after the bookmark bookmart url e.g., "www.google.co.in search googlesearch", use lariza-external-handler for adding bookmarks
BOOKMARKS=/tmp/bookmarks
# use rofi to display bookmarks and select one
rofi -dmenu -location 1 -l 10 -width 100 -font "Inconsolata 14" -p $BROWSER: < "$BOOKMARKS" | read -a "url"
# use dmenu inplace of rofi
#/usr/bin/dmenu -l 10 -fn "Inconsolata 14" -p $BROWSER: < "$BOOKMARKS" | read -a url

[[ ! $url ]] && exit

duckimage() {
x=$@
$BROWSER "https://duckduckgo.com/?q=${x// /+}&ia=images&iax=1"
}
duckvideo() {
x=$@
$BROWSER "https://duckduckgo.com/?q=${x// /+}&ia=videos&iax=1"
}
DDG() {
x=$@; $BROWSER "https://duckduckgo.com/?q=${x// /+}"
}
MED() {
x=$@; $BROWSER "http://medical-dictionary.thefreedictionary.com/${x// /+}"
}

case "${url[0]}" in
    *.*|*:*|*/*)		     $BROWSER "${url[0]}" ;;
    aw|awiki)   archwiki    -browser=$BROWSER "${url[@]:1}" ;;
    wi|wiki)    wikipedia   -browser=$BROWSER "${url[@]:1}" ;;
    imdb)	imdb        -browser=$BROWSER "${url[@]:1}" ;;
    aur)	aur         -browser=$BROWSER "${url[@]:1}" ;;
    pkg)	archpkg     -browser=$BROWSER "${url[@]:1}" ;;
    ddg|S|DDG)	DDG			      "${url[@]:1}" ;;
    go|google)  google      -browser=$BROWSER "${url[@]:1}" ;;
    map)	google -m   -browser=$BROWSER "${url[@]:1}" ;;
#    image)	google -i   -browser=$BROWSER "${url[@]:1}" ;;   
    image) duckimage "${url[@]:1}" ;;   
#    video)	google -v   -browser=$BROWSER "${url[@]:1}" ;;   
    video) duckvideo "${url[@]:1}" ;;
    news)	google -n   -browser=$BROWSER "${url[@]:1}" ;;      
    yt|youtube)	youtube     -browser=$BROWSER "${url[@]:1}" ;;
    ebay)	ebay        -browser=$BROWSER "${url[@]:1}" ;;
    pubmed)	pubmed      -browser=$BROWSER "${url[@]:1}" ;;
    git|github)	github      -browser=$BROWSER "${url[@]:1}" ;;
    def) notify-send "$( sdcv --data-dir ~/dic/english/ "${url[@]:1}" )" && exit ;;
    guj) notify-send "$( sdcv --data-dir ~/dic/gujarati/ "${url[@]:1}" )" && exit ;;
    hin) notify-send "$( sdcv --data-dir ~/dic/hindi/ "${url[@]:1}" )" && exit ;;
    med)        MED                           "${url[@]:1}" ;;
    *)	if [[ $BROWSER != dillo ]]; then
	duckduckgo -j -browser=$BROWSER "${url[@]}"
	else
	google -browser=$BROWSER "${url[@]}"
	fi ;;
esac

EDIT: I think you were talking about running this script from dmenu. You just need this script in your $PATH. After that you can start this script from dmenu_run. You can add the folder containing this script ( or all your scripts) to your $PATH, just like you did with /usr/lib/surfraw. Alternatively you can put this script somewhere already in your path, like /usr/bin as you just did.

Last edited by Docbroke (2017-08-02 10:26:33)


Arch is home!
cwm rofi weaver clifm vis lizzy pass terminator
https://github.com/Docbroke

Offline

Board footer

Powered by FluxBB