Tallying html elements on a page

Good Afternoon!

I'm running a discussion forum and I would like KM (if possible) to tally the number of times an individual posts in the forum. The html code is similar for a specific user's posts. Here is a screen shot of someone who posted twice:

My question is: is there a way that KM can identify the number of times an element of this code repeats? That would give me the total number of posts per each user.

I can’t see your image because it is a TIFF (best to post JPG or PNG images).
So, shooting in the dark, you could do a JavaScript query to find and count the number of HTML elements of a given class or tag.

For example, I used this in a Execute a JavaScript in Google Chrome action (KM Wiki) recently. It returns the number of elements found of class “section-name”.

var secNameElem = document.getElementsByClassName("section-name")
var numElem = secNameElem.length

If you need more help, I’d need to see a section of HTML where the posts are made.

Thank you for the reply, I appreciate it! Sorry about the image, I just posted the JPEG!

I don’t see any HTML code there that is user unique.
How do you identify the user?

Can you provide a URL of an actual page you want to use?

BTW, most forum software that I have used provides an admin panel that shows the number of posts for each user.

The user is only displayed as a link at the top of the page with their photo. Unfortunately, this is behind a password so I can’t provide access. I looked all over for the number of posts and it’s not there, which is strange.

That said the html code doesn’t have to be unique to the user. In fact, I would ideally just like KM to repeat through a macro that will identify the number of posts, then go to the next user…and so forth.

My thinking was, is there a way for it to count “discussion_entry communication_message” or perhaps “data-mark-read-url”? Since that shows up every time a new post is made, it could count those and mission accomplished!

Yes. I showed that to you in my above example:

var secNameElem = document.getElementsByClassName("section-name")
var numElem = secNameElem.length

Just replace “section-name” with “discussion_entry communication_message”.
Of course, you can rename the variables to fit the data.

Awesome!! I’ll give it a try - thank you for your help!

I was able to get it to work on everything within the page, except the posts! It looks like the reason probably is because the posts are set within an iframe. Any suggestions?

It is very hard to debug without the actual HTML code. Is there any way you can share that with me? Feel free to PM it if you like.

The iframe may be getting in the way, but I'm not sure because I don't have access to the full page. Based on the HTML you sent me, this works for me:

var postList = document.querySelectorAll("div.discussion_entry");

It returns:

If this does not work for you, then I need to see the HTML around the <iframe> tag.

BTW, to post HTML code, like any code, use the forum code markdown of triple back-quotes, like this:

 --- your HTML code here ---

@Scott_Matkovich, if my above post does not work for you, you might try this (just guessing):

var iframeElem = document.getElementById("ID_of_your_iframe");
var postList = iframeElem.querySelectorAll("div.discussion_entry");


var iframeElem = document.getElementById("ID_of_your_iframe");
var iframeContent = iframeElem.contentWindow.document.body;
var postList = iframeContent.querySelectorAll("div.discussion_entry");

Replace “ID_of_your_iframe” with the actual ID from the HTML code.

Unfortunately, it keeps give me a “0” in return. Here is the script for the iframe:

    <div id="iframe_holder" style="display: block;">
    <iframe id="speedgrader_iframe" src="/courses/3467/assignments/60728/submissions/32328?preview=true"  frameborder="0" allowfullscreen="true"></iframe></div>
    <!DOCTYPE html>
    <html class=" lato-font-loaded" lang="en"><head>
    <meta charset="utf-8">

(I think I posted it right!) :slight_smile:

Try this:

var iframeElem = document.getElementById("speedgrader_iframe");
var postList =iframeElem.contentWindow.document.body.querySelectorAll("div.discussion_entry");

A really easy way to test is to right-click on the web page, and select “inspect”.
Then just paste the above code into the JavaScript Console.

Hey JMichaelTX - I just have to say, that you are an awesome contributor! Also, every time I see your screen-name I think you are Texas Michael - which is a pretty awesome name.
Anyway, I am working on something similar to what Scott here is working on. Your iframe script is freaking awesome! I was able to get it to work by adding .length onto the end, and it worked like a charm.

So, what I am wondering, is there a way to write an if/then macro such that if there is no iframe that it will execute a 0 (zero) but if there is an iframe it will execute your script? Is there another script that is needed in order to recognize whether or not there is an iframe? Any advice or thoughts?
As always - Totally appreciate you and your help!