add surf one-tab focus browser script
This commit is contained in:
parent
9f5c60992e
commit
f81a2849a6
1 changed files with 92 additions and 0 deletions
92
local/bin/surf
Executable file
92
local/bin/surf
Executable file
|
|
@ -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] <url> [chromium args...]\n' "$1" "${0##*/}" >&2
|
||||
exit 1
|
||||
;;
|
||||
*)
|
||||
break
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [[ $# -lt 1 ]]; then
|
||||
printf 'Usage: %s [-f|--focus] <url> [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[@]}" \
|
||||
"$@"
|
||||
Loading…
Add table
Add a link
Reference in a new issue