MediaWiki:Common.js: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 62: Line 62:


                 // find the person's name in the node, and surround their name (in the innerhtml) with a span and change the color to purple. Make sure that the name only exists in the text, and not in the html (so we don't change the color of the link)
                 // find the person's name in the node, and surround their name (in the innerhtml) with a span and change the color to purple. Make sure that the name only exists in the text, and not in the html (so we don't change the color of the link)
                 var name = node.innerHTML.match(/^(.*?)\s/)[1];
                 var nodeText = node.textContent;
                var nameIndex = node.innerHTML.indexOf(name);
                var nameLength = name.length;
                var html = node.innerHTML;
                var text = node.textContent;
                var textIndex = text.indexOf(name);
                var textLength = name.length;
                var startTag = '<span style="color:purple">';
                var endTag = '</span>';
                var htmlStart = html.substring(0, nameIndex);
                var htmlEnd = html.substring(nameIndex + nameLength);
                var textStart = text.substring(0, textIndex);
                var textEnd = text.substring(textIndex + textLength);
                var newHtml = htmlStart + startTag + html.substring(nameIndex, nameIndex + nameLength) + endTag + htmlEnd;


                 // replace the node's innerHTML with the new html
                 // Iterate through each user's name
                node.innerHTML = newHtml;
                for (var j = 0; j < users.length; j++) {
                 // this entire section was made by copilot cuz im too lazy to do it myself
                    var userName = users[j];
           
                    // Create a regular expression to match the name in the text
                    var regex = new RegExp('\\b' + userName + '\\b', 'g');
           
                    // Check if the user's name is present in the text
                    if (nodeText.match(regex)) {
                        // Create a new HTML string with the name wrapped in a <span> element
                        var highlightedText = nodeText.replace(regex, '<span style="color: purple;">$&</span>');
           
                        // Update the node's HTML with the highlighted text
                        node.innerHTML = highlightedText;
                    }
                }
                 // this entire section was made by copilot because im too lazy to do it myself
             }
             }