views:

28

answers:

1

I want to create a bookmarklete that counts up all the text on a webpage and then displays the results from most to least it in a absolutely positioned div.

Every google search i've done talks about counting the total number of words in a form or text area or known div id. That's not what i want. I want the number of times each /w appears on the entire webpage.

I know enough javascript to know that i don't know how to do this.

+1  A: 

Something like this should work:

function countWordFrequency() {
  var freq={};
  // Traverse the DOM looking for text nodes.
  recurseTextNodes(function(textNode) {
    // Split the text into words, removing punctuation.
    var words = textNode.data.replace(/[^\w\s]/g, '').split(/\s+/)
      , len = words.length;
    // Count the word frequency.
    for (var i=0; i<len; i++) {
      if (freq[words[i]]) {
        freq[words[i]] += 1;
      } else  {
        freq[words[i]] = 1;
      }
    }
  });
  return freq;
}

This solution might be overly simple in the way that it removes punctuation and parses words but should demonstrate the idea. Also the recurseTextNodes function is left as an exercise to the reader =). There are also implications of how to store this routine as a bookmarklet (esp. how to display the results to the end-user) but again, I'll assume you've got some idea of how to do that.

maerics