#!/bin/bash set -eu # shellcheck disable=2034 description="url handler" FUZL_PROMPT=urlopen . libfuzl.sh main() { case "$#" in 1) fuzl_action_add "open" "" "open" fuzl_action_add "ask" "󰮫" "ask" fuzl_action_add "clipboard" "󰅌" "clipboard" fuzl_action_add "primary" "󱊁" "primary" fuzl_action_menu "$(get_text "$*")" || open_default "$(get_text "$*")" ;; 2) local action action=${1:?missing action} shift eval "urlopen_$action" "'$(get_text "$*")'" ;; esac } # # helpers # DEFAULT_BROWSER=librewolf.desktop get_text() { local text="$*" [ -z "$text" ] && read -r text printf '%s' "$text" } get_mimetype() { local text=${1:?missing url} case "$text" in http*) printf %s 'x-scheme-handler/http' ;; *) xdg-mime query filetype "$text" 2>/dev/null || file -bE --mime-type "$text" ;; esac } get_x-scheme-handler_override() { case "$1" in slack.com) printf %s 'workfox.desktop' ;; *) return 1 ;; esac } open_default() { local text=${1:?missing url} local application case "$(get_mimetype "$text")" in x-scheme-handler/http*) application=$DEFAULT_BROWSER ;; esac application_override=$(get_x-scheme-handler_override "$text" || :) if [ -n "${application_override}" ]; then application=$application_override fi if [ -n "${application:-}" ]; then desktop_file=$(rg -Lil -g "$application" . -- /usr/share/applications/ /var/lib/flatpak/exports/share/applications/ ~/.local/share/applications/ | head -1) exec gio launch "$desktop_file" "$text" fi exec gio open "$text" } # # actions # urlopen_ask() { local text=${1:?missing url} env porta open-uri "$text" ask } urlopen_open() { local text=${1:?missing url} open_default "$text" } urlopen_clipboard() { local text=${1:?missing url} wl-copy -n "$text" # fuzl_notification "copied to clipboard" "$text" } urlopen_primary() { local text=${1:?missing url} wl-copy -np "$text" # fuzl_notification "copied to primary" "$text" } main $@