Add clipboard flag to copy URL to clipboard instead of print.

This commit is contained in:
2026-03-11 16:21:14 +01:00
parent 9c0e6fc1bc
commit 213cd56afc
2 changed files with 95 additions and 15 deletions

View File

@@ -24,25 +24,64 @@ set -euo pipefail
# 3. This notice may not be removed or altered from any source # 3. This notice may not be removed or altered from any source
# distribution. # distribution.
YT_URL="$1"
INVID_BASE="${INVID_BASE:-https://inv.nadeko.net}" INVID_BASE="${INVID_BASE:-https://inv.nadeko.net}"
CLIP=0
if [ $# -ne 1 ] if [ $# -lt 1 ]
then then
>&2 echo "Usage: $0 <url>" >&2 echo "Usage: $0 [-c] <url>"
exit 1 exit 1
elif [ "$1" = "-h" ]
then
echo "Usage: $0 <url>"
exit 0
fi fi
if [[ $YT_URL == *"youtu.be"* ]] for arg in "$@"
do
case "$arg" in
-c|--clip)
CLIP=1
;;
-h|--help)
echo "Usage: $0 <url>"
exit 0
;;
*)
YT_URL="$arg"
;;
esac
done
if [ -z "${YT_URL:-}" ]
then
>&2 echo "No URL provided"
exit 1
elif [[ $YT_URL == *"youtu.be"* ]]
then then
INVID_URL="${INVID_BASE}/watch?v=${YT_URL##*/}" INVID_URL="${INVID_BASE}/watch?v=${YT_URL##*/}"
elif [[ $YT_URL == *"www.youtube.com"* ]] elif [[ $YT_URL == *"www.youtube.com"* ]]
then then
INVID_URL="${INVID_BASE}/${YT_URL##*/}" INVID_URL="${INVID_BASE}/${YT_URL##*/}"
else
>&2 echo "Invalid URL: $YT_URL"
exit 1
fi fi
if [ $CLIP -eq 0 ]
then
echo "$INVID_URL" echo "$INVID_URL"
else
if [ "$XDG_SESSION_TYPE" = "wayland" ]
then
if ! command -v wl-copy &> /dev/null
then
echo "wl-copy not found, cannot copy to clipboard"
exit 1
fi
echo "$INVID_URL" | wl-copy
else
if ! command -v xclip &> /dev/null
then
echo "xclip not found, cannot copy to clipboard"
exit 1
fi
echo "$INVID_URL" | xclip -sel clip
fi
fi

51
yt-conv
View File

@@ -24,16 +24,57 @@ set -euo pipefail
# 3. This notice may not be removed or altered from any source # 3. This notice may not be removed or altered from any source
# distribution. # distribution.
CLIP=0
if [ $# -ne 1 ] if [ $# -ne 1 ]
then then
>&2 echo "Usage: $0 <url>" >&2 echo "Usage: $0 <url>"
exit 1 exit 1
elif [ "$1" = "-h" ]
then
echo "Usage: $0 <url>"
exit 0
fi fi
URL="$1" for arg in "$@"
do
case "$arg" in
-c|--clip)
CLIP=1
;;
-h|--help)
echo "Usage: $0 [-c] <url>"
exit 0
;;
*)
URL="$arg"
;;
esac
done
if [ -z "${URL:-}" ]
then
>&2 echo "No URL provided"
exit 1
fi
YT_URL="${URL//https:\/\/*\//https:\/\/www.youtube.com\/}" YT_URL="${URL//https:\/\/*\//https:\/\/www.youtube.com\/}"
if [ $CLIP -eq 0 ]
then
echo "$YT_URL" echo "$YT_URL"
else
if [ "$XDG_SESSION_TYPE" = "wayland" ]
then
if ! command -v wl-copy &> /dev/null
then
>&2 echo "wl-copy not found, cannot copy to clipboard"
exit 1
fi
echo "$YT_URL" | wl-copy
else
if ! command -v xclip &> /dev/null
then
>&2 echo "xclip not found, cannot copy to clipboard"
exit 1
fi
echo "$YT_URL" | xclip -sel clip
fi
fi