Stack Apps is a question and answer site for apps, scripts, and development with the Stack Exchange API. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


This is a small userscript I made that shows the amount of characters in a code block above it. It is mainly for the site.


(This is just some random answer on codegolf.SE.)




$("pre code").each(function(){$(this).parent().before('<div style="border: 1px dotted #C0D4DB; border-bottom: none; border-radius: 5px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; padding: 3px; font: 12px monospace; display: inline; background-color: #F7FDFF; margin-left: 5px">'+$(this).text().trim().replace(/&amp;|&lt;|&gt;|&quot;|&#039;/g," ").length+" chars</div>")})

Type javascript: in the URL bar, and paste this after it. (or just paste it into the console)


share|improve this question
I wrote a similar script a long time back but later abandoned it. I like the style of yours. – Nathan Osman Oct 19 '13 at 3:57
Just a bit of clarification if you don't mind: /&amp;|&lt;|&gt;|&quot;|&#039;/g. Wouldn't this not count characters when someone uses html entities for codegolf? – Mhmd Feb 15 '14 at 18:08
@user689 It replaces them with spaces, so it still counts them. – Doorknob Feb 15 '14 at 18:10
But it would count them as a 1 character and not 4. Why are you using this in the first place? – Mhmd Feb 15 '14 at 18:12
@user689 Because they are one character, not 4. If I didn't have that they would be counted as 4. – Doorknob Feb 15 '14 at 18:12
Thanks I didn't know about that. – Mhmd Feb 15 '14 at 18:13

You must log in to answer this question.

Browse other questions tagged .