21
\$\begingroup\$

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
\$\endgroup\$
71
  • \$\begingroup\$ Ideas for improvement are welcome! \$\endgroup\$ Commented Feb 5, 2011 at 1:47
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Feb 5, 2011 at 2:07
  • \$\begingroup\$ @dmckee: Can you give me a link to that post? I'd like to make this script as clever as possible. \$\endgroup\$ Commented Feb 5, 2011 at 2:49
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Feb 5, 2011 at 2:54
  • \$\begingroup\$ 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" \$\endgroup\$ Commented Feb 5, 2011 at 3:31
  • \$\begingroup\$ dmckee: Looking into it... \$\endgroup\$ Commented Feb 5, 2011 at 3:33
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Feb 5, 2011 at 3:47
  • \$\begingroup\$ @dmckee: Just in case, I have pushed a new update. Also, the script will automatically check for updates now. \$\endgroup\$ Commented Feb 5, 2011 at 4:06
  • 4
    \$\begingroup\$ We really need this to be a built-in feature \$\endgroup\$
    – moinudin
    Commented Feb 5, 2011 at 8:16
  • 1
    \$\begingroup\$ @George No, it says Uknown, and it should be Unknown. \$\endgroup\$
    – user344
    Commented Feb 23, 2011 at 21:21
  • 2
    \$\begingroup\$ @recursive: You need to use these instructions to install UserScripts in Opera. \$\endgroup\$ Commented Aug 17, 2011 at 18:53
  • 5
    \$\begingroup\$ Where can I download Code Golf UserScript from? The source link return no file. Is there somewhere a new project page? \$\endgroup\$
    – avall
    Commented Dec 30, 2013 at 0:22
  • 13
    \$\begingroup\$ The userscript is unavailable from the links above. \$\endgroup\$
    – Xantix
    Commented Feb 16, 2014 at 15:35
  • 1
    \$\begingroup\$ @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! \$\endgroup\$
    – user344
    Commented Apr 29, 2014 at 11:14
  • 2
    \$\begingroup\$ Script still unavailable. \$\endgroup\$ Commented Oct 3, 2016 at 13:35

9 Answers 9

4
\$\begingroup\$

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 :)

\$\endgroup\$
1
  • \$\begingroup\$ Cool. I'll certainly add that feature when I get a chance. \$\endgroup\$ Commented Feb 18, 2011 at 5:40
4
\$\begingroup\$

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; }");
\$\endgroup\$
2
  • \$\begingroup\$ Pictures on a meta post I should not have made. \$\endgroup\$ Commented Mar 26, 2012 at 20:30
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Mar 27, 2012 at 10:03
2
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Feb 7, 2011 at 6:04
  • \$\begingroup\$ Changed my mind. This feature should be added very soon. \$\endgroup\$ Commented Feb 7, 2011 at 21:59
  • \$\begingroup\$ There is an experimental version of this available now. \$\endgroup\$ Commented Feb 8, 2011 at 3:58
  • \$\begingroup\$ @George: I'm impressed. Now I have to double check a couple of cases, because it looks like I counted them wrong. \$\endgroup\$ Commented Feb 8, 2011 at 5:42
2
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Good catch. I have fixed that and will issue a new release shortly. \$\endgroup\$ Commented Feb 14, 2011 at 6:55
2
\$\begingroup\$

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?

\$\endgroup\$
7
  • \$\begingroup\$ How about setting the shortest sorting as default? You should use one of the jQuery storage plugins, that chooses whatever storage it can take. \$\endgroup\$
    – Champo
    Commented Feb 15, 2011 at 22:57
  • \$\begingroup\$ @Juan: jQuery storage? Hmmm... never thought of that - thanks! I'll be sure to make that an option. \$\endgroup\$ Commented Feb 15, 2011 at 23:30
  • \$\begingroup\$ @Juan: There. v0.29 now uses HTML5 storage where possible. \$\endgroup\$ Commented Feb 16, 2011 at 8:23
  • \$\begingroup\$ @Juan: With v0.32, that is now a configurable setting. \$\endgroup\$ Commented Feb 18, 2011 at 5:47
  • \$\begingroup\$ 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 \$\endgroup\$
    – Gaffi
    Commented Apr 11, 2012 at 21:34
  • \$\begingroup\$ @Gaffi: I'll look into it. \$\endgroup\$ Commented Apr 11, 2012 at 22:04
  • \$\begingroup\$ 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 \$\endgroup\$
    – tomsmeding
    Commented May 8, 2013 at 14:05
1
\$\begingroup\$

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.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Is that HQ9+ exclusion really worth it? After all, there are only 4 (overestimated) problems it can be used in! \$\endgroup\$
    – J B
    Commented Feb 6, 2011 at 21:25
  • 2
    \$\begingroup\$ @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.) \$\endgroup\$ Commented Feb 6, 2011 at 21:28
  • \$\begingroup\$ 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?) \$\endgroup\$
    – Gaffi
    Commented Apr 11, 2012 at 21:37
1
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Cool... I never heard of that before. \$\endgroup\$ Commented Feb 24, 2011 at 0:55
1
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
16
  • \$\begingroup\$ Ah. Thank you for finding that. I forget that .text() combines the text from all matched elements. I'll get that fixed soon. \$\endgroup\$ Commented Feb 13, 2011 at 8:09
  • \$\begingroup\$ There. I just released a fix for that - thanks! \$\endgroup\$ Commented Feb 14, 2011 at 7:00
  • \$\begingroup\$ @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>? \$\endgroup\$
    – YOU
    Commented Feb 14, 2011 at 7:08
  • \$\begingroup\$ or you may try .replace(/[^\n]+\n/,'') instead of <br> with .text()? \$\endgroup\$
    – YOU
    Commented Feb 14, 2011 at 7:11
  • \$\begingroup\$ @SMark: The one you're linking to is working fine with the latest release... are you using 0.27? \$\endgroup\$ Commented Feb 14, 2011 at 7:12
  • \$\begingroup\$ @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 \$\endgroup\$
    – YOU
    Commented Feb 14, 2011 at 7:13
  • \$\begingroup\$ same on firefox 3.6.12 win32 greasemonkey 0.9.0 too. \$\endgroup\$
    – YOU
    Commented Feb 14, 2011 at 7:18
  • \$\begingroup\$ @SMark: Well to be honest, I'm using .replace(/^\d+ revs/,'') because .text() strips the HTML out. \$\endgroup\$ Commented Feb 14, 2011 at 7:18
  • \$\begingroup\$ @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/,'') \$\endgroup\$ Commented Feb 14, 2011 at 7:20
  • \$\begingroup\$ @George, Sorry You might be right, I am having issue with my cache. I could see Joey with your code in my chrome console \$\endgroup\$
    – YOU
    Commented Feb 14, 2011 at 7:24
  • \$\begingroup\$ @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. \$\endgroup\$
    – YOU
    Commented Feb 14, 2011 at 7:33
  • \$\begingroup\$ @SMark: Hmmm... I never thought of that. That's a good idea. I'll see about implementing that. \$\endgroup\$ Commented Feb 14, 2011 at 7:35
  • \$\begingroup\$ @SMark: There. The update code on my server will supply unique URLs from now on. Hopefully this will not happen again. \$\endgroup\$ Commented Feb 14, 2011 at 8:07
  • \$\begingroup\$ Thanks @George, that would be nice too. \$\endgroup\$
    – YOU
    Commented Feb 14, 2011 at 8:09
  • \$\begingroup\$ @SMark: You can try it out by triggering an update check. Simply delete the codegolf_lastcheck cookie and visit the home page. \$\endgroup\$ Commented Feb 14, 2011 at 8:09

You must log in to answer this question.

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