1
0
.dotfiles/home/bin/browser-wrap

60 lines
1.6 KiB
Plaintext
Raw Normal View History

2014-02-28 23:42:51 +00:00
#!/usr/bin/perl
# browser-wrap: Set this as your default browser to open certain links with
# certain browsers.
#
# To get `xdg-open` to use this, put this in your ~/.profile (update the path
# to match where you installed the script to):
#
# if [ -n "$DISPLAY" ]; then
# BROWSER=/home/kirsle/bin/browser-wrap
# fi
#
# --Kirsle
# http://sh.kirsle.net/
use 5.14.0;
use strict;
use warnings;
#------------------------------------------------------------------------------#
# Configuration Section #
#------------------------------------------------------------------------------#
# Define your browser rules here.
my $rules = {
# These are domain names to match. Use a regular expression.
qr/(facebook|fbcdn)\.(com|net)/ => "google-chrome",
};
# Default browser for links that don't have rules that match.
my $default = "firefox";
#------------------------------------------------------------------------------#
# End Configuration Section #
#------------------------------------------------------------------------------#
# Get the URL passed in.
my $url = shift(@ARGV);
my $browser = $default;
# Looks okay?
if ($url =~ /^https?:\/\/([^\/]+)\/?/i) {
print "Domain: $1\n";
my $domain = $1;
# Look for the best rule.
my @sorted = sort { length($b) <=> length($a) } keys %{$rules};
foreach my $rule (@sorted) {
if ($domain =~ /$rule/i) {
# Matched!
$browser = $rules->{$rule};
last;
}
}
}
# Launch the browser.
my ($app, @args) = split(/\s+/, $browser);
exec($app, @args, $url, @ARGV);