3 ms·
For HN: Polls chart bookmarklet (and GreaseMonkey script)
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.
- bd 18y agoWhen 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 http://alteredqualia.com/visualization/hn/hnpoll.png For this poll: http://news.ycombinator.com/item?id=527681 http://news.ycombinator.com/item?id=527681
- entelarust 18y agocool
- lucumo 18y agoThat's pretty cool. Any chance of making it a GreaseMonkey script so it will be done automatically on HN?
- bd 18y agoHere it is: http://alteredqualia.com/visualization/hn/hnpollchart.user.js http://alteredqualia.com/visualization/hn/hnpollchart.user.j... And here some are some polls: http://www.google.com/search?hl=en&q=site:ycombinator.com+poll http://www.google.com/search?hl=en&q=site:ycombinator.co...
- lucumo 18y agoVery 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 18y agoThanks 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.js http://alteredqualia.com/visualization/hn/hnpollchart.user.j... (GreaseMonkey) http://alteredqualia.com/visualization/hn/hn_poll_graph.js http://alteredqualia.com/visualization/hn/hn_poll_graph.js (bookmarklet include)
- lucumo 18y agoBoth work beautifully! Thanks!