wordcount.pl 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Copyright (C) 2005 Robin V. Stacey (robin@greywulf.net)
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the
  15. # Free Software Foundation, Inc.
  16. # 59 Temple Place, Suite 330
  17. # Boston, MA 02111-1307 USA
  18. # This module adds a wordcount to the bottom of edit boxes. The javascript code is munged from
  19. # Richard Livsey's Textarea Tools page: http://livsey.org/experiments/textareatools/
  20. # Though I've stripped it down to it's barest necessities
  21. use strict;
  22. use v5.10;
  23. our (@MyInitVariables, $HtmlHeaders, $EditNote);
  24. AddModuleDescription('wordcount.pl', 'Word Count Extension');
  25. push(@MyInitVariables, \&WordcountAddScript);
  26. sub WordcountAddScript {
  27. $HtmlHeaders .= "<script type='text/javascript'>
  28. function addEvent(obj, evType, fn) {
  29. if (obj.addEventListener) {
  30. obj.addEventListener(evType, fn, true);
  31. return true;
  32. } else if (obj.attachEvent) {
  33. var r = obj.attachEvent('on'+evType, fn);
  34. return r;
  35. } else { return false; }
  36. }
  37. addEvent(window, 'load', function() {
  38. document.getElementById('textWordCount').innerHTML = numWords(document.getElementById('text').value);
  39. document.getElementById('text').onkeyup = function() {
  40. document.getElementById('textWordCount').innerHTML = numWords(document.getElementById('text').value);
  41. }
  42. });
  43. function numWords(string) {
  44. string = string + ' ';
  45. string = string.replace(/^[^A-Za-z0-9]+/gi, '');
  46. string = string.replace(/[^A-Za-z0-9]+/gi, ' ');
  47. var items = string.split(' ');
  48. return items.length -1;
  49. }
  50. </script>";
  51. }
  52. $EditNote = "Words: <span id='textWordCount'></span>" . $EditNote;