Find user profiles - Moodle

04/05/2022, 11:05:08

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        Find user profiles - Moodle
// @namespace   Violentmonkey Scripts
// @match       *://*moodle*/message/*
// @match       *://ena*/message/*
// @grant       none
// @version     1.0
// @author      -
// @description 04/05/2022, 11:05:08
// @license MIT
// ==/UserScript==


// IF YOUR MOODLE'S DOMAIN NAME DOES NOT MATCH THE WILDCARD UP THERE, CHANGE THE @match ATTRIBUTE ACCORDINGLY
// AND ADD THE `/messages/*` PATH TO IT !! EXAMPLE : https://moodl.yourschool.com/message/*


var baseURL = window.location.origin;

// Refresh every 3 seconds, to see if the pane opened is the correct one
setInterval(function(){
  var button = document.getElementById("view-profile-button");
  if(button != null){
    button.remove();
  }
  refresh();
}, 3000);


function refresh(){
  var messageIndexContainer = document.querySelectorAll(`[data-region*="message-index"]`)[0];
  var viewContactContainer = messageIndexContainer.querySelectorAll(`[data-region="view-contact"]`)[0];
  var contentContainer = viewContactContainer.childNodes[1];
  if(contentContainer.childNodes[2] != undefined){
    var buttonsContainer = contentContainer.childNodes[2].childNodes[5] // Container where all user buttons are located
    var userid = buttonsContainer.childNodes[1].getAttribute("data-route-param-3");
    
    // Create button
    var viewProfileButton = document.createElement("button");
    viewProfileButton.id = "view-profile-button"
    viewProfileButton.className = "btn btn-primary btn-block "
    viewProfileButton.innerHTML = "Profile";
    viewProfileButton.userId = userid;
    viewProfileButton.addEventListener("click", function(evt){window.open(baseURL + "/user/profile.php?id=" + evt.currentTarget.userId,'_blank');})
    buttonsContainer.appendChild(viewProfileButton);
  }
}