Last active
March 8, 2017 19:42
-
-
Save altercation/3c283a27072ccf72b147b0f838fbffc1 to your computer and use it in GitHub Desktop.
URL Hander
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
#!/bin/sh | |
# return to original window (true) or stay in browser after opening url | |
RETURN_TO_ORIGIN=true | |
# for google chrome beta | |
#PROCESS_NAME=chrome | |
#CLASS_NAME=Google-chrome-beta | |
#BROWSERCMD=google-chrome-beta | |
#SUFFIX=" - Google Chrome" | |
# for chromium | |
PROCESS_NAME=chromium | |
CLASS_NAME=Chromium | |
BROWSERCMD=chromium | |
SUFFIX=" - Chromium" | |
WORKSPACE_ORIGIN="$(xdotool get_desktop)" | |
WINDOW_ORIGIN="$(xdotool getwindowfocus)" | |
CONFIGDIR=${XDG_CONFIG_HOME:-${HOME}/.config} | |
SCRIPTPATH=$(readlink -f $0) | |
main () { | |
# no existing process, just launch a new one | |
#pgrep $PROCESS_NAME &>/dev/null || { $BROWSERCMD $*; exit 0; } | |
pgrep $PROCESS_NAME &>/dev/null || { ($BROWSERCMD $* &); exit 0; } | |
# switch to window if a browser is on the current desktop | |
# otherwise try all desktops | |
switch_to_browser $WORKSPACE_ORIGIN || switch_to_browser | |
$BROWSERCMD $* & | |
# optional return to current workspace | |
if [ $RETURN_TO_ORIGIN = "true" ] | |
then | |
xdotool set_desktop $WORKSPACE_ORIGIN | |
xdotool windowactivate $WINDOW_ORIGIN | |
fi | |
} | |
switch_to_browser () { | |
for WINDOW in $(xdotool search ${1:+--desktop $1}--sync --class "$CLASS_NAME") | |
do | |
WORKSPACE=$(xdotool get_desktop_for_window $WINDOW 2>/dev/null) | |
NAME=$(xdotool getwindowname $WINDOW 2>/dev/null) | |
# only mapped (WS >-1) browser windows ending in our suffix | |
# matter, since they are the actual browsers, not just web apps | |
(( WORKSPACE >= 0 )) && \ | |
[ "$NAME" != "${NAME%$SUFFIX}" ] && \ | |
{ xdotool set_desktop $WORKSPACE; xdotool windowactivate $WINDOW; return 0; } | |
done | |
return 1 | |
} | |
set_default () { | |
cat > $HOME/.local/share/applications/urlhandler.desktop <<EOF | |
[Desktop Entry] | |
Version=1.0 | |
Name=URL Handler | |
GenericName=URL Handler | |
# Gnome and KDE 3 uses Comment. | |
Comment=URL Handler | |
StartupWMClass=$CLASS_NAME | |
Exec=$SCRIPTPATH %U | |
Terminal=false | |
Icon=$BROWSERCMD | |
Type=Application | |
Categories=Network;WebBrowser; | |
Actions=NewWindow;NewPrivateWindow; | |
[Desktop Action NewWindow] | |
Name=New Window | |
StartupWMClass=$CLASS_NAME | |
Exec=$SCRIPTPATH | |
[Desktop Action NewPrivateWindow] | |
Name=New Incognito Window | |
StartupWMClass=$CLASS_NAME | |
Exec=$SCRIPTPATH --incognito | |
MimeType=x-scheme-handler/unknown;x-scheme-handler/about;text/html;text/xml;application/xhtml_xml;image/webp;x-scheme-handler/http;x-scheme-handler/https;x-scheme-handler/ftp; | |
EOF | |
xdg-settings get default-web-browser urlhandler.desktop | |
export BROWSER=$SCRIPTPATH | |
} | |
case ${1:-} in | |
*default) set_default ;; | |
*) main $* ;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment