Skip to content

Instantly share code, notes, and snippets.

@stephenmm
Last active December 28, 2020 20:46
Show Gist options
  • Save stephenmm/64f7726a75eb53fdce933e4285d920c3 to your computer and use it in GitHub Desktop.
Save stephenmm/64f7726a75eb53fdce933e4285d920c3 to your computer and use it in GitHub Desktop.
Open webpage in chrome from linux terminal (and search for string "my_string"): xchrome -f my_string /path/to/html.html
function xchrome() {
read -r -d '' HELP_MSG << END_HELP_MSG
Description:
Open webpage in chrome from terminal (and search for string "my_string")
Installation:
Add this function to your .bashrc
Known limitiations:
find string can only contain [a-zA-Z_]
URL cannot have anchors /path/to/html.html#anchor
Required options:
<url> # Cannot have internal anchors defined.
Optional args:
-f|--find <string> # Can only contain [a-zA-Z_]
-h|--help # This help message
Usage example:
xchrome -f my_string /path/to/html.html
END_HELP_MSG
unset str2find
# Parse args
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-h|--help) echo -e "$HELP_MSG"; return 0; ;;
-f|--find) str2find="$2"; shift; shift; ;;
*) POSITIONAL+=("$1") $(: save it in an array for later ); shift; ;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
url="$1"; shift
# Bring the Chrome window to the current desktop/workspace
xdotool search --name chrome set_desktop_for_window %@ $( xdotool get_desktop )
# Open the URL
/usr/bin/google-chrome-stable "$url"
# send keys to chrome to find "string"
if [ ! -z ${str2find+x} ]; then
str2find_spaced=$( echo $str2find | sed -e 's/\(.\)/\1 /g' )
str2find_expanded=$( echo $str2find_spaced | sed -e 's/_/underscore/g' )
finalStr="ctrl+f $str2find_expanded"
winId=$( xdotool search --name "Google Chrome" )
xdotool windowactivate --sync $winId key $finalStr
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment