forked from GithubBackups/vichan
thread-stats.js: reduce duplicated code
This commit is contained in:
parent
1a7ebc88ba
commit
bff9d0ab19
@ -14,6 +14,7 @@ if (active_page == 'thread') {
|
|||||||
let idSupport = ($('.poster_id').length > 0);
|
let idSupport = ($('.poster_id').length > 0);
|
||||||
let threadId = (document.location.pathname + document.location.search).split('/');
|
let threadId = (document.location.pathname + document.location.search).split('/');
|
||||||
threadId = threadId[threadId.length -1].split('+')[0].split('-')[0].split('.')[0];
|
threadId = threadId[threadId.length -1].split('+')[0].split('-')[0].split('.')[0];
|
||||||
|
let boardName = $('input[name="board"]').val();
|
||||||
|
|
||||||
$('.boardlist.bottom, footer')
|
$('.boardlist.bottom, footer')
|
||||||
.first()
|
.first()
|
||||||
@ -28,9 +29,37 @@ if (active_page == 'thread') {
|
|||||||
el.prepend('<span id="thread_stats_posts">0</span> ' +_('replies')+ ' | ');
|
el.prepend('<span id="thread_stats_posts">0</span> ' +_('replies')+ ' | ');
|
||||||
delete el;
|
delete el;
|
||||||
|
|
||||||
|
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() {
|
function updateThreadStats() {
|
||||||
let op = $('#thread_' + threadId + ' > div.post.op:not(.post-hover):not(.inline)').first();
|
let op = $('#thread_' + threadId).find('div.post.op:not(.post-hover):not(.inline)').first();
|
||||||
let replies = $('#thread_' + threadId + ' > div.post.reply:not(.post-hover):not(.inline)');
|
let replies = $('#thread_' + threadId).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.
|
||||||
@ -76,60 +105,12 @@ if (active_page == 'thread') {
|
|||||||
$('#thread_stats_uids').text(size(ids));
|
$('#thread_stats_uids').text(size(ids));
|
||||||
}
|
}
|
||||||
|
|
||||||
let boardName = $('input[name="board"]').val();
|
fetchPageNumber();
|
||||||
$.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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$('#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.
|
// Load the current page the thread is on.
|
||||||
// Uses ajax call so it gets loaded on a delay (depending on network resources available).
|
// Uses ajax call so it gets loaded on a delay (depending on network resources available).
|
||||||
let thread_stats_page_timer = setInterval(function() {
|
setInterval(fetchPageNumber, 30000);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$('#thread_stats_page').text(page);
|
|
||||||
if (!found) {
|
|
||||||
$('#thread_stats_page').css('color', 'red');
|
|
||||||
} else {
|
|
||||||
$('#thread_stats_page').css('color', '');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}, 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>');
|
||||||
updateThreadStats();
|
updateThreadStats();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user