Last active
March 28, 2023 15:49
-
-
Save iul1an/883f5a54b8e4b908168b9008c6e0762e to your computer and use it in GitHub Desktop.
Simple browser selector for Linux, requires yad to display GTK+ dialog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Desktop Entry] | |
Name=Browser Chooser | |
GenericName=Browser Chooser | |
Comment=Select a browser to access the Internet | |
Categories=Network;WebBrowser; | |
Icon=browser | |
Type=Application | |
Exec=browser-chooser.sh --url %u | |
MimeType=application/pdf;application/rdf+xml;application/rss+xml;application/xhtml+xml;application/xhtml_xml;application/xml;image/gif;image/jpeg;image/png;image/webp;text/html;text/xml;x-scheme-handler/http;x-scheme-handler/https; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Variables | |
ARGC=$# | |
ARGV=("$@") | |
URLRE="^(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]\.[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]$" | |
RULES="${HOME}/.config/browser-rules.txt" | |
# Help | |
help() { | |
echo "Usage: $0 [--dry-run] --url URL" | |
exit 1 | |
} | |
# Fail if not enough args provided | |
if [[ $ARGC -lt 2 ]]; then | |
help | |
fi | |
# Parse args | |
for ((idx = 0; idx < ARGC; idx++)); do | |
OPT="${ARGV[idx]}" | |
NEXTOPT="${ARGV[idx + 1]}" | |
case "$OPT" in | |
# URL is next arg only if next arg is a valid URL | |
--url) grep -q -E "$URLRE" <<<"$NEXTOPT" && URL="$NEXTOPT" ;; | |
# When dry run is enabled, print the browse() function call on the screen instead of executing the function | |
--dry-run) CMD=("echo") ;; | |
esac | |
done | |
# Check if a valid URL was provided | |
if [[ -z "$URL" ]]; then | |
echo "Invalid URL or no URL provided" | |
help | |
fi | |
# Try to set browser from rules | |
if [[ -f "$RULES" ]]; then | |
# RULES file has two columns separated by a "|" | |
# 1st column is the URL/keyword, second is browser name | |
MATCH=$(grep -o -i -f <(cut -d "|" -f1 "$RULES") <<<"$URL") | |
# If there's a match, try to set the browser name | |
if [[ -n "$MATCH" ]]; then | |
# If there's multiple rules for the same URL, use the last one | |
BROWSER=$(awk -F'[|]' '$1=="'"${MATCH}"'"{print $2}' "$RULES" | tail -1) | |
fi | |
fi | |
# If there's no rule for provided URL, display browser chooser dialog | |
if [[ -z $BROWSER ]]; then | |
BROWSER=$( | |
yad \ | |
--title="Open URL with" \ | |
--list \ | |
--borders=25 \ | |
--width=300 \ | |
--height=300 \ | |
--buttons-layout=center \ | |
--button=gtk-cancel:1 \ | |
--button="Remember!bookmark_add":20 \ | |
--button=gtk-ok:0 \ | |
--center \ | |
--fixed \ | |
--on-top \ | |
--separator="" \ | |
--column="" \ | |
--no-headers \ | |
"Chrome" \ | |
"Firefox" \ | |
"Brave" \ | |
"Chrome Incognito" \ | |
"Firefox Incognito" \ | |
"Brave Incognito" | |
) | |
# Save yad exit code for later use | |
retval=$? | |
# Exit if "Cancel" button was pressed | |
if [[ -z "$BROWSER" ]]; then | |
exit 0 | |
fi | |
# Remember choice | |
# If yad exit code is "20", then "Remember" button was pressed | |
if [[ $retval -eq 20 ]]; then | |
# Optionally modify the URL to remember the choice for | |
REMEMBER=$( | |
yad \ | |
--title="Edit URL to remember choice for" \ | |
--center \ | |
--fixed \ | |
--on-top \ | |
--entry \ | |
--entry-text="$URL" \ | |
--button=gtk-cancel:1 \ | |
--button=gtk-ok:0 \ | |
--width 600 | |
) || exit 0 # Exit if "Cancel" button was pressed | |
# Save choice | |
echo "${REMEMBER}|${BROWSER}" >>"$RULES" | |
fi | |
fi | |
# Set browser command depending on the browser name | |
browse() { | |
local BROWSER="$1" | |
local URL="$2" | |
case "$BROWSER" in | |
"Chrome") command google-chrome --profile-directory="Default" "$URL" &>/dev/null & disown ;; | |
"Firefox") command firefox "$URL" &>/dev/null 2>&1 & disown ;; | |
"Brave") command brave-browser-stable "$URL" &>/dev/null 2>&1 & disown ;; | |
"Chrome Incognito") command google-chrome --incognito --profile-directory="Default" "$URL" &>/dev/null 2>&1 & disown ;; | |
"Firefox Incognito") command firefox -private-window "$URL" &>/dev/null 2>&1 & disown ;; | |
"Brave Incognito") command brave-browser-stable --incognito "$URL" &>/dev/null 2>&1 & disown ;; | |
*) ;; | |
esac | |
} | |
# Execute browse() function or display function call on the screen if "--dry-run" arg was provided | |
"${CMD[@]}" browse "$BROWSER" "$URL" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Setup instructions
Put
browser-chooser.sh
somewhere in PATH, eg: ~/binSet
browser-chooser.desktop
as the default browser application