@Gabriel33, I really love when I see people putting out code and I've seen a lot of good comments you've made recently. I was one of the early champions of UserScripts and MutationObservers in the Canvas Community.
I was a little surprised when I saw the ChatGPT in there. ChatGPT is not a replacement for knowing what you're doing and no one should trust it without verifying what it does. The same goes for any code released in a forum like this, even mine.
After looking at the code, no one should install this userscript as it is. There are some serious performance issues here for every web page you visit.
The Name parameter should be lowercase name. That's not one of the major issues I'm talking about.
Be narrow in your matching. Modify the conditions in the match line to only run on the appropriate Canvas pages. You run it on every page on every website. For just the Canvas stuff, not only does it run on the main SpeedGrader interface, but it also runs on the iframe that loads the content. It's going to run on every iframe in every web page that you have. Most of that won't have a div element with a classname of studentSelection, but if they do, you probably don't want to do the rest of the code on that page. It's way too broad.
Be narrow in your listening. You listen for changes in the document.body, which is usually overly broad and causes it to run every time anything on any page changes. That can really slow things down. If you're including subtree, you may want to filter the items matched. You may also want to pass the mutations into the function and scan those for the changes. But generally speaking, you want to be as narrow as you can. I have a suspicion that Canvas attaching observers to the body is part of the reason that some of their Inst-UI things are so slow.
For me, it runs at least 8 times before it ever finds the name to hide. Then it runs again when I open a rubric. It runs when I click on each item in a rubric. It runs again for e v e r y s i n g l e c h a r a c t e r I type in a comment box.
Combine the excessive impact on a page (because body is not where you should listen) and that it runs on every single web page you visit (because of the match) and this script should not be used as is. It even runs here in the forums and slows it down.
You find the URL for SpeedGrader, which will keep it from running on all pages. Then you focus on when it should run on that page. It may take more than one MutationObserver to do it properly. You may need to watch for the framework that needs to be watched to be present before attaching the MutationObserver. That might be one on the body that you disconnect once the other is present. Then you add one to the content that ever-present after that.
There is a probably a better solution than just setting textContent = '', so I did some testing. I generally try to use CSS to change the display or visibility, but this works. My concern was whether changing the textContent would trigger a second mutation. Unfortunately, it does. The code executes twice if I advance between students when setting textContent='', but only once if I set span.style.display='none';
A better solution, although more difficult to accomplish, would be to do what @BryanCarrier asked for and to number the student names sequentially after they were randomized.
This is a start to something, but it needs some tweaking before anyone should use it.