Skip to content

Instantly share code, notes, and snippets.

@potatowire
Forked from altercation/browser.sh
Created November 1, 2016 22:51
Show Gist options
  • Save potatowire/3d64f51c2d13ad1cebdd8059fbbdadd9 to your computer and use it in GitHub Desktop.
Save potatowire/3d64f51c2d13ad1cebdd8059fbbdadd9 to your computer and use it in GitHub Desktop.
Per workspace browser launcher
#!/usr/bin/env zsh
setopt EXTENDED_GLOB NO_UNSET
unsetopt NO_MATCH
# launch different browser profiles automatically on any named i3 workspace
# plain numbered workspaces (or those listed in DEFAULT_WS) launch the normal
# browser profile
# expected names for chromium/google-chrome executable
#browsers=(chromium google-chrome google-chrome-${^=:-stable unstable beta dev})
#browsers=(google-chrome${^=:-'' -stable -unstable -beta -dev} chromium)
browsers=(google-chrome-beta google-chrome chromium)
# command line defaults
CLI_OPTS=""
# applying workaround for bug in chrome-dev:
# https://code.google.com/p/chromium/issues/detail?id=474431
CLI_OPTS+=" --enable-unsafe-es3-apis "
# TESTING: move browser cache to tmpfs for performance (?effective?)
# https://wiki.archlinux.org/index.php/Chromium_tweaks#Tmpfs
# CLI_OPTS+=" --disk-cache-dir=/tmp/cache "
# trying again, this time with space limitation (100MB) to avoid eating the world and hanging on cache access
#CLI_OPTS+=" --disk-cache-dir=/tmp/cache --disk-cache-size=104857600 "
# adding simple cache flag to see if that helps cache hang issue
CLI_OPTS+=" --use-simple-cache-backend=on "
# working on rendering bug
# https://bbs.archlinux.org/viewtopic.php?id=181823
CLI_OPTS+=" --ignore-gpu-blacklist "
# if in gnome, use this mapping for numbered spaces
NUMBERED_WS_MAPPING=(es wrk hak)
# workspace names that are "disregarded" and treated like normal, numbered
# workspaces (and thus use the default browser profile, whate're that may be)
DEFAULT_WS=(es coms hak)
# always use incognito profile on the following spaces
INCOGNITO_WS=(test dev)
# first checks $BROWSER value to see if it matches an expected chromium or google-chrome
# otherwise checks for valid browser executable in $PATH
# checks for known google-chrome/chromium variations
if [[ -z ${(M)browsers:#${BROWSER:-}} ]]
then
for browser in $browsers; do [[ -n ${(M)${~:-=$browser}#=} ]] || { BROWSER=$browser; break }; done
fi
# get current name of workspace, lowercased and stripped of non alpha chars
if [[ -n ${(M)${(L)$(wmctrl -m)}:#gnome} ]]
then
#WS=${${:-$NUMBERED_WS_MAPPING[$((${$(xprop -root -notype _NET_CURRENT_DESKTOP)[(w)-1]}+1))]}//[^[:alpha:]]/}
WS=${${:-$NUMBERED_WS_MAPPING[$((${$(xdotool get_desktop)[(w)-1]}+1))]}//[^[:alpha:]]/}
#(( $SCALING > 1 )) && WS_SUFFIX="_highdpi"
#CLI_OPTS+=" --force-device-scale-factor=$SCALING"
else
#WS=${${(SL)$(i3-msg -t get_workspaces)//(#b)(#s)*name\":\"([[:alnum:]:]##)*focused\":true*(#e)/$match}//[^[:alpha:]]/}
#CLI_OPTS+=" --force-device-scale-factor=$SCALING"
fi
# filter out named spaces that we want to use the default profile on (no --user-data-dir option will be used for these workspaces)
WS_FILTERED=${WS:|DEFAULT_WS}
# browser profile config data directory root (XDG config directory or defaults to $HOME/.config
CONFIG_DIR=${XDG_CONFIG_HOME:-${HOME}/.config}
# command line options to apply on default workspaces
DEFAULT_OPTS=(${WS_SUFFIX:+--user-data-dir=${CONFIG_DIR}/${BROWSER}$WS_SUFFIX})
# browser command
# NOTE: the XDG_CACHE_HOME example below (commented out) seems to be enough to get chrome to use /tmp/cache as the cache; the commented out CLI_OPTS flag above that sets /tmp/cache as well appears to be redundant
#XDG_CACHE_HOME=/tmp/cache $BROWSER $CLI_OPTS ${*} ${${${WS:*INCOGNITO_WS}:+--incognito}:-${${WS_FILTERED:+--user-data-dir=${CONFIG_DIR}/${BROWSER}_$WS_FILTERED${WS_SUFFIX:-}}:-${DEFAULT_OPTS:-}}} $*
$BROWSER $CLI_OPTS ${*} ${${${WS:*INCOGNITO_WS}:+--incognito}:-${${WS_FILTERED:+--user-data-dir=${CONFIG_DIR}/${BROWSER}_$WS_FILTERED${WS_SUFFIX:-}}:-${DEFAULT_OPTS:-}}} $*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment