Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Perl Web Browser using Gtk2 and WebKit

by zentara (Archbishop)
on Feb 02, 2010 at 17:39 UTC ( #820982=CUFP: print w/replies, xml ) Need Help??

Hi, often the question comes up, how to show html within an app.... like in automating screenshots of sites. Thanks to Gtk2::WebKit ,( which requires WebKit ), now you can have almost the full power of modern browser's right in your own program.

The webkit c libs, come with a few examples.... one was called GtkLauncher.... which is a rudimentary browser reminiscent of the old Netscape 3. So, I converted the c code, to a Perl version.

I'm sure it's not perfect, but it works pretty good, and ought to get you started on use in your own programs.

Most of the perldocs are in longish named strings, like Gtk2::WebKit::WebSettings, so check out the man3 pages that come with the perl module, for access to the docs.

#!/usr/bin/perl use strict; use warnings; use Gtk2 -init; use Gtk2::WebKit; use Glib qw(TRUE FALSE); # by zentara.... a free Perl adaptation of WebKit's GtkLauncher my $win = Gtk2::Window->new; $win->set_default_size(800, 600); $win->signal_connect(destroy => sub { Gtk2->main_quit }); #globals my ($toolbarbox,$viewbox,$statusbox,$entry,$view,$statusbar, $title, $load_progress, $status_context_id); $title = 'Perl_Browser'; $win->set_title($title); my $vbox = Gtk2::VBox->new( FALSE, 2 ); $vbox->set_border_width(2); $win->add($vbox); $vbox->set_border_width(2); foreach my $box ( $toolbarbox, $viewbox, $statusbox){ $box = Gtk2::VBox->new( FALSE, 0 ); $box->set_border_width(0); $vbox->pack_start($box,1,1,1); } create_toolbar($toolbarbox); create_browser($viewbox); create_status($statusbox); $win->show_all; $view->grab_focus; #if you want to open a default file or uri #$view->open( $ARGV[0] || 'file://z.html' ); #$view->open( $ARGV[0] || '' ); Gtk2->main; sub create_toolbar{ my $box = $_[0]; my $toolbar = Gtk2::Toolbar->new; $toolbar->set_icon_size ('large-toolbar'); $toolbar->set_show_arrow (FALSE); $toolbar->set_orientation ('GTK_ORIENTATION_HORIZONTAL'); #make the toolbar buttons, etc my $button = Gtk2::ToolButton->new_from_stock('gtk-go-back'); $button->signal_connect( "clicked" => \&go_back, undef ); $toolbar->insert($button, -1); $toolbar->insert(Gtk2::SeparatorToolItem->new ,-1 ); $button = Gtk2::ToolButton->new_from_stock('gtk-go-forward'); $button->signal_connect( "clicked" => \&go_forward, undef ); $toolbar->insert($button, -1); $toolbar->insert(Gtk2::SeparatorToolItem->new ,-1 ); # make entry for URI's my $entbox = Gtk2::ToolItem->new(); $entbox->set_expand(1); $entry = Gtk2::Entry->new(); $entbox->add($entry); $entry->signal_connect( "activate" => \&activate_uri_entry, und +ef ); $toolbar->insert($entbox, -1); $button = Gtk2::ToolButton->new_from_stock('gtk-ok'); $button->signal_connect( "clicked" => \&activate_uri_entry, und +ef ); $toolbar->insert($button, -1); $button = Gtk2::ToolButton->new(undef,'Screenshot'); $button->signal_connect( "clicked" => \&screenshot, undef ); $toolbar->insert($button, -1); $box->pack_start($toolbar,FALSE,FALSE,0); } sub create_browser{ my $box = $_[0]; my $sw = Gtk2::ScrolledWindow->new; $sw->set_policy ('automatic', 'automatic'); $sw->set_size_request (500, 500); # hack to set initial empty +browser size $view = Gtk2::WebKit::WebView->new; $view->signal_connect( 'notify::title' => \&notify_title, unde +f ); $view->signal_connect( 'notify::load-status' => \&notify_load_ +status, undef ); $view->signal_connect( 'notify::progress' => \&notify_progress +, undef ); $view->signal_connect( 'hovering-over-link' => \&hovering_over +_link, undef ); $sw->add($view); $box->pack_start($sw,1,1,0); } sub create_status{ my $box = $_[0]; $statusbar = Gtk2::Statusbar->new; $status_context_id = $statusbar->get_context_id('Link Hover'); $box->pack_start($statusbar,FALSE,FALSE,0); } sub go_back{ $view->go_back } sub go_forward{ $view->go_forward } sub activate_uri_entry{ my $uri = $entry->get_text; $uri ||= ''; $view->load_uri($uri); } sub notify_title{ my $vtitle = $view->get_title(); if ($vtitle){ $win->set_title( $vtitle ) }; } sub notify_load_status{ if( $view->get('load_status') eq 'WEBKIT_LOAD_COMMITTED'){ my $frame = $view->get_main_frame(); my $uri = $view->frame_get_uri($frame); if( $uri ){ $entry->set_text($uri) } } } sub notify_progress{ $load_progress = $view->get('progress'); my $title = sprintf ("Progress: %2d%%", 100 * $load_progress); $win->set_title($title); } sub hovering_over_link{ my ($hash, undef, $link) = @_; if(defined $link){ #print "$status_context_id\t$link \n"; $statusbar->pop($status_context_id); $statusbar->push($status_context_id, $link); } } sub update_title{ $win->set_title($load_progress) } sub screenshot{ # we are going to save the visible browser view window # this DOES NOT save the entire html, only what is visible my ($width, $height) = $view->window->get_size; # create blank pixbuf to hold the image my $gdkpixbuf = Gtk2::Gdk::Pixbuf->new ('rgb', 0, 8, $width, $height); $gdkpixbuf->get_from_drawable ($view->window, undef, 0, 0, 0, 0, $width, $height); #only jpeg and png is supported !!!! it's 'jpeg', not 'jpg' $gdkpixbuf->save ("$0.jpg", 'jpeg', quality => 100); return FALSE; }

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku

Replies are listed 'Best First'.
Re: Perl Web Browser using Gtk2 and WebKit
by xorcist (Initiate) on May 07, 2012 at 17:11 UTC
    I have got this excellent code to work without problems and now I'd like to automate moving the mouse cursor over the newly opened browser and also automate clicking... what's the best way to do that (with Perl Gtk2)?
      Hi, that's a tough one, If you google for Gtk2 Webkit mouse position you get some links to C code ( which you could probably easily convert to Perl), especially WebKitHitTestResult.

      As far as being able to programmably move the mouse position to an arbitrary point in a window, I've not seen it done, your best bet would be to ask this question on the gtk-perl-maillist.

      You might have more success using keys, like tabbing to advance focus to the next clickable link.

      If you find an answer, be sure to post it. :-)

      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh
Re: Perl Web Browser using Gtk2 and WebKit
by renegadex (Beadle) on Jan 13, 2013 at 04:04 UTC
    Hi Zentara! Wow I've been looking for these for 3 years!!! Finally!! Great Code! Works great. Can I-repost your code on my blog?
    Mabuhay Civil Engineers! :D
Re: Perl Web Browser using Gtk2 and WebKit
by Anonymous Monk on Nov 13, 2016 at 09:42 UTC
    For those using Gtk3: sed -i -e 's/Gtk2/Gtk3/g'

      There are places where sed beats perl. This isn't one of them. :-)

      perl -pi -e 's/Gtk2/Gtk3/g'
Re: Perl Web Browser using Gtk2 and WebKit
by Anonymous Monk on Apr 05, 2012 at 22:47 UTC
    hi zentara, do you have an updated version for this? I can't seem to find the Gtk2::Webkit. I've been searching the Perl Gtk2 Documentation and I can't find a widget that will display HTML.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://820982]
Approved by marto
Front-paged by Arunbear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2018-04-19 11:03 GMT
Find Nodes?
    Voting Booth?