User Tools

Site Tools


uzblctrl-pl

Table of Contents

uzblctrl.pl

This is a simple perl script showing how you can use perl to communicate with a running instance of uzbl.

Usage

For instance, to get a dump of the current html document, you can do this:

uzblctrl.pl <socket> 'print @<document.all[0].innerHTML>@'

Code

#!/usr/bin/env perl
 
# simple perl implementation of uzblctrl, which can be used to query uzbl for stuff.
# usage: uzblctrl.pl <socket> <command>
#
 
use strict;
 
use IO::Socket::UNIX;
use Digest::MD5 qw(md5_hex);
 
my $path = shift;
my @cmd = @ARGV;
 
# simple Uzbl socket object
sub Uzbl{
  my $path = shift;
  # connect the socket
  my $socket = IO::Socket::UNIX->new(Type => SOCK_STREAM, Peer => $path) or die $@;
  return sub {
    my $line = shift;
    # generate a end marker
    my $mark = md5_hex(rand());
    # send the command and request endmarker
    print $socket "$line\nprint $mark\n";
    my $str;
    while(my $line = <$socket>){
      #read until end marker
      if($line =~ m/^$mark$/){
        chomp($str);
        return $str;
      } else {
        $str .= $line;
      }
    }
  }
}
 
# create object
my $query = Uzbl($path);
 
# send command
print $query->("@cmd");
uzblctrl-pl.txt · Last modified: 2016/08/31 14:10 (external edit)