From f81a2849a613bf1772eb2de470e0be877edec069 Mon Sep 17 00:00:00 2001 From: Hunter Haugen Date: Fri, 3 Apr 2026 13:55:44 -0700 Subject: [PATCH] add surf one-tab focus browser script --- local/bin/surf | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100755 local/bin/surf diff --git a/local/bin/surf b/local/bin/surf new file mode 100755 index 0000000..97fd2d2 --- /dev/null +++ b/local/bin/surf @@ -0,0 +1,92 @@ +#!/usr/bin/env bash + +set -euo pipefail + +focus_mode=false + +while [[ $# -gt 0 ]]; do + case "$1" in + -f|--focus) + focus_mode=true + shift + ;; + --) + shift + break + ;; + -*) + printf 'Unknown option: %s\nUsage: %s [-f|--focus] [chromium args...]\n' "$1" "${0##*/}" >&2 + exit 1 + ;; + *) + break + ;; + esac +done + +if [[ $# -lt 1 ]]; then + printf 'Usage: %s [-f|--focus] [chromium args...]\n' "${0##*/}" >&2 + exit 1 +fi + +raw_target="$1" +shift + +urlencode() { + local input="$1" + local output="" + local i char hex + + for ((i = 0; i < ${#input}; i++)); do + char="${input:i:1}" + case "$char" in + [a-zA-Z0-9.~_-]) + output+="$char" + ;; + ' ') + output+='%20' + ;; + *) + printf -v hex '%%%02X' "'$char" + output+="$hex" + ;; + esac + done + + printf '%s\n' "$output" +} + +looks_like_url_without_schema() { + local target="$1" + + [[ ! "$target" =~ [[:space:]] ]] || return 1 + [[ "$target" =~ ^localhost([:/?#].*)?$ ]] && return 0 + [[ "$target" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}([:/?#].*)?$ ]] && return 0 + [[ "$target" =~ ^([[:alnum:]][[:alnum:]-]*\.)+[[:alpha:]]{2,}([:/?#].*)?$ ]] +} + +if [[ "$raw_target" =~ ^[[:alpha:]][[:alnum:]+.-]*: ]]; then + url="$raw_target" +elif looks_like_url_without_schema "$raw_target"; then + url="https://$raw_target" +else + encoded_query="$(urlencode "$raw_target")" + url="https://search.hunner.dev/search?q=$encoded_query" +fi + +if "$focus_mode"; then + chromium_args=( + --kiosk + --new-window + "$url" + ) +else + chromium_args=( + --app="$url" + --new-window + ) +fi + +chromium \ + "${chromium_args[@]}" \ + "$@"