Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
Yeah!.... already triggering
Browse files Browse the repository at this point in the history
  • Loading branch information
Getty committed May 3, 2012
1 parent 323b750 commit 74bba28
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
19 changes: 16 additions & 3 deletions lib/App/DuckPAN/Cmd/Server.pm
Expand Up @@ -118,10 +118,23 @@ sub change_html {
);

for (@script) {
if ($_->attr('src') && substr($_->attr('src'),0,1) eq '/') {
$_->attr('src','/?duckduckhack_js=1');
if (my $src = $_->attr('src')) {
if ($src =~ m/^\/d\d{3}\.js/) {
$_->attr('src','/?duckduckhack_js=1');
} elsif (substr($src,0,1) eq '/') {
$_->attr('src','https://duckduckgo.com'.$_->attr('src'));
}
}
}

my @img = $root->look_down(
"_tag", "img"
);

for (@img) {
if ($_->attr('src')) {
$_->attr('src','https://duckduckgo.com'.$_->attr('src'));
}
#$_->attr('src','https://duckduckgo.com'.$_->attr('src'));
}

return $self->change_css($root->as_HTML);
Expand Down
40 changes: 37 additions & 3 deletions lib/App/DuckPAN/Web.pm
@@ -1,8 +1,11 @@
package App::DuckPAN::Web;

use Moo;
use DDG::Request;
use Plack::Request;
use Plack::Response;
use HTML::Entities;
use HTML::TreeBuilder;

has blocks => ( is => 'ro', required => 1 );
has page_root => ( is => 'ro', required => 1 );
Expand All @@ -20,16 +23,47 @@ sub run_psgi {
sub request {
my ( $self, $request ) = @_;
my $response = Plack::Response->new(200);
my $body;
if ($request->param('duckduckhack_css')) {
$response->content_type('text/css');
$response->body($self->page_css);
$body = $self->page_css;
} elsif ($request->param('duckduckhack_js')) {
$response->content_type('text/javascript');
$response->body($self->page_js);
$body = $self->page_js;
} elsif ($request->param('q')) {
my $query = $request->param('q');
Encode::_utf8_on($query);
my $ddg_request = DDG::Request->new( query_raw => $query );
my $result;
for (@{$self->blocks}) {
$result = $_->request($ddg_request);
last if $result;
}
my $page = $self->page_spice;
$page =~ s/duckduckhack-template-for-spice/$query/g;
if ($result) {

} else {
my $root = HTML::TreeBuilder->new;
$root->parse($self->page_root);
my $error_field = $root->look_down(
"id", "error_homepage"
);
$error_field->push_content("Sorry, no hit on your plugins");
$error_field->attr( id => "error_duckduckhack" );
my $text_field = $root->look_down(
"name", "q"
);
$text_field->attr( value => $query );
$page = $root->as_HTML;
}
$response->content_type('text/html');
$body = $page;
} else {
$response->content_type('text/html');
$response->body($self->page_root);
$body = $self->page_root;
}
$response->body($body);
return $response;
}

Expand Down

0 comments on commit 74bba28

Please sign in to comment.