Like many, I use a password manager for all my web password. KeePass allows you to add custom icons to each entry, so I thought I would use the favicon that comes with each site. The problem is that, at least in FireFox, the icons are saved in the bookmarks file, base-64 encoded, so there is no easy way to get them. Also KeePass lists the names of the icon files, but doesn't seem to have a preview, so each icon needs to be properly named.

I use the code below to extract the icons, eliminate duplicates, and save each one to a properly named file in the current directory.

I apologize for using XML::Twig (again!), but that's the module I know best.

To get all the files, export your bookmarks in an empty directory, then run the code there.

#!/usr/bin/perl use strict; use warnings; use XML::Twig; use MIME::Base64; my $HEADER= "data:image/([^:]*);base64,"; # image header # if not listed here the type is used as extension (eg png, or jpeg) my %type2ext= ( 'x-icon' => 'ico'); my %seen; # used to avoid duplicates, both for sites and for icons XML::Twig->new( twig_handlers => { 'dt/a[@icon]' => \&save_icon }) ->parsefile_html( 'bookmarks.html'); sub save_icon { my( $t, $a)= @_; my $url= $a->att('href'); my $site= url2name( $url); return if( $seen{$site}); $seen{$site}=1; my $icon= $a->att( 'icon'); return unless $icon=~ s{^$HEADER}{}; # skip if icon is not base-64 + encoded my $icon_type= $1; return if( $seen{$icon}); $seen{$icon}=1; my $ext= $type2ext{$icon_type} || $icon_type; my $file= join '.', $site, $ext; $icon= decode_base64( $icon); warn "adding icon for $site ($icon_type) to $file\n"; open( my $out, '>:raw', $file) or die "cannot create file $file: $ +!"; print {$out} $icon; } # takes a full url and returns a short version of the site name # eg http://http://perlmonks.org/?node=Newest%20Nodes => perlmonks sub url2name { my( $url)= @_; $url=~ s{^https?://}{}; # remove protocol $url=~ s{^www.}{}; # no need for the www. bit $url=~ s{/.*$}{}; # keep only the site name $url=~ s{\.[^.]*$}{}; # ermove the tld return $url; }

In reply to Extracting icons from a bookmark file by mirod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.