diff --git a/js/thread-stats.js b/js/thread-stats.js index ecdf527a..43847db1 100644 --- a/js/thread-stats.js +++ b/js/thread-stats.js @@ -12,8 +12,8 @@ if (active_page == 'thread') { $(document).ready(function() { // Check if page uses unique ID. let IDsupport = ($('.poster_id').length > 0); - let thread_id = (document.location.pathname + document.location.search).split('/'); - thread_id = thread_id[thread_id.length -1].split('+')[0].split('.')[0]; + let threadId = (document.location.pathname + document.location.search).split('/'); + threadId = threadId[threadId.length -1].split('+')[0].split('.')[0]; $('.clear').before('
'); @@ -26,9 +26,37 @@ if (active_page == 'thread') { el.prepend('| 0 replies | '); delete el; - function update_thread_stats() { - let op = $('#thread_' + thread_id).find('div.post.op:not(.post-hover):not(.inline)').first(); - let replies = $('#thread_' + thread_id).find('div.post.reply:not(.post-hover):not(.inline)'); + function fetchPageNumber() { + $.getJSON('//' + document.location.host + '/' + boardName + '/threads.json', function(data) { + let found; + let page = '???'; + let threadIdInt = parseInt(threadId); + for (let i = 0; data[i]; i++) { + let threads = data[i].threads; + for (let j = 0; threads[j]; j++) { + if (parseInt(threads[j].no) === threadIdInt) { + page = data[i].page +1; + found = true; + break; + } + } + if (found) { + break; + } + } + let threadStatsPage = $('#thread_stats_page'); + threadStatsPage.text(page); + if (!found) { + threadStatsPage.css('color', 'red'); + } else { + threadStatsPage.css('color', ''); + } + }); + } + + function updateThreadStats() { + let op = $('#thread_' + threadId).find('div.post.op:not(.post-hover):not(.inline)').first(); + let replies = $('#thread_' + threadId).find('div.post.reply:not(.post-hover):not(.inline)'); // Post count. $('#thread_stats_posts').text(replies.length); // Image count. @@ -70,61 +98,17 @@ if (active_page == 'thread') { }; $('#thread_stats_uids').text(size(ids)); } - $.getJSON('//' + document.location.host + '/' + board_name + '/threads.json', function(data) { - let found; - let page = '???'; - let threadId = parseInt(thread_id); - for (let i = 0; data[i]; i++) { - let threads = data[i].threads; - for (let j = 0; threads[j]; j++) { - if (parseInt(threads[j].no) === threadId) { - page = data[i].page +1; - found = true; - break; - } - } - if (found) { - break; - } - } - let threadStatsPage = $('#thread_stats_page'); - threadStatsPage.text(page); - if (!found) { - threadStatsPage.css('color', 'red'); - } else { - threadStatsPage.css('color', ''); - } - }); + + fetchPageNumber(); } // Load the current page the thread is on. // Uses ajax call so it gets loaded on a delay (depending on network resources available). - setInterval(function() { - $.getJSON('//' + document.location.host + '/' + board_name + '/threads.json', function(data) { - let found; - 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(thread_id)) { - page = data[i].page +1; - found = true; - break; - } - } - if (found) { - break; - } - } - $('#thread_stats_page').text(page); - if (!found) $('#thread_stats_page').css('color','red'); - else $('#thread_stats_page').css('color',''); - }); - }, 30000); + setInterval(fetchPageNumber, 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); }); }