I uploaded my Firefox bookmarks.html file to the website, so that I could access it from anywhere; I have about a million of them, and it's useful to bring them up at work. The file isn't very user-friendly in its raw form.
So I decided to parse the file, extract the icons from it (they're in there) and add some script to expand and collapse the folders. I could get a little practice with regular expressions along the way.
// Rip the icon data out and replace with images
// I match to the end of the anchor tag so I can use the length
// to put the image at the end of the tag.
Regex regex = new Regex("ICON=\"data:image/x-icon;base64,(.*?)\".*?\\>");
i = 0;
icon = 1;
Match match = regex.Match(file, i);
while (match != null && match.Length != 0)
{
Debug.WriteLine(match.Value);
iconname = ExtractIcon(match.Groups[1].Value, dest, icon);
file = file.Insert(match.Index + match.Length, string.Format(
"<img src=\"{0}\" border=\"0\" width=16 height=16> ", iconname));
icon++;
i = match.Index + match.Length;
match = null;
if (i < file.Length)
{
match = regex.Match(file, i);
}
}
The image is in the .ico format, so it can be streamed right into a file with that extension:
private static string ExtractIcon(string data, string folder, int index)
{
int i;
string filename;
// Open a binary file for writing
filename = string.Format("bookmark_icon_{0}.ico", index);
BinaryWriter bw = new BinaryWriter(
File.Open(Path.Combine(folder, filename), FileMode.Create));
bw.Write(Convert.FromBase64String(data));
bw.Flush();
bw.Close();
return filename;
}
Piece of cake. The javascript for showing and hiding the DIVs is pretty straightforward; a little more regex does the trick. Full code for console application here.
In Firefox, image tags can contain data in base64 format. This isn't supported in IE6, but I've tested it in IE7 beta, and that does support it. Unfortunately, I won't have that at work until it's officially released.