Skip to content
odnar-dev edited this page Sep 22, 2021 · 11 revisions

How to add a custom search engine?

  • You need to create a file in .config/nbrowser/engines/ directory with the name of the keyword you want it to be called with.
  • in that file you need to create nbrowser_search() function which receive the search query and then use it to construct an URL

this is a sample example of duckduckgo search engine saved in .config/nbrowser/engines/ddg

nbrowser_search(){
	echo "http://duckduckgo.com/?q=$@"
}

now if i called nbrowser and typed ?ddg music it well open http://duckduckgo.com/?q=music

# pro tips:
- you can symlink that file, and then call the same search engine using different keywords
  like (ddg -> duckduckgo), (git -> github) ...
- you can use parts of the search query as condition to construct different urls
  like if it start with '-p' return pictures url, "-v" for videos, "-n" for news... 

How to add a custom bang?

  • You need to create a file in .config/nbrowser/bangs/ directory with the name of the keyword you want it to be called with.
  • in that file you need to create nbrowser_bang() function which may receive a search query.

this is a sample example of a bang that use ddgr and jq saved in .config/nbrowser/bangs/ddg

nbrowser_bang(){
  # search for query $@ using ddgr and get result in json format
  ddg_result_json=$(ddgr --json "$@")
  # parse results with jq and show a menu to let the user choose,
  # then extract the url from selected line using awk
  ddg_result_url=$(printf '%s' "${ddg_result_json}" | jq -r '.[]| "\(.title)  --  \(.url)"' \
                 | _choose | awk '{print $NF}' )
  # if no result has been selected exit
  [ -z "$ddg_result_url" ] && exit 0
  # open selected URL
  url_handler "$ddg_result_url"
}

now if i called nbrowser and typed !ddg music it well search ddg for the query music and then open a menu of results you can choose from

# pro tips:
- bangs don't necessary need search query; so you may use that as condition to run different functions
- bangs are differnt from engines you can make them do anything you want
- bangs and `nbrowser` share the same shell environment, so you can read variables or
  call any function available in `nbrowser`.

## functions
  - `has` : to check for installed dependencies. (has "arg")
  - `_pemx` : show an error msg and exit. (_pemx "msg")
  - `_notify` : show an msg. (_notify "msg")
  - `_choose` : show a menu and print the selected line. (echo "arg" | _choose)
  - `_clean_url` : remove tracking parameters from url (_clean_url "url")
  - `_copy_to_clipboard` : copy to clipboard (_copy_to_clipboard "text")
  - `open_video_with` : show menu to open video with. (open_video_with "url")
  - `open_picture_with` : show menu to open picture with. (open_picture_with "url")
  - `open_in_browser` : show menu to open url in browser. (open_in_browser "url")
  - `url_handler` : clean url then open with. (url_handler "url")

## variables
  $NBROWSER_DEFAULT_SEARCH : the name of the default search engine
  + any variable from the config file ".config/nbrowser/config"
Clone this wiki locally