aboutsummaryrefslogtreecommitdiff
path: root/webtrayctl
blob: 11a36d16b9e13658927f7a4f7e8553bda3ec1041 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/sh

help() {
	echo webtrayctl - install and remove webtray applications
	echo
	echo "    install   <url> <name> [--open-at-startup] [--icon <icon-file>]"
	echo "    uninstall <name>"
	echo "    show <url>"
	echo
	exit
}

install_webapp() {
	mkdir -p ~/.local/share/webtray/icons
	mkdir -p ~/.local/share/webtray/applications/

	url="$1"
	name="$2"
	open_at_startup="$3"
	iconfile="$4"
	

	full_url=$(curl -Ls -o /dev/null -w%{url_effective} $url)

	if [ -z "$iconfile" ]; then
		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)
		if echo "$full_url" | grep 'x\.com' > /dev/null; then
			iconfile='https://upload.wikimedia.org/wikipedia/commons/thumb/6/6f/Logo_of_Twitter.svg/240px-Logo_of_Twitter.svg.png'
		else
			iconfile=$(curl -Ls "$url" | grep -o "<link[^>]*rel=[\"']\\(shortcut \\)\\?icon[\"'][^>]*>" | head -n 1 | sed "s/.*href=[\"']\\([^\"]*\\)[\"'].*/\\1/g" | sed "/^http/!s/.*/$full_url_cancled\\/&/")
		fi
		file_extension=$(echo "$iconfile" | grep -o '\.[^.]*$')
		tmp_icon_name="$name$file_extension"

		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 path to a favicon on your computer with the --icon argument"
		fi
	fi

	echo "[Desktop Entry]
Type=Application
Name=$name
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"

	exit
}

remove_webapp() {
	name="$1"
	xdg-desktop-menu uninstall "$HOME/.local/share/webtray/applications/webtray-$name.desktop"
	rm "$HOME/.local/share/webtray/applications/webtray-$name.desktop" "$HOME/.local/share/webtray/icons/$name.png"
	exit
}

show() {
	host=$(echo "$1" | sed 's/^https\?:\/\/\([^/]*\).*$/\1/')
	dbus-send \
		--dest="xyz.nathanreiner.WebTray.$host" \
		--type=method_call \
		--print-reply \
		'/' \
		local.webtray.DBusHandler.show > /dev/null \
		|| setsid webtray "$1" --open-at-startup &
}

while true
do
	case "$1" in
		install)
			action="install"
			shift 2>/dev/null
			url="$1"
			name="$2"
			shift 2 2>/dev/null
			;;
		uninstall)
			action="uninstall"
			shift 2>/dev/null
			toRemove="$1"
			shift 2>/dev/null
			;;
		"--open-at-startup")
			open_at_startup="--open-at-startup"
			shift 2>/dev/null
			;;
		"--icon")
			shift 2>/dev/null
			icon="$1"
			[ -z "$1" ] && help
			shift 2>/dev/null
			;;
		show)
			action="show"
			shift 2>/dev/null
			url="$1"
			shift 2>/dev/null
			;;
		*)help;;
	esac
	if [ -z "$1" ]; then
		break
	fi
done

case "$action" in
	install)
		[ -z "$url" -o -z "$name" ] && help
		install_webapp "$url" "$name" "$open_at_startup" "$icon"
		;;
	uninstall)
		[ -z "$toRemove" ] && help
		remove_webapp "$toRemove"
		;;
	show)
		[ -z "$url" ] && help
		show "$url"
		;;
	*)help;;
esac