diff --git a/js/thread-stats.js b/js/thread-stats.js index 8873dbdb..a535d499 100644 --- a/js/thread-stats.js +++ b/js/thread-stats.js @@ -2,115 +2,138 @@ * thread-stats.js * - Adds statistics of the thread below the posts area * - Shows ID post count beside each postID on hover - * + * * Usage: * $config['additional_javascript'][] = 'js/jquery.min.js'; * $config['additional_javascript'][] = 'js/thread-stats.js'; */ + if (active_page == 'thread') { -$(document).ready(function(){ - //check if page uses unique ID - var IDsupport = ($('.poster_id').length > 0); - var thread_id = (document.location.pathname + document.location.search).split('/'); - thread_id = thread_id[thread_id.length -1].split('+')[0].split('-')[0].split('.')[0]; - - $('.boardlist.bottom, footer') - .first() - .before('
'); - var el = $('#thread_stats'); - el.prepend(_('Page')+' ?'); - if (IDsupport){ - el.prepend('0 UIDs | '); - } - el.prepend('0 '+_('images')+' | '); - el.prepend('0 '+_('replies')+' | '); - delete el; - function update_thread_stats(){ - var op = $('#thread_'+ thread_id +' > div.post.op:not(.post-hover):not(.inline)').first(); - var replies = $('#thread_'+ thread_id +' > div.post.reply:not(.post-hover):not(.inline)'); - // post count - $('#thread_stats_posts').text(replies.length); - // image count - $('#thread_stats_images').text(replies.filter(function(){ - return $(this).find('> .files').text().trim() != false; - }).length); - // unique ID count - if (IDsupport) { - var opID = op.find('> .intro > .poster_id').text(); - var ids = {}; - replies.each(function(){ - var cur = $(this).find('> .intro > .poster_id'); - var curID = cur.text(); - if (ids[curID] === undefined) { - ids[curID] = 0; - } - ids[curID]++; - }); - if (ids[opID] === undefined) { - ids[opID] = 0; - } - ids[opID]++; - var cur = op.find('>.intro >.poster_id'); - cur.find('+.posts_by_id').remove(); - cur.after(' ('+ ids[cur.text()] +')'); - replies.each(function(){ - cur = $(this).find('>.intro >.poster_id'); - cur.find('+.posts_by_id').remove(); - cur.after(' ('+ ids[cur.text()] +')'); - }); - var size = function(obj) { - var size = 0, key; - for (key in obj) { - if (obj.hasOwnProperty(key)) size++; - } - return size; - }; - $('#thread_stats_uids').text(size(ids)); + $(document).ready(function() { + // Check if page uses unique ID. + let idSupport = ($('.poster_id').length > 0); + let threadId = (document.location.pathname + document.location.search).split('/'); + threadId = threadId[threadId.length -1].split('+')[0].split('-')[0].split('.')[0]; + + $('.boardlist.bottom, footer') + .first() + .before(''); + + let el = $('#thread_stats'); + el.prepend(_('Page')+ ' ?'); + if (idSupport) { + el.prepend('0 UIDs | '); } - var board_name = $('input[name="board"]').val(); - $.getJSON('//'+ document.location.host +'/'+ board_name +'/threads.json').success(function(data){ - var found, page = '???'; - for (var i=0;data[i];i++){ - var threads = data[i].threads; - for (var j=0; threads[j]; j++){ - if (parseInt(threads[j].no) == parseInt(thread_id)) { - page = data[i].page +1; - found = true; + el.prepend('0 ' +_('images')+ ' | '); + el.prepend('0 ' +_('replies')+ ' | '); + delete el; + + function updateThreadStats() { + let op = $('#thread_' + threadId + ' > div.post.op:not(.post-hover):not(.inline)').first(); + let replies = $('#thread_' + threadId + ' > div.post.reply:not(.post-hover):not(.inline)'); + // Post count. + $('#thread_stats_posts').text(replies.length); + // Image count. + $('#thread_stats_images').text(replies.filter(function() { + return $(this).find('> .files').text().trim() != false; + }).length); + + // Unique ID count. + if (idSupport) { + let opID = op.find('> .intro > .poster_id').text(); + let ids = {}; + replies.each(function() { + let cur = $(this).find('> .intro > .poster_id'); + let curID = cur.text(); + if (ids[curID] === undefined) { + ids[curID] = 0; + } + ids[curID]++; + }); + + if (ids[opID] === undefined) { + ids[opID] = 0; + } + ids[opID]++; + + let cur = op.find('>.intro >.poster_id'); + cur.find(' +.posts_by_id').remove(); + cur.after(' (' + ids[cur.text()] + ')'); + replies.each(function() { + cur = $(this).find('>.intro >.poster_id'); + cur.find(' +.posts_by_id').remove(); + cur.after(' (' + ids[cur.text()] + ')'); + }); + let size = function(obj) { + let size = 0; + for (key in obj) { + if (obj.hasOwnProperty(key)) { + size++; + } + } + return size; + }; + $('#thread_stats_uids').text(size(ids)); + } + + let boardName = $('input[name="board"]').val(); + $.getJSON('//' + document.location.host + '/' + boardName + '/threads.json').success(function(data) { + let found, page = '???'; + for (let i = 0; data[i]; i++) { + let threads = data[i].threads; + for (let j = 0; threads[j]; j++) { + if (parseInt(threads[j].no) == parseInt(threadId)) { + page = data[i].page +1; + found = true; + break; + } + } + if (found) { break; } } - if (found) break; - } - $('#thread_stats_page').text(page); - if (!found) $('#thread_stats_page').css('color','red'); - else $('#thread_stats_page').css('color',''); - }); - } - // load the current page the thread is on. - // uses ajax call so it gets loaded on a delay (depending on network resources available) - var thread_stats_page_timer = setInterval(function(){ - var board_name = $('input[name="board"]').val(); - $.getJSON('//'+ document.location.host +'/'+ board_name +'/threads.json').success(function(data){ - var found, page = '???'; - for (var i=0;data[i];i++){ - var threads = data[i].threads; - for (var j=0; threads[j]; j++){ - if (parseInt(threads[j].no) == parseInt(thread_id)) { - page = data[i].page +1; - found = true; + $('#thread_stats_page').text(page); + if (!found) { + $('#thread_stats_page').css('color', 'red'); + } else { + $('#thread_stats_page').css('color', ''); + } + }); + } + + // Load the current page the thread is on. + // Uses ajax call so it gets loaded on a delay (depending on network resources available). + let thread_stats_page_timer = setInterval(function() { + let boardName = $('input[name="board"]').val(); + $.getJSON('//' + document.location.host + '/' + boardName + '/threads.json').success(function(data) { + let found = false; + let page = '???'; + for (let i = 0; data[i]; i++) { + let threads = data[i].threads; + for (let j = 0; threads[j]; j++) { + if (parseInt(threads[j].no) == parseInt(threadId)) { + page = data[i].page +1; + found = true; + break; + } + } + if (found) { break; } } - if (found) break; - } - $('#thread_stats_page').text(page); - if (!found) $('#thread_stats_page').css('color','red'); - else $('#thread_stats_page').css('color',''); - }); - },30000); + + $('#thread_stats_page').text(page); + if (!found) { + $('#thread_stats_page').css('color', 'red'); + } else { + $('#thread_stats_page').css('color', ''); + } + }); + }, 30000); + $('body').append(''); - update_thread_stats(); - $('#update_thread').click(update_thread_stats); - $(document).on('new_post',update_thread_stats); -}); + updateThreadStats(); + $('#update_thread').click(updateThreadStats); + $(document).on('new_post', updateThreadStats); + }); }