diff options
author | Kuerbiskosar <kuerbiskosar@gmail.com> | 2024-10-21 23:25:05 +0200 |
---|---|---|
committer | Kuerbiskosar <kuerbiskosar@gmail.com> | 2024-10-21 23:25:05 +0200 |
commit | a26d1c62309e85879ddca74b55ba6797cf4facd5 (patch) | |
tree | 0f14f92a0f6d311c3fc2284279a79bcadb98e841 | |
parent | 21afec7d459855808444ef8157a2249b2fb2782c (diff) |
improved favicon search algorithm and made them transparent
-rwxr-xr-x | webtrayctl | 83 |
1 files changed, 69 insertions, 14 deletions
@@ -3,7 +3,7 @@ help() { echo webtrayctl - install and remove webtray applications echo - echo " install <url> <name> [--open-at-startup]" + echo " install <url> <name> [--open-at-startup] [--icon <icon-file>]" echo " uninstall <name>" echo exit @@ -15,43 +15,98 @@ install_webapp() { url="$1" name="$2" - extra="$3" + open_at_startup="$3" + iconfile="$4" + - tmp_icon_name="$name-$$.ico" + full_url=$(curl -Ls -o /dev/null -w%{url_effective} $url) - curl -s "$url/$(curl -s "$url" | grep 'rel="[^"]*icon.*\.ico' | sed 's/^.*href="\([^"]*\)".*/\1/g')" --output "/tmp/$tmp_icon_name" + if [ -z "$iconfile" ]; then + echo using favicon as icon file + full_url_cancled=$(echo $full_url | sed 's/\//\\\//g') + # get the linkt to the icon by first searching for every <link> tag, containing a icon link + # then getting the string of the first href in the first link, and then appending the extended url to the beginning, if it is not a relative path (only works for http and https protocols) + iconfile=$(curl -Ls "$url" | grep -o "<link[^>]*rel=[\"']\\(shortcut \\)\\?icon[\"'][^>]*>" | head -n 1 | sed "s/.*href=[\"']\\([^\"]*\\)[\"'].*/\\1/g" | sed "/^http/!s/.*/$full_url_cancled\\/&/") + + file_extension=$(echo "$iconfile" | grep -o '\.[^.]*$') + tmp_icon_name="$name$file_extension" - magick "/tmp/${tmp_icon_name}[0]" -flatten "$HOME/.local/share/webtray/icons/$name.png" + if [ -n "$iconfile" ]; then + curl -s "$iconfile" --output "/tmp/${tmp_icon_name}" + magick "/tmp/${tmp_icon_name}[0]" "$HOME/.local/share/webtray/icons/$name.png" + rm "/tmp/$tmp_icon_name" + iconfile="$HOME/.local/share/webtray/icons/$name.png" + else + echo "the website does not seem to have a favicon" + echo "consider adding the local path to a favicon on your computer with the --icon argument" + fi + fi echo "[Desktop Entry] Type=Application Name=$name -Exec=$(which webtray) '$url' $extra -Icon=$HOME/.local/share/webtray/icons/$name.png +Exec=$(which webtray) '$full_url' "$open_at_startup" "$iconfile" +Icon="$iconfile" Terminal=false Categories=WebApp " > "$HOME/.local/share/webtray/applications/webtray-$name.desktop" xdg-desktop-menu install "$HOME/.local/share/webtray/applications/webtray-$name.desktop" - rm "/tmp/$tmp_icon_name" + exit } remove_webapp() { name="$1" xdg-desktop-menu uninstall "$HOME/.local/share/webtray/applications/webtray-$name.desktop" - rm "$HOME/.local/share/webtray/applications/$name.desktop" "$HOME/.local/share/webtray/icons/$name.png" + rm "$HOME/.local/share/webtray/applications/webtray-$name.desktop" "$HOME/.local/share/webtray/icons/$name.png" + exit } +while true +do + case "$1" in + install) + action="install" + shift + url="$1" + name="$2" + shift 2 + ;; + uninstall) + action="uninstall" + shift + toRemove="$1" + shift + ;; + "--open-at-startup") + echo we are opening at startup + echo "$argument" + open_at_startup="--open-at-startup" + shift + ;; + "--icon") + shift + icon="$1" + [ -z "$1" ] && help + shift + ;; + *)help;; + esac + if [ -z "$1" ]; then + break + fi + #shift 1 || break +done -case "$1" in +case "$action" in install) - [ -z "$2" -o -z "$3" ] && help - install_webapp "$2" "$3" + [ -z "$url" -o -z "$name" ] && help + install_webapp "$url" "$name" "$open_at_startup" "$icon" ;; uninstall) - [ -z "$2" ] && help - remove_webapp "$2" + [ -z "$toRemove" ] && help + remove_webapp "$toRemove" ;; *)help;; esac |