2014-01-13 12:47:24 -05:00
|
|
|
/* global discourseUrl:true */
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
|
|
|
|
// Discover the URLs we want counts for
|
|
|
|
var links = document.getElementsByTagName("a"),
|
|
|
|
countFor = [];
|
|
|
|
|
|
|
|
for(var i=0; i<links.length; i++) {
|
2014-01-13 13:37:55 -05:00
|
|
|
var link = links[i],
|
|
|
|
href = link.href;
|
2014-01-13 12:47:24 -05:00
|
|
|
if (href && href.length) {
|
2014-01-13 13:37:55 -05:00
|
|
|
if (/#discourse-comments$/.test(href)) {
|
|
|
|
countFor.push(href);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var dataEmbed = link.getAttribute('data-discourse-embed-url');
|
|
|
|
if (dataEmbed && dataEmbed.length) {
|
|
|
|
countFor.push(dataEmbed);
|
2014-01-13 12:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-13 13:37:55 -05:00
|
|
|
|
2014-01-13 12:47:24 -05:00
|
|
|
// JSONP callback to update counts
|
|
|
|
window.discourseUpdateCounts = function(result) {
|
|
|
|
if (result && result.counts) {
|
|
|
|
var byUrl = result.counts;
|
|
|
|
for (var i=0; i<links.length; i++) {
|
|
|
|
var link = links[i],
|
2014-03-20 15:37:00 -04:00
|
|
|
linkCount = byUrl[link.href] || byUrl[link.href.replace(/\/#/, '#')];
|
2014-01-13 12:47:24 -05:00
|
|
|
|
|
|
|
if (linkCount) {
|
|
|
|
var t = document.createTextNode(" (" + linkCount + ")");
|
|
|
|
link.appendChild(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (countFor.length > 0) {
|
|
|
|
// Send JSONP request for the counts
|
2014-05-20 15:20:02 -04:00
|
|
|
var d = document.createElement('script'),
|
|
|
|
srcUrl = discourseUrl + "embed/count?callback=discourseUpdateCounts";
|
2014-01-13 12:47:24 -05:00
|
|
|
|
|
|
|
for (var j=0; j<countFor.length; j++) {
|
2014-05-20 15:20:02 -04:00
|
|
|
srcUrl += "&" + "embed_url[]=" + encodeURIComponent(countFor[j]);
|
2014-01-13 12:47:24 -05:00
|
|
|
}
|
2014-05-20 15:20:02 -04:00
|
|
|
d.src = srcUrl;
|
2014-01-13 12:47:24 -05:00
|
|
|
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(d);
|
|
|
|
}
|
|
|
|
|
2014-05-20 15:20:02 -04:00
|
|
|
})();
|