summaryrefslogtreecommitdiffstats
path: root/browser_launcher.pl
blob: a77dc2ae288a55e8b790d6329f6e34ae84cd5021 (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
#!/usr/bin/env perl

=head1 DESCRIPTION

Open an URL in different browsers depending on the URL. Play spotify URLs with
the spotify client.

=cut

use strict;
use warnings;
use autodie qw(:all);

use Text::Template 'fill_in_string';

my $sites = {
	'ssh-git' => {
		'patterns' => [
			qr|^ssh://(?:(?<username>\w+)@)?(?<domain>[\w.]+)(?::(?<port>\d+))?/(?<path>.*)\.git|,
		],
		'command' => ['firefox', 'https://{$domain}/{$path}'],
	},
	'youtube' => {
		'patterns' => [
			qr|^https://www.youtube.com|,
			qr|^https://youtube.com|,
			qr|^https://youtu.be|,
		],
		'command' => ['firefox', '{$url}'],
	},
	'spotify' => {
		'patterns' => [
			qr|^https://open\.spotify\.com/(?<group>\w+)/(?<track>\w+)?|
		],
		'command' => [
			'dbus-send', '--print-reply',
			'--dest=org.mpris.MediaPlayer2.spotify',
			'/org/mpris/MediaPlayer2',
			'org.mpris.MediaPlayer2.Player.OpenUri',
			'string:spotify:{$group}:{$track}',
		],
	},
};

my $url_argument = $ARGV[0];

for my $key (keys $sites->%*) {
	my $site = $sites->{$key};
	for my $pattern ($site->{patterns}->@*) {
		if ($url_argument =~ m/$pattern/) {
			my @cmd = $site->{command}->@*;
			# template @cmd
			@cmd = map {fill_in_string($_, HASH => {%+, url => $url_argument}) } @cmd;
			exec(@cmd);
		}
	}
}

exec $ENV{'REAL_BROWSER'} // 'firefox', $url_argument;