wordCount.xpi – Part 1

So, if you recall, I was asked to write a Firefox extension that would do word counting on websites.

Originally, when I started this project, I set a goal for myself:  I copied the text from Project Gutenberg’s First Folio version of Shakespeare’s Hamlet into OpenOffice Writer, recorded the word/line/character count statistics, and set that as my projected goal for my first iteration of my extension.

But there’s a problem with this approach:  I’m supposed to be copying the behaviour of Unix’s wc, not OpenOffice Writer’s word count.  Normally, this wouldn’t be a problem – a word count is a word count, a line count is a line count, and Writer should pump out the same numbers as wc.

Not so.

In my last post, I wrote:

According to OpenOffice Writer, this text has 32230 words, 173543 characters, and 4257 lines.

However, upon passing the same text (saved in the textfile “count.txt”) through wc, I got the following output:

5302 32230 178845 count.txt

Writer and wc agree on the number of words, but disagree on the number of lines – 5302 (wc) vs 4257 (Writer).  It’s a disagreement of about a thousand lines.

Brutal.

Anyhow, I’m going to focus on wc’s approach to line counting – simply returning the number of newline characters in the file.

And guess what…it works.  For Hamlet, my extension pumps out:

Document statistics:

Word Count:  32230
Line Count:  5302
Character Count:  178845
Character Count (no spaces):  142368

Nice.

Hamlet’s just the simple case though.  There are plenty of other cases to consider, but this is a start.

Anyhow, download here.

In this version, I’m using Mozilla’s TreeWalker implementation to stitch together the page text.  So far it seems to be working alright, but if it somehow ends up falling through, I might end up using something like Andrew Trusty’s code with the jQuery library to do the text stitching.

So there it is.  Maybe I’ll keep working on this, pretty it up a bit, etc.  However, work starts on Monday, and that’ll probably take up most of my technical attention.

We’ll see though.

4 thoughts on “wordCount.xpi – Part 1

  1. Mike

    @Greg I haven’t tested it, but I can’t see why not. When the page is updated, the DOM Tree is updated, and that’s what I’m stitching together the page text with. There’s no caching, so it’s doing the count every time the button is pressed.

    So, a short, tentative answer: yes, I think so.

  2. David Liontooth

    Hi Mike,

    I like your approach and attempted to install the word count plugin, but no dice — it’s not compatible with Firefox 3.6.3.

    I’m sure you’re a busy guy, but if you could keep this project live you would be making a big contribution.

    Cheers,
    Dave

  3. Mike

    David:

    I think the only thing preventing it from working with Firefox 3.6.3 is the maxVersion setting in the install.rdf file.

    Try the following:

    -Download the wordcount.xpi file
    -Rename the file to wordcount.zip, and extract
    -Open the install.rdf file, and change the maxVersion setting to 3.6.*
    -Save install.rdf, rezip, and rename to wordcount.xpi
    -Drag wordcount.xpi into Firefox window to install.

    See if that works.

    -Mike

Comments are closed.