

For HN: Polls chart bookmarklet (and GreaseMonkey script) - bd

I made a bookmarklet that embeds pie charts into Hacker News poll pages. It scrapes data from a poll page and creates a chart using Google Charts API.<p>Just copy &#38; paste following code into url field of newly created bookmark:<p>---------<p>javascript:(function(){var%20tables=document.getElementsByTagName("table");var%20poll_table=tables[3];var%20labels=poll_table.getElementsByTagName("font");var%20values=poll_table.getElementsByTagName("span");var%20lab=[];for(var%20i=0;i&#60;labels.length;i++)lab.push(labels[i].innerHTML);var%20num=[];for(var%20i=0;i&#60;values.length;i++){if(values[i].parentNode.className=="comhead")num.push(/\d+/.exec(values[i].innerHTML))}var%20chl=lab.join("|");var%20chd=num.join(",");var%20chdmax=Math.max.apply(Math,num);var%20img=document.createElement('IMG');img.src="http://chart.apis.google.com/chart?chs=500x200&#38;chd=t:"+chd+"&#38;chds=0,"+chdmax+"&#38;cht=p&#38;chl="+chl+"&#38;chf=bg,s,FFFFFF00";img.style.padding="1em";poll_table.parentNode.insertBefore(img,poll_table);})()<p>---------<p>Or, if you want to change something more easily, you can also use a version that loads a script from an external source:<p>---------<p>javascript:function%20loadScript(scriptURL){var%20s=document.createElement('SCRIPT');s.setAttribute('language','JavaScript');%20s.setAttribute('src',scriptURL);document.body.appendChild(s);}loadScript('http://alteredqualia.com/visualization/hn/hn_poll_graph.js');<p>---------<p>Note: Scraping is pretty fragile, it can break with any change of HN's html formatting.
======
bd
When you are on some HN poll page, clicking on this bookmark should create a
pie chart just above the poll options.

For example, here is how it should look like:

<http://alteredqualia.com/visualization/hn/hnpoll.png>

For this poll:

<http://news.ycombinator.com/item?id=527681>

------
lucumo
That's pretty cool. Any chance of making it a GreaseMonkey script so it will
be done automatically on HN?

~~~
bd
Here it is:

[http://alteredqualia.com/visualization/hn/hnpollchart.user.j...](http://alteredqualia.com/visualization/hn/hnpollchart.user.js)

And here some are some polls:

[http://www.google.com/search?hl=en&q=site:ycombinator.co...](http://www.google.com/search?hl=en&q=site:ycombinator.com+poll)

~~~
lucumo
Very cool! Thanks!

I did notice a little bug, though. If you've started a poll, it thinks the *
marker of your own vote is a poll option. I didn't happen to me on other
polls, just on the one I started.

~~~
bd
Thanks for noticing. I never started a poll, so I don't have a test case. I
made a fix based on how the * marker is rendered elsewhere.

Please try if it works for you:

[http://alteredqualia.com/visualization/hn/hnpollchart.user.j...](http://alteredqualia.com/visualization/hn/hnpollchart.user.js)
(GreaseMonkey)

<http://alteredqualia.com/visualization/hn/hn_poll_graph.js> (bookmarklet
include)

~~~
lucumo
Both work beautifully! Thanks!

------
entelarust
cool

