29 באפר׳ 2009

WC - word count and char count


Update 2: There was a silly typo in the code which created a bug in gmail. I fixed that now, just remove the old WC and re-add the one just below.

Update: I have improved the bookmarklet to work with all types of frames and iframes (and not only gmail ones). Bookmarklet and code are updated.

Have you ever needed to count words in a document that you're authoring, in an email or a web page? Or even better, count characters and make sure you conform to the 140 twitter de-facto standard?
You have to try this bookmarklet then: WC

Again I'm blogging in English on this blog which is usually kept Hebrew, but since the last post on the subject was Heb I think that's fine.

A few days ago I was asked to give a twintterview, a short interview in email for which every question has to be answered with only 140 characters. Sounds like fun, right? But now, what if you had to actually count the characters for each and every answer? Not so much fun now... and twitter can't help you b/c you're in gmail...
So I did the interview and then took the time to write a simple bookmarklet that does that - counts words and chars of the selected text on a web page.

Many other applications already have this simple functionality built-in (e.g. Word or Unix) but unfortunately I couldn't find the same functionality as a bookmarklet or something else available to my gmail web client. Why does it have to be a bookmarklet? Well, it doesn't have to be one but I prefer it to be a bookmarklet b/c I want it to be available for my gmail and I want it to be browser agnostic. So, no Firefox add-ons, no Explorer toolbar, just a simple and straight forward bookmarklet.

First I thought, hey this is such a simple and useful functionality, there must be dozens of bookmarklet of add-ons out there doing just that. I googled it and found a few, but none of them were good enough for me, so I took the liberty to write my own (and use them as reference). For example the first one I found only counted words, while what I really need is chars. None of the bookmarklets of greasmonkey scripts. extension etc actually worked with gmail which is really where I needed it.

The code is right here (a bit compacted) for your enjoyment

javascript:(function(){
// Function: finds selected text on document d.
// @return the selected text or null
function f(d){
var t;
if (d.getSelection) t = d.getSelection();
else if(d.selection) t = d.selection.createRange();
if (t.text != undefined) t = t.text;
if (!t || t=='') {
var a = d.getElementsByTagName('textarea');
for (var i = 0; i < a.length; ++i) {
if (a[i].selectionStart != undefined && a[i].selectionStart != a[i].selectionEnd) {
t = a[i].value.substring(a[i].selectionStart, a[i].selectionEnd);
break;
}
}
}
return t;
};
// Function: finds selected text in document d and frames and subframes of d
// @return the selected text or null
function g(d){
var t;
try{t = f(d);}catch(e){};
if (!t || t == '') {
var fs = d.getElementsByTagName('frame');
for (var i = 0; i < fs.length; ++i){
t = g(fs[i].contentDocument);
if(t && t.toString() != '') break;
}
if (!t || t.toString() == '') {
fs = d.getElementsByTagName('iframe');
for (var i = 0; i < fs.length; ++i){
t = g(fs[i].contentDocument);
if(t && t.toString() != '') break;
}
}
}
return t;
};
var t= g(document);
if (!t || t == '') alert('please select some text');
else alert('Chars: '+t.toString().length+'\nWords: '+t.toString().match(/(\S+)/g).length);
})()


Disclaimer:
- Tested on FF and Safari. Didn't test on IE, so could be buggy

2 תגובות:

vpsean אמר/ה...

Hey, great stuff....took me a moment to figure out where the comments are since it's all inn Hebrew!

The bookmarklet looks great, but any chance you could provide instructions on what to do with the code to get it on? I've personally never seen a bookmarklet and haven't the slightest clue as to what to do with the code.

Thanks!

--Sean Patrick Simpson
www.TheMindsetApprentice.com
Twitter @vpsean

Ran Tavory אמר/ה...

I just created a better version of the bookmarklet and added a short explanation how to install a bookmarklet
http://www.gbsheli.com/2009/05/count-improved-wc-bookmarklet.html