To Our Amazing Educators Everywhere,
Happy Teacher Appreciation Week!
Found this content helpful? Log in or sign up to leave a like!
I would like for Canvas to randomly assign students a number when hiding student names option is selected. After several trials, it seems that Canvas is programmed to assign these numbers alphabetically. For the primary teacher, this poses a non-random feel as these students are regularly numbered in the same alphabetic fashion. Randomizing the order of grading doesn't help because again the number assigned is still alphabetical and the student number is known to both teacher and student.
If you have something like Ublock Origin, you can hide the student's names (or their generated numbers) by right clicking the name and selecting "block element".
It can also be done with this tampermonkey script, if you prefer that (note to change the "match" to apply to your Canvas instance's URL, so it doesn't go over every single website:
// ==UserScript==
// Hide Span Text Inside .studentSelection
// @namespace http://tampermonkey.net/
// @version 1.1
// @description Hides text of spans with class 'ui-selectmenu-item-header' inside .studentSelection divs only
// Gabriel33 and ChatGPT 4o
// @match https://www.example.com
// none
// ==/UserScript==
(function() {
'use strict';
function hideSpanText() {
const containers = document.querySelectorAll('div.studentSelection');
containers.forEach(container => {
const spans = container.querySelectorAll('span.ui-selectmenu-item-header');
spans.forEach(span => {
span.textContent = '';
});
});
}
// Run initially
hideSpanText();
// Observe DOM changes to handle dynamically added content
const observer = new MutationObserver(hideSpanText);
observer.observe(document.body, { childList: true, subtree: true });
})();
--EDIT-- I changed the @match to example.com to avoid use of the script without changes
@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.
Hi @James, thanks for your comments (and scripts).
I did point out to change the match in the text. I don't usually like the specific match that are used in other scripts because my university uses canvas.[university].edu instead of what seems to be more common [university].instructure.com, so I am used to changing it anyway (and @match canvas.*.edu doesn't work, need to use @include and some regex to go around that)
I agree with the efficiency comments, those could be improved, I only tested whether it worked.
I also agree that having random numbers would be better than hiding, and considered a formula to replace each student original number with a random number, but that would over complicate it.
--EDIT-- to avoid people using that script without paying attention to the reply, I'll remove the wildcard match
Somehow my mind completely skipped the changing the match directive. It went from tampermonkey immediately to spoiler. Sorry about that.
The reason most put [instance].*.instructure.com is because that's what most clients use -- and probably because that's what I use. But I would rather have a few people to change it to match their university than putting out code that runs everywhere because someone missed the need to change it.
The include directive is more flexible, but Chrome is pushing for not using it. I haven't used match a lot, but I think you'll need multiple match lines if you want it to run on production, beta, and test.
To participate in the Instructure Community, you need to sign up or log in:
Sign In