thread-stats.js: format

This commit is contained in:
Zankaria 2024-08-20 10:53:36 +02:00
parent 9c3755c7a7
commit 7ffb81d1f8

View file

@ -7,16 +7,17 @@
* $config['additional_javascript'][] = 'js/jquery.min.js'; * $config['additional_javascript'][] = 'js/jquery.min.js';
* $config['additional_javascript'][] = 'js/thread-stats.js'; * $config['additional_javascript'][] = 'js/thread-stats.js';
*/ */
if (active_page == 'thread') { if (active_page == 'thread') {
$(document).ready(function() { $(document).ready(function() {
//check if page uses unique ID // Check if page uses unique ID.
var IDsupport = ($('.poster_id').length > 0); let IDsupport = ($('.poster_id').length > 0);
var thread_id = (document.location.pathname + document.location.search).split('/'); let thread_id = (document.location.pathname + document.location.search).split('/');
thread_id = thread_id[thread_id.length -1].split('+')[0].split('.')[0]; thread_id = thread_id[thread_id.length -1].split('+')[0].split('.')[0];
$('.clear').before('<div id="thread_stats"></div>'); $('.clear').before('<div id="thread_stats"></div>');
var el = $('#thread_stats'); let el = $('#thread_stats');
el.prepend('Page <span id="thread_stats_page">?</span>'); el.prepend('Page <span id="thread_stats_page">?</span>');
if (IDsupport) { if (IDsupport) {
el.prepend('<span id="thread_stats_uids">0</span> UIDs |&nbsp;'); el.prepend('<span id="thread_stats_uids">0</span> UIDs |&nbsp;');
@ -24,22 +25,23 @@ $(document).ready(function(){
el.prepend('<span id="thread_stats_images">0</span> images |&nbsp;'); el.prepend('<span id="thread_stats_images">0</span> images |&nbsp;');
el.prepend('|&nbsp<span id="thread_stats_posts">0</span> replies |&nbsp;'); el.prepend('|&nbsp<span id="thread_stats_posts">0</span> replies |&nbsp;');
delete el; delete el;
function update_thread_stats() { function update_thread_stats() {
var op = $('#thread_'+ thread_id).find('div.post.op:not(.post-hover):not(.inline)').first(); let op = $('#thread_' + thread_id).find('div.post.op:not(.post-hover):not(.inline)').first();
var replies = $('#thread_'+ thread_id).find('div.post.reply:not(.post-hover):not(.inline)'); let replies = $('#thread_' + thread_id).find('div.post.reply:not(.post-hover):not(.inline)');
// post count // Post count.
$('#thread_stats_posts').text(replies.length); $('#thread_stats_posts').text(replies.length);
// image count // Image count.
$('#thread_stats_images').text(replies.filter(function() { $('#thread_stats_images').text(replies.filter(function() {
return $(this).find('> .files').text().trim() != false; return $(this).find('> .files').text().trim() != false;
}).length); }).length);
// unique ID count // Unique ID count.
if (IDsupport) { if (IDsupport) {
var opID = op.find('> .intro > .poster_id').text(); let opID = op.find('> .intro > .poster_id').text();
var ids = {}; let ids = {};
replies.each(function() { replies.each(function() {
var cur = $(this).find('> .intro > .poster_id'); let cur = $(this).find('> .intro > .poster_id');
var curID = cur.text(); let curID = cur.text();
if (ids[curID] === undefined) { if (ids[curID] === undefined) {
ids[curID] = 0; ids[curID] = 0;
} }
@ -49,7 +51,7 @@ $(document).ready(function(){
ids[opID] = 0; ids[opID] = 0;
} }
ids[opID]++; ids[opID]++;
var cur = op.find('>.intro >.poster_id'); let cur = op.find('>.intro >.poster_id');
cur.find('+.posts_by_id').remove(); cur.find('+.posts_by_id').remove();
cur.after('<span class="posts_by_id"> (' + ids[cur.text()] + ')</span>'); cur.after('<span class="posts_by_id"> (' + ids[cur.text()] + ')</span>');
replies.each(function() { replies.each(function() {
@ -57,54 +59,69 @@ $(document).ready(function(){
cur.find('+.posts_by_id').remove(); cur.find('+.posts_by_id').remove();
cur.after('<span class="posts_by_id"> (' + ids[cur.text()] + ')</span>'); cur.after('<span class="posts_by_id"> (' + ids[cur.text()] + ')</span>');
}); });
var size = function(obj) { let size = function(obj) {
var size = 0, key; let size = 0
for (key in obj) { for (let key in obj) {
if (obj.hasOwnProperty(key)) size++; if (obj.hasOwnProperty(key)) {
size++;
}
} }
return size; return size;
}; };
$('#thread_stats_uids').text(size(ids)); $('#thread_stats_uids').text(size(ids));
} }
$.getJSON('//' + document.location.host + '/' + board_name + '/threads.json', function(data) { $.getJSON('//' + document.location.host + '/' + board_name + '/threads.json', function(data) {
var found, page = '???'; let found;
for (var i=0;data[i];i++){ let page = '???';
var threads = data[i].threads; let threadId = parseInt(thread_id);
for (var j=0; threads[j]; j++){ for (let i = 0; data[i]; i++) {
if (parseInt(threads[j].no) == parseInt(thread_id)) { let threads = data[i].threads;
for (let j = 0; threads[j]; j++) {
if (parseInt(threads[j].no) === threadId) {
page = data[i].page +1; page = data[i].page +1;
found = true; found = true;
break; break;
} }
} }
if (found) break; if (found) {
break;
}
}
let threadStatsPage = $('#thread_stats_page');
threadStatsPage.text(page);
if (!found) {
threadStatsPage.css('color', 'red');
} else {
threadStatsPage.css('color', '');
} }
$('#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) // Load the current page the thread is on.
var thread_stats_page_timer = setInterval(function(){ // 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) { $.getJSON('//' + document.location.host + '/' + board_name + '/threads.json', function(data) {
var found, page = '???'; let found;
for (var i=0;data[i];i++){ let page = '???';
var threads = data[i].threads; for (let i = 0; data[i]; i++) {
for (var j=0; threads[j]; j++){ let threads = data[i].threads;
for (let j = 0; threads[j]; j++) {
if (parseInt(threads[j].no) == parseInt(thread_id)) { if (parseInt(threads[j].no) == parseInt(thread_id)) {
page = data[i].page +1; page = data[i].page +1;
found = true; found = true;
break; break;
} }
} }
if (found) break; if (found) {
break;
}
} }
$('#thread_stats_page').text(page); $('#thread_stats_page').text(page);
if (!found) $('#thread_stats_page').css('color','red'); if (!found) $('#thread_stats_page').css('color','red');
else $('#thread_stats_page').css('color',''); else $('#thread_stats_page').css('color','');
}); });
}, 30000); }, 30000);
$('body').append('<style>.posts_by_id{display:none;}.poster_id:hover+.posts_by_id{display:initial}</style>'); $('body').append('<style>.posts_by_id{display:none;}.poster_id:hover+.posts_by_id{display:initial}</style>');
update_thread_stats(); update_thread_stats();
$('#update_thread').click(update_thread_stats); $('#update_thread').click(update_thread_stats);