Current version:

The current version of the script is 0.37.


I was inspired by this Meta post to write a UserScript that would automatically tally up the size of code blocks and display the count underneath.

A couple weeks later and I now have a giant UserScript that is over 32kb in size :)

Well, how can I install it?

I'm glad you asked! Since this is a UserScript, a few browsers allow you to install it by simply clicking on the link below and downloading it.

http://files.quickmediasolutions.com/codegolf/count_it.user.js

I have tested the script on Chrome (needs this workaround), Firefox (with GreaseMonkey), and Opera. It is reported to work on Safari using GreaseKit.

What does it do exactly?

This list seems to grow every day, but here is a (somewhat) exhaustive list:

  • Analysis and display of exact character count of each code entry (with syntax highlighting):

    Note: you can even directly run some entries by clicking the '[Run]' link

  • Summary of entries for a particular question, including the current winner(s) for shortest code:

  • The ability to sort the answers on the page by the size of their code:

  • An evolving options menu for changing the settings for the script:

  • An experimental theme that can be injected onto the pages of the site:

What about updates?

Yes, this humble UserScript is now capable of updating itself. Every 24 hours, it checks to see if there is a new version available. If so, it will prompt you to download it. You will have the option of choosing not to install the update if you don't want to.

Version History:

  • 0.37 fixed support for some languages, deleted answers no longer count
  • 0.36 added syntax highlighting
  • 0.35 added warning for >30 answers, added live preview
  • 0.34 added ability to run certain code entries
  • 0.33 added menu item for reporting problems with the current page
  • 0.32 made enhancements to theme, added more settings, changed about box
  • 0.31 added options to dialog, added experimental theme
  • 0.30 more reorganization, added smooth scrolling
  • 0.29 complete reorganization of code, added HTML5 local storage support
  • 0.28 added settings menu to top of page, fixed non-unique update URL problem
  • 0.27 fixed script grabbing extra text for username field, added support for <h3> elements
  • 0.26 added support for code-bowling posts
  • 0.25 script now only runs on questions with code-golf tag
  • 0.24 added DC programming language, fixed tab showing up on wrong pages
  • 0.23 added special handler for C++ code entries
  • 0.22 fixed links with new sort order, fixed multiple tied winners problem
  • 0.21 added new sorting option
  • 0.20 support for Firefox and Opera added, fixed jQuery include bug
  • 0.19 added username, added more languages
  • 0.18 added language to header, reorganized language code
  • 0.17 fixed jQuery load bug, detects HQ9+ entries, fixed off-by-one count errors
  • 0.16 fixed comment bug, moved header block, only first code block qualifies
  • 0.15 added auto-update support
share
    
Ideas for improvement are welcome! –  Nathan Osman Feb 5 '11 at 1:47
    
Clever. Alas it doesn't know the difference between when I inserted a line break because this was a c pre-processor line, when I inserted an unnecessary line break to so the code would fit in display and when I inserted a line break so the code would fit in the display but in lieu of a space. –  dmckee Feb 5 '11 at 2:07
    
@dmckee: Can you give me a link to that post? I'd like to make this script as clever as possible. –  Nathan Osman Feb 5 '11 at 2:49
    
@Geaorge: now I have to admit that I didn't ru the code. I merely inspected it. But I do generate three different kinds of line breaks in my c code; two types need to be counted and one should not be. Actually, I most only generated cpp lines which should be counted and other that shouldn't to avoid the problem when I "fix up" wc's count. I'll run it and report back. –  dmckee Feb 5 '11 at 2:54
    
It isn't running for me. Firefox 3.6.13, Greesemonkey 0.9.1, everything enable, reload the page and the error console says " Error: Source File: file:///Users/dmckee/Library/ Application%20Support/Firefox/Profiles/ r5dicgzx.default/gm_scripts/code_golf_se_counter/code_golf_se_counter.user.js Line: 19" –  dmckee Feb 5 '11 at 3:31
    
dmckee: Looking into it... –  Nathan Osman Feb 5 '11 at 3:33
    
@George, it may be Greesemonkey's fault. I see some comments at addons.mozilla.org/en-US/firefox/addon/greasemonkey that seem to blame 0.9.1. –  dmckee Feb 5 '11 at 3:47
    
@dmckee: Just in case, I have pushed a new update. Also, the script will automatically check for updates now. –  Nathan Osman Feb 5 '11 at 4:06
    
@George, when calculating "Note: the shortest piece of code on this page is currently XX bytes.", could you only use first code block on each answer? pls take a look this question, if you are not sure what I mean. –  YOU Feb 5 '11 at 7:18
4  
We really need this to be a built-in feature –  marcog Feb 5 '11 at 8:16
    
@S.Mark: I most certainly can. Look for it in the next release. –  Nathan Osman Feb 5 '11 at 18:02
2  
@recursive: You need to use these instructions to install UserScripts in Opera. –  Nathan Osman Aug 17 '11 at 18:53
3  
Where can I download Code Golf UserScript from? The source link return no file. Is there somewhere a new project page? –  avall Dec 30 '13 at 0:22
6  
The userscript is unavailable from the links above. –  Xantix Feb 16 at 15:35
1  
@Xantix I noticed that too, I only just stumbled upon this script again after a few years and it would be great to be able to use it! –  nyuszika7h Apr 29 at 11:14

9 Answers 9

Please support binary hex dumps counter. an example here, and copy-pasted as follows.

0000000: 332c 7b3a 533b 2e7b 3438 2d22 5e70 285d  3,{:S;.{48-"^p(]
0000010: 7025 d3c4 4ab1 7d4a b8dc 4469 ce41 2222  p%..J.}J..Di.A""
0000020: f303 227b 6261 7365 7d2f 3330 2f53 3d33  .."{base}/30/S=3
0000030: 2f3d 7b22 5f20 7c22 3d7d 257d 256e 407d  /={"_ |"=}%}%n@}
0000040: 2f3b                                     /;

My current idea is that, if the .text() is startswith /^0{3,}: / which is 0000000: then do
.text().match(/ [\da-z]{2}|[\da-z]{2}(?= )/gim).length, which gave me 66 chars.

There is another one here with slightly different format. Should work too, but I havn't tested well that though :)

share
    
Cool. I'll certainly add that feature when I get a chance. –  Nathan Osman Feb 18 '11 at 5:40

It looks like the userscript and the new 'Grainy' theme have a small issue. All inline code blocks are displaying as block making some comments with suggestions for corrections etc. really long. Example 1, example 2. It's not serious - nothing's actually broken - but I thought I'd let you know.

As a temporary workaround until this is officially fixed, you can edit the script source (via Manage User Scripts... from the Greasemonkey menu) and add the following line somewhere after the header:

GM_addStyle("code.prettyprint { display: inline; }");
share
    
Pictures on a meta post I should not have made. –  dmckee Mar 26 '12 at 20:30
    
Yeah, I found that pretty annoying too, so I came up with a workaround. As far as I can tell, it doesn't seem to be causing any unwanted side effects. –  Ilmari Karonen Mar 27 '12 at 10:03

When you extract username, could you strip revision part?

for example, Forecast the weather

Current winner: 21 revsJoey's [Unknown] entry has 103 characters.

probably, .replace(/[^<]+<br>/,'') will work.

share
    
Good catch. I have fixed that and will issue a new release shortly. –  Nathan Osman Feb 14 '11 at 6:55

Dealing with line breaks correctly is non-trivial.

Just looking at c-like languages there are at least three classes

  1. The newline ending a pre-processor line. These are required and should count.
  2. Newlines inserted only for limiting line length. They shouldn't count unless...
  3. Newlines for limiting line length inserted in lieu of a space that would otherwise be required which should count.

And telling the last two classes apart requires lexing the preceding and subsequent "words" with and without some whitespace to know if they can be safely jammed together. And that, of course, is language specific.

share
    
I totally see what you're saying (I'm a C++ programmer myself). It would be rather difficult to check each line ending in regards to these criterion, but it would be a nice feature. It may be a far-off-down-the-road feature. –  Nathan Osman Feb 7 '11 at 6:04
    
Changed my mind. This feature should be added very soon. –  Nathan Osman Feb 7 '11 at 21:59
    
There is an experimental version of this available now. –  Nathan Osman Feb 8 '11 at 3:58
    
@George: I'm impressed. Now I have to double check a couple of cases, because it looks like I counted them wrong. –  dmckee Feb 8 '11 at 5:42

I have added a menu to the top of every page now:

enter image description here

However, the options item is not functional. I was wondering if anyone had any ideas as far as configurable settings goes:

  • What settings do you want to be able to configure? For example, perhaps it would be nice to be able to add extra languages to the search list or to disable certain features?

  • How should these settings be stored? Should I continue using cookies, or turn to HTML5 local storage? Any thoughts?

share
    
How about setting the shortest sorting as default? You should use one of the jQuery storage plugins, that chooses whatever storage it can take. –  Juan Feb 15 '11 at 22:57
    
@Juan: jQuery storage? Hmmm... never thought of that - thanks! I'll be sure to make that an option. –  Nathan Osman Feb 15 '11 at 23:30
    
@Juan: There. v0.29 now uses HTML5 storage where possible. –  Nathan Osman Feb 16 '11 at 8:23
    
@Juan: With v0.32, that is now a configurable setting. –  Nathan Osman Feb 18 '11 at 5:47
    
My options don't appear to have any effect (don't save?)... I've tried with Chrome 18/Win 7 and FF (most recent version, not sitting at that PC)/Win Server 2k3 –  Gaffi Apr 11 '12 at 21:34
    
@Gaffi: I'll look into it. –  Nathan Osman Apr 11 '12 at 22:04
    
In fact, I know this may be a bit late, but the flaw is in the checking whether the checkboxes are checked: you're checking for .attr('checked')==true to give your answer, but .attr('checked') returns 'checked'. Not true. Just remove the ` == true` part and everything works completely fine! :D –  tomsmeding May 8 '13 at 14:05

Questions to consider:

Here are some things to be considered and discussed as this little script grows in functionality.

  • What languages do we want to skip? Someone on the chat mentioned that there are some languages (like HQ9+) that should be skipped and not counted when determining the smallest piece of code.

    Update: HQ9+ is now disqualified.

  • Does the character totaling code need adjustment? There are a few things to consider when totaling up the characters in a code snippet. Like: Should all newlines count?, Should trailing and leading newlines count?, Indentation?

Please comment below to voice your opinion on these issues.

share
1  
Is that HQ9+ exclusion really worth it? After all, there are only 4 (overestimated) problems it can be used in! –  J B Feb 6 '11 at 21:25
2  
@JB: Yes, but there may be more of these types of entries down the road. But perhaps a better way would be to exclude items with a score of < 0. Then we can all go and vote down the HQ9+ entry. (Not sure if that's really a solution.) –  Nathan Osman Feb 6 '11 at 21:28
    
For item #2, I think \n may matter a) depending on the language (in some languages, spaces/newlines matter - VBA for example), and b) depending on the challenge (some posters/askers specify this, some do not - perhaps if the site had more stringent guidelines?) –  Gaffi Apr 11 '12 at 21:37

Suggestion: Include scores for the posts.

Anarchy Golf gives 10000 points to the shortest answer(s) and after that the score is calculated as follows:

minimal solution length
----------------------- ⋅ 10000
   solution length

So a solution twice the length of the shortest one will have 5000 points.

That way you have a rough measure how close each solution is to the currently top-rated one.

share
    
Cool... I never heard of that before. –  Nathan Osman Feb 24 '11 at 0:55

Perhaps if the code provided is valid base64, your script should detect that and display the size it would have when decoded, which is easy to calculate: remove all the whitespace, remove the trailing = characters, multiply the length by 3 and divide by 4 (round down).

To give an example where it makes sense to post a base64-encoded answer, see my Perl script that uses unprintable bytes in the source.

share

On this question,

Physics golf: inclined shooting.

Current winner: George EdisonFUZxxl's Haskell 

You may use, .find('.user-details a:last').text(), since author is on the right most

share
    
Ah. Thank you for finding that. I forget that .text() combines the text from all matched elements. I'll get that fixed soon. –  Nathan Osman Feb 13 '11 at 8:09
    
There. I just released a fix for that - thanks! –  Nathan Osman Feb 14 '11 at 7:00
    
@George, Thanks. this one works, but other one still having issue for .replace(/[^<]+<br>/,'') may be you need to use .html instead of .text to strip first part of <br>? –  YOU Feb 14 '11 at 7:08
    
or you may try .replace(/[^\n]+\n/,'') instead of <br> with .text()? –  YOU Feb 14 '11 at 7:11
    
@SMark: The one you're linking to is working fine with the latest release... are you using 0.27? –  Nathan Osman Feb 14 '11 at 7:12
    
@George, really, I am seeing 21 revsJoey's [Unknown] in chrome win32, I just uninstalled and reinstalled it, still same, I guess .text() stripped <br> tag / or converted to \n –  YOU Feb 14 '11 at 7:13
    
same on firefox 3.6.12 win32 greasemonkey 0.9.0 too. –  YOU Feb 14 '11 at 7:18
    
@SMark: Well to be honest, I'm using .replace(/^\d+ revs/,'') because .text() strips the HTML out. –  Nathan Osman Feb 14 '11 at 7:18
    
@SMark: Can you open the Chrome console when you're on that page and type: $('#answer-443 .user-details:last a:last').text().replace(/^\d+ revs/,'') –  Nathan Osman Feb 14 '11 at 7:20
    
@George, Sorry You might be right, I am having issue with my cache. I could see Joey with your code in my chrome console –  YOU Feb 14 '11 at 7:24
    
@George, now looks good now. thank you. when your script try to autoupdate, could you add url with version numbers like ....user.js?0.27 or something else like random numbers? autoupdate script looks failed because of the cache. –  YOU Feb 14 '11 at 7:33
    
@SMark: Hmmm... I never thought of that. That's a good idea. I'll see about implementing that. –  Nathan Osman Feb 14 '11 at 7:35
    
@SMark: There. The update code on my server will supply unique URLs from now on. Hopefully this will not happen again. –  Nathan Osman Feb 14 '11 at 8:07
    
Thanks @George, that would be nice too. –  YOU Feb 14 '11 at 8:09
    
@SMark: You can try it out by triggering an update check. Simply delete the codegolf_lastcheck cookie and visit the home page. –  Nathan Osman Feb 14 '11 at 8:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .