When analyzing a web page with Mojo::DOM(or any other structure) it is quite common to pull out the address of a resource, which can be either relative or absolute. Is there a shortcut method for translating such a resource address to an absolute URL?
The following command mojopulls all stylesheets to mojolicio.us:
$ mojo get http:
/mojo/prettify/prettify-mojo-light.css
/css/index.css
And the following script does the same, but also uses URIit to translate the resource into an absolute URL.
use strict;
use warnings;
use Mojo::UserAgent;
use URI;
my $url = 'http://mojolicio.us';
my $ua = Mojo::UserAgent->new;
my $dom = $ua->get($url)->res->dom;
for my $csshref ($dom->find('link[rel=stylesheet]')->attr('href')->each) {
my $cssurl = URI->new($csshref)->abs($url);
print "$cssurl\n";
}
Outputs:
http:
http:
Obviously, the relative URL in this context should be absolute using the URL that the DOM was loading. However, I do not know how to get the absolute URL of a resource other than the encoding itself.
Mojolicious Mojo::URL #to_abs. , , - Mojo::DOM , URI.
, - script, , - Mojo :
mojo get http://mojolicio.us "link[rel=stylesheet]" attr href to_abs