Trying to calculate the number of topics each poster has participated in

67 Views Asked by At

First of all I'm sorry about the title, I didn't know how to put my problem into words.

I'm trying to build a script that will go on a forumotion forum; it is supposed to calculate the number of topics each member has posted in a specific forum. The members of my forum would post in a specific topic with a <div class="macartefidelite"></div> in their post, and the script would calculate their number of topics in the "Archives des rps" forum (thanks to one of the search pages of my forum that displays all topics a member has participated in) and put the resulting number in the div.

I've managed to build a script that kind of works:

document.addEventListener("DOMContentLoaded", () => { /*-waiting for dom to be loaded-*/
    if(window.location.href.indexOf("-demander-l-archivage-d-un-rp-abandonne") > -1) { /*-run script only on specific set of pages-*/
  
        $('.macartefidelite').each(function(){ /*-for each .macartefidelite div on page-*/
            var nbretopics = 0;
            var idmembre = $(this).parents('.pab_sujpost').find('.pab_sujpseudo').find('a').attr('href').split('u')[1];/*-get closest member ID-*/
            
            var pagi = ['', '/15', '/30', '/45', '/60', '/75','/90','/105','/120','/135','/150','/165']; /*-handle pagination-*/
            var pagecherchee = '';
                    
            for (var i = 0; i < pagi.length; i++) { /*-for each page in pagination-*/
                pagecherchee = 'https://peekaboo-rpg.forumactif.com/sta/u'+idmembre+pagi[i]; /*-build the url of the search page-*/
                    
                $.get(pagecherchee, function(datas){ /*-get the search page-*/
                    $('.pab_untopic',datas).each(function(){ /*-for each topic in the search page-*/
                        var lieutopic = $(this).find('.pab_ltopdesctop').find('a').attr('href'); /*-get the forum where topic was posted-*/      
                        
                        if(lieutopic == '/f59-archives-des-rps'){ /*-if forum is the one we want aka "Archives des rps"-*/
                            nbretopics++; /*-add 1 to nbretopics-*/
                        }
                    });
                    /*-when all topics have been checked-*/
                    $('a[href="/u'+idmembre+'"]').closest('.pab_sujpost').find('.macartefidelite').html(nbretopics); /*-put number in corresponding .macartefidelite div-*/
                  
                });
            }
  
        });
    }
});

However when I put it in use, the numbers displayed in the topic are wrong for some members... For example, you can check the topic in which each post has a .macartefidelite div, but for example the 5th post should display 11 and displays 14! The 18th post displays 11 and should display 1... 20th post should display 5 instead of 17, etc, etc.

I can't seem to figure out what goes wrong since the calculation does work for some posts and not for others...

I thought the nbretopics variable might not get reset right between each member, so I did try to move the var nbretopics = 0; declaration ahead or below, which does change the results... but never the right ones. So my guess is I didn't write the script in the right way, but I'm lost as to what I should do :( I guess there's a problem with the $.get part and the nbretopics variable, but I don't see where.

I'm self-taught, but not very well, so I'd be glad to get some help on this one, guys. Thank you in advance!

0

There are 0 best solutions below