You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

47 lines
1.3 KiB

  1. (function($) {
  2. wpWordCount = {
  3. settings : {
  4. strip : /<[a-zA-Z\/][^<>]*>/g, // strip HTML tags
  5. clean : /[0-9.(),;:!?%#$¿'"_+=\\/-]+/g, // regexp to remove punctuation, etc.
  6. count : /\S\s+/g // counting regexp
  7. },
  8. settingsEastAsia : {
  9. count : /[\u3100-\u312F\u31A0-\u31BF\u4E00-\u9FCF\u3400-\u4DBF\uF900-\uFAFF\u2F00-\u2FDF\u2E80-\u2EFF\u31C0-\u31EF\u2FF0-\u2FFF\u1100-\u11FF\uA960-\uA97F\uD780-\uD7FF\u3130-\u318F\uFFA0-\uFFDC\uAC00-\uD7AF\u3040-\u309F\u30A0-\u30FF\u31F0-\u31FF\uFF65-\uFF9F\u3190-\u319F\uA4D0-\uA4FF\uA000-\uA48F\uA490-\uA4CF]/g
  10. },
  11. block : 0,
  12. wc : function(tx) {
  13. var t = this, w = $('.word-count'), tc = 0;
  14. if ( t.block )
  15. return;
  16. t.block = 1;
  17. setTimeout( function() {
  18. if ( tx ) {
  19. // remove generally useless stuff first
  20. tx = tx.replace( t.settings.strip, ' ' ).replace( /&nbsp;|&#160;/gi, ' ' );
  21. // count east asia chars
  22. tx = tx.replace( t.settingsEastAsia.count, function(){ tc++; return ''; } );
  23. // count remaining western characters
  24. tx = tx.replace( t.settings.clean, '' );
  25. tx.replace( t.settings.count, function(){ tc++; return ''; } );
  26. }
  27. w.html(tc.toString());
  28. setTimeout( function() { t.block = 0; }, 2000 );
  29. }, 1 );
  30. }
  31. }
  32. $(document).bind( 'wpcountwords', function(e, txt) {
  33. wpWordCount.wc(txt);
  34. });
  35. }(jQuery));