Hacker News new | past | comments | ask | show | jobs | submit login
For HN: Polls chart bookmarklet (and GreaseMonkey script)
20 points by bd on Mar 23, 2009 | hide | past | web | favorite | 7 comments
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.

Just copy & paste following code into url field of newly created bookmark:

---------

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<labels.length;i++)lab.push(labels[i].innerHTML);var%20num=[];for(var%20i=0;i<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&chd=t:"+chd+"&chds=0,"+chdmax+"&cht=p&chl="+chl+"&chf=bg,s,FFFFFF00";img.style.padding="1em";poll_table.parentNode.insertBefore(img,poll_table);})()

---------

Or, if you want to change something more easily, you can also use a version that loads a script from an external source:

---------

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');

---------

Note: Scraping is pretty fragile, it can break with any change of HN's html formatting.




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


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



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.


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... (GreaseMonkey)

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


Both work beautifully! Thanks!


cool




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: