Mentioner - MAL

Adds a button to copy the @user name on all users who have commented on any topics on MAL. The script also adds a search user name box near the box were the reply is written, so that you can search for any user that commented on that topic and click on the displayed @UserName to auto paste the @UserName into the reply box.

目前為 2022-08-15 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Mentioner - MAL
// @namespace    MALMentioner
// @version      8
// @description  Adds a button to copy the @user name on all users who have commented on any topics on MAL. The script also adds a search user name box near the box were the reply is written, so that you can search for any user that commented on that topic and click on the displayed @UserName to auto paste the @UserName into the reply box.
// @author       hacker09
// @match        https://myanimelist.net/forum/?topicid=*
// @match        https://myanimelist.net/clubs.php?cid=*
// @icon         https://t3.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=http://myanimelist.net&size=64
// @run-at       document-end
// @grant        none
// ==/UserScript==

(function() {
  'use strict';
  var MALUserParsedList = []; //Creates a array to later add all non-dup mal usernames on the page
  const ScriptUsername = document.querySelector("a.header-profile-link").innerText; //Gets the script username

  document.querySelectorAll('td.forum_boardrow2 > div > div > a > strong,div[style*="margin-bottom: 6px;"] > a').forEach(function(UserName) { //Execute this function for each username on the topic page
    if (!MALUserParsedList.includes(UserName.innerText) && UserName.innerText !== ScriptUsername && UserName.innerText !== 'removed-user') { //If the username isn't already on the array and if isn't the Script Username or the removed-user
      MALUserParsedList.push(UserName.innerText); //Add the username on the array

      window.jQuery('td.forum_boardrow2 > div > div > a > strong:contains("' + UserName.innerText + '"),div[style*="margin-bottom: 6px;"]:contains("' + UserName.innerText + '")').parent().after('<a title="Copy @' + UserName.innerText + '" onclick=navigator.clipboard.writeText("@' + UserName.innerText + '") style="cursor: pointer; margin-left: 9px; height: 10px; width: 10px; background-size: cover; display: inline-block; transform: scale(1.5); vertical-align: top; margin-top: 2px; background-image: url(https://i.imgur.com/vU0m0ye.png);"></a>'); //Add the copy @UserName button to every username that replied on the topic

    } //Finishes the if condition

    MALUserParsedList = MALUserParsedList.filter(v => v !== ScriptUsername); //Remove the script user username of the array if the script user also commented on the topic
    MALUserParsedList = MALUserParsedList.filter(v => v !== 'removed-user'); //Remove the 'removed-user' username of the array (if existent on the topic page)

  }); //Finishes the async function

  if (document.querySelector("#messageText") !== null) //If the user isn't on a club first page
  { //Starts the if condition
    document.querySelector("#quickReply,form.form-club-user-comment").insertAdjacentHTML("beforeend", `<textarea id="autocomplete-input" placeholder="Find User" style="resize: none; margin-left: 250px; margin-top: -30px;" cols="10" rows="1"></textarea> <div id="autocomplete-list" style="margin-left: 250px; margin-top: -5px; cursor: pointer; width: 150px; height: 145px; overflow-y: scroll; display: none;"></div>`); //Add a new div and text area to the page display: none; grid-template-columns: 1fr 1fr 1fr 1fr 1fr; grid-gap: 5px

    var Executed = 0; //Create a new variable with the value 0
    const MALTxtBox = document.querySelector("#messageText"); //Add the Mal txt box to a variable
    const UserNameList = document.querySelector("#autocomplete-list"); //Add the script div to a variable

    new MutationObserver(function() { //If the user changes the MAL txt box size
      document.querySelector("#autocomplete-input").style.marginLeft = parseInt(MALTxtBox.style.width) + 10 + 'px'; //Keep the script txt box on the same position
      document.querySelector("#autocomplete-input").style.marginTop = '-' + String(parseInt(MALTxtBox.style.height) + 25 + 'px'); //Keep the script txt box on the same position

      document.querySelector("#autocomplete-list").style.marginLeft = parseInt(MALTxtBox.style.width) + 10 + 'px'; //Keep the script div on the same position
      document.querySelector("#autocomplete-list").style.marginTop = '-' + MALTxtBox.style.height; //Keep the script div on the same position
    }).observe(MALTxtBox, { //Defines the element and attributes to be observed
      attributes: true
    }); //Finishes the definitions that will be observed

    document.querySelector("#autocomplete-input").onclick = function() //When the script txt box is clicked
    { //Starts the onclick condition
      Executed += 1; //Sum the total amount of times that the script txt box was clicked
      if (Executed === 1) //If it's the first time that the script txt box is clicked
      { //Starts the if condition
        UserNameList.style.display = ''; //Display the list containing the User Names
        MALUserParsedList.forEach(UserName => UserNameList.innerHTML += `<div  title="Click to paste @${UserName} on the text box" onmouseout='this.style.color = "black"' onmouseover='this.style.color = "#6386d5"' onclick='document.querySelector("#messageText").value === "" ? document.querySelector("#messageText").value += "@${UserName}\\n\\n" : document.querySelector("#messageText").value += "@${UserName} "'>\n@${UserName}</div>`); //Add the @UserNames to the script div
      } //Finishes the if condition
    }; //Finishes the onclick condition

    document.querySelector("#autocomplete-input").oninput = function(e) { //When any letter is written on the script txt box
      const matcher = new RegExp(`^${this.value}`, 'gi'); //Get the letters written on the script txt box and create a regex with that user input
      const matches = MALUserParsedList.filter(MALUserParsedList => MALUserParsedList.match(matcher)); //Find the user input in the arrays
      UserNameList.innerHTML = ''; //Remove the previously displayed UserNames
      matches.forEach(UserName => UserNameList.innerHTML += `<div  title="Click to paste @${UserName} on the text box" onmouseout='this.style.color = "black"' onmouseover='this.style.color = "#6386d5"' onclick='document.querySelector("#messageText").value === "" ? document.querySelector("#messageText").value += "@${UserName}\\n\\n" : document.querySelector("#messageText").value += "@${UserName} "'>\n@${UserName}</div>`); //Display the users found
    }; //Finishes the oninput event listener
  } //Finishes the if condition
})();