Skip to content

Instantly share code, notes, and snippets.

/frontpage.pl Secret

Created September 17, 2014 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/9869edbdf244a6c69a99 to your computer and use it in GitHub Desktop.
Save anonymous/9869edbdf244a6c69a99 to your computer and use it in GitHub Desktop.
use Mojolicious::Lite;
helper title => sub { shift->ua->get(shift)->res->dom->at('title')->text };
# ...insert awesome comment here...
get '/' => sub {
my $c = shift;
return $c->render('index') unless my $url = $c->param('url');
$c->render(text => $c->title($url));
};
# WebSocket service scraping information from a web site
websocket '/title' => sub {
my $c = shift;
$c->on(message => sub {
my ($c, $url) = @_;
$c->send($c->title($url));
});
};
app->start;
__DATA__
@@ index.html.ep
% my $websocket = url_for 'title';
<script>
var ws = new WebSocket('<%= $websocket->to_abs %>');
ws.onmessage = function (event) {
document.body.innerHTML += event.data;
};
ws.onopen = function () {
ws.send('http://mojolicio.us');
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment