/** * Script to add a wordcounter on the edit form * https://www.dokuwiki.org/tips:wordcounter * * @author Andreas Gohr * @author Lars Flintzak * @license GPL 2 */ var wordcounter_timeout; function wordcounter(text){ var list = text.split(/[^\w\-_]+/); var len = list.length; if(list[len-1] == '') len--; if(list[0] == '') len--; if(len < 0) len=0; return len; } function charcounter(text){ var list = text.split(/[^\w\-_]+/); var len = text.length; if(list[len-1] == '') len--; if(list[0] == '') len--; if(len < 0) len=0; return len; } // replaced next line as per https://www.dokuwiki.org/devel:jqueryfaq [Rik, 2013-10-20] // addInitEvent(function(){ jQuery(function(){ // replaced next line as per https://www.dokuwiki.org/devel:jqueryfaq [Rik, 2013-10-20] // var form = $('dw__editform'); var $form = jQuery('#dw__editform'); var form = $form[0]; if(!form) return; var div = document.createElement('div'); div.id = 'word__counter__output'; div.style.position = 'absolute'; div.style.width = '80px'; div.style.right = '50px'; div.style.top = (findPosY(form.elements.wikitext)+10)+'px'; div.style.color = '#0a0'; form.appendChild(div); var all = wordcounter(form.elements.prefix.value); all += wordcounter(form.elements.suffix.value); var zei_all = charcounter(form.elements.prefix.value); zei_all += charcounter(form.elements.suffix.value); // replaced next line as per https://www.dokuwiki.org/devel:jqueryfaq [Rik, 2013-10-20] // addEvent(form.elements.wikitext,'keyup',function(){ jQuery(form.elements.wikitext).keyup(function(){ if(wordcounter_timeout) window.clearTimeout(wordcounter_timeout); wordcounter_timeout = window.setTimeout(function(){ var len = wordcounter(form.elements.wikitext.value); var zei = charcounter(form.elements.wikitext.value); div.innerHTML = 'Section:
'+ '  '+zei+' chars
'+ '  '+len+' words
'+ 'Total:
'+ '  '+(zei_all+zei)+' chars
'+ '  '+(all+len)+' words'; },1000); return true; }); });