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

ספירת מילית ותווים WC


נתבקשתי לעשות ראיון-טוויטר, שזה אומר שכל תשובה לשאלה בראיון צריכה להיות באורך של לא יותר מ 140 תווים
את הראיון כבר השלמתי אבל סיימתי אותו בתחושת חוסר סיפוק. ולמה? מכיוון שלא הייתי בטוח שלכל התשובות אכן עמדתי במבחן ה 140. לחלק ספרתי אבל זה מתיש וברור שצריך כאן כלי אוטומטי.
חיפשתי משהו שסופר ומצאתי בערך, כלומר לא בדיוק מה שחיפשתי. אז קצת העתקתי והרבה שיפרתי וכתבתי את ה bookmarklet הבא ועכשיו אני הרבה יותר רגוע. אפשר לספור מילים ותווים ע"י סימון טקסט ולחיצה על ה bookmarklet

WC

מעניין שהאתגר הגדול יותר היה לגרום לו לעבוד ב gmail עם המערכת המופרעת של ה iframes שהולכת שם.
בדקתי בספארי ופיירפוקס. אין ברשותי אקספלורר, אז נותר רק להחזיק אצבעות, אבל אם יש באג נא לדווח.

הקוד במלואו, קצת מקומפקט להלן:


javascript:(function(){
var t,d=document;
if(location.href.indexOf('mail.google.com')>0){
d=d.getElementById('canvas_frame').contentDocument;
if(d.getElementsByTagName('iframe').length)d=d.getElementsByTagName('iframe')[0].contentDocument;
}
if(d.getSelection)t=d.getSelection();
else if(d.selection)t=d.selection.createRange();
if(t.text!=undefined)t=t.text;
if(!t||t==''){
a=document.getElementsByTagName('textarea');
for(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;
}
}
}
if(!t||t=='')alert('please select some text');
else{
alert('Chars: '+t.toString().length+'\nWords: '+t.toString().match(/(\S+)/g).length);
}
})()


10 באפר׳ 2009

לוח המשרות של רברס עם פלטפורמה


מזה מספר חודשים אורי ואני מקליטים פודקאסט לאנשי תכנה, רברס עם פלטפורמה. אנחנו נהנים מעצם העשייה ומהמפגש המעשיר עם האורחים שלנו, המאזינים והתגובות.
לאחרונה, עם התדרדרות מצב שוק העבודה החלטנו לעשות את שביכולתנו לסייע לאנשי תכנה מובטלים והקמנו את לוח המשרות של רוורס עם פלטפורמה.
לוח זה הוא שרות חינמי לחלוטין וללא כל התחייבות ומטרתו אחת: לשדך דורשי עבודה עם נותני עבודה. מה שמיוחד בלוח זה הוא שאני ואורי חושבים שיש הרבה שאלות שמעניינות דורשי עבודה בענף התכנה שאינן מקבלות מענה בלוחות המשרות הקיימים היום וזה מקשה על חיפוש העבודה ולכן כל מעסיק שמפרסם משרה מתבקש לענות על השאלון. מעבר לכך כיוון שהקהל שלנו מאוד ממוקד אנחנו מאמינים שביכולתנו לעשות שידוכים טובים. אז ללא שהיות מיותרות, מי שמחפש עבודה או מי שיש לו משרה להציע מוזמן לבקר את http://jobs.reversim.com נכון להיום יש כבר שלוש משרות פתוחות. תודה רבה לאדם מתן שלקח על עצמו את הפרוייקט הזה ועוזר לנו בבניית האתר.

אם מצאתם עבודה דרך הלוח אנא השאירו לנו הודעה או מייל - בטח נרצה לפרסם סיפורי הצלחה וגם להסיר את המשרה מהלוח.

מי שמחפש מהנדסי תוכנה מוצלחים- ניגש לאתר, ממלא את השאלון (בעברית או באנגלית) ושולח לאדם (שהתנדב לסייע במשימה החשובה הזו). משם אנחנו דואגים לפרסם אותה, לקדם אותה בבלוגים, בטוויטר וכמובן בפודקאסט.

בהצלחה לכולם ו... קדימה לעבודה.