Affinity to You

Shows the "Affinity to You" that all users who have commented on any topic on MAL have with you!

目前为 2021-04-01 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Affinity to You
  3. // @namespace AffinityShow
  4. // @version 0.6
  5. // @description Shows the "Affinity to You" that all users who have commented on any topic on MAL have with you!
  6. // @author hacker09
  7. // @match https://myanimelist.net/forum/?topicid=*
  8. // @icon https://www.google.com/s2/favicons?domain=myanimelist.net
  9. // @run-at document-end
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15. var UserAffinityList = []; //Create a new global array
  16. var MALUserParsedList = new Map(); //Creates a new map to later add all non-dup mal usernames on the page
  17. var ScriptUsername = document.querySelector("a.header-profile-link").innerText; //Gets the script username
  18.  
  19. if (document.querySelector("#quickReply") !== null) //If the topic isn't locked
  20. { //Starts the if condition
  21. var AffinityList = document.createElement("button"); //Creates a btn element
  22. AffinityList.setAttribute("style", "display: none; background-color: snow; margin-left: 10px;"); //The CSS for the "button"
  23. AffinityList.setAttribute("id", "AffinityList"); //Adds an ID to the button
  24. AffinityList.setAttribute("class", "inputButton"); //Adds a class to the button
  25. document.querySelector("#quickReply").prepend(AffinityList); //Append the button above the text box element
  26.  
  27. var ShowAffinityList = document.createElement("input"); //Creates an a element
  28. ShowAffinityList.setAttribute("style", "background-color: #4165ba;"); //The CSS for the input btn
  29. ShowAffinityList.setAttribute("id", "ShowAffinityList"); //Adds an ID to the input btn
  30. ShowAffinityList.setAttribute("class", "inputButton"); //Adds a class to the input btn
  31. ShowAffinityList.setAttribute("value", " Show Affinity list"); //Adds the input btn default text
  32. ShowAffinityList.setAttribute("readonly", "readonly"); //Make the input btn default text not editable
  33. document.querySelector("#quickReply").prepend(ShowAffinityList); //Append the input btn above the text box element
  34. } //Finishes the if condition
  35.  
  36. MALUserParsedList.set(ScriptUsername, {}); //Add the script username to the map,so that the script won't fetch the script user profile
  37. MALUserParsedList.set('removed-user', {}); //Add the 'removed-user' username to the map,so that the script won't fetch the non existent user profile
  38. document.querySelectorAll('td.forum_boardrow2 > div > div > a > strong').forEach(async function(UserName) { //Execute this function for each username on the topic page
  39. if (!MALUserParsedList.has(UserName.innerText)) { //If the username isn't already on the map
  40. MALUserParsedList.set(UserName.innerText, {}); //Add the username on the map
  41.  
  42. const html = await (await fetch('https://myanimelist.net/profile/' + UserName.innerText)).text(); //Gets the fetch response
  43. var newDocument = new DOMParser().parseFromString(html, 'text/html'); //Parses the fetch response
  44. if (newDocument.querySelector("span[class*='bar-inner']") !== null) //If the element containing the affinity % exists
  45. { //Starts the if condition
  46. var AffinityPercentage = newDocument.querySelector("span[class*='bar-inner']").innerText.replace('--', '-').trim(); //Gets the affinity %
  47. } //Finishes the if condition
  48. else //If the element containing the affinity % doesn't exist
  49. { //Starts the else condition
  50. AffinityPercentage = ' Unkown'; //Sets the affinity % to Unkown
  51. } //Finishes the else condition
  52.  
  53. if (AffinityPercentage.match('-') === null && AffinityPercentage.match('Unkown') === null) //If the - symbol doesn't exist and if the affinity isn't Unkown
  54. { //Starts the if condition
  55. UserAffinityList.push('<a href="' + UserName.parentElement.href + '" target="_blank" title="Click to open the ' + UserName.innerText + '\'s Profile">' + UserName.innerText + ' ' + AffinityPercentage + '</a>'); //Store all the topic User Names and links
  56.  
  57. AffinityPercentage = '<strong style="color:blue; font-weight: normal;">Affinity to You ' + AffinityPercentage + '</strong>'; //Make the text blue
  58. } //Finishes the if condition
  59. else //If the - symbol DOESN'T exist
  60. { //Starts the else condition
  61. AffinityPercentage = '<strong style="color:red; font-weight: normal;">Affinity to You ' + AffinityPercentage + '</strong>'; //Make the text red
  62. } //Finishes the else condition
  63.  
  64. window.jQuery('td.forum_boardrow2 > div > div > a > strong:contains("' + UserName.innerText + '")').parent().parent().parent().after(AffinityPercentage); //Add the affinity % to every topic reply that matches the fetched profile username
  65. } //Finishes the if condition
  66.  
  67. if (document.querySelector("#quickReply") !== null) //If the topic isn't locked
  68. { //Starts the if condition
  69. document.querySelector("#AffinityList").innerHTML = UserAffinityList.sort(function(a, b) { //Add the sorted list to the button element
  70. var aA = parseFloat(a.match(/\d+(?:\.\d+)?(?=%)/)); //Get only the Affinity % numbers
  71. var bA = parseFloat(b.match(/\d+(?:\.\d+)?(?=%)/)); //Get only the Affinity % numbers
  72. return bA > aA ? 1 : -1; //Compare the Affinity % and sort the array
  73. }).join('<br><br>'); //Finishes the sorting condition and add "spaces" between the links
  74. } //Finishes the if condition
  75.  
  76. }); //Finishes the async function
  77.  
  78. if (document.querySelector("#quickReply") !== null) //If the topic isn't locked
  79. { //Starts the if condition
  80. document.querySelector("#ShowAffinityList").onclick = function() { //When the Show Affinity button is clicked
  81. var AffinityList = document.querySelector("#AffinityList"); //Store the Affinity List to a variable
  82. if (AffinityList.style.display === 'none') { //If the Affinity list is hidden
  83. AffinityList.style.display = ''; //Hide the Affinity List
  84. document.querySelector("#ShowAffinityList").value = " Hide Affinity list"; //Change the Show Affinity List text to Hide Affinity List
  85. } else { //If the Affinity list is being shown
  86. AffinityList.style.display = 'none'; //Show the Affinity List
  87. document.querySelector("#ShowAffinityList").value = " Show Affinity list"; //Change the Hide Affinity List text to Show Affinity List
  88. } //Finishes the if condition
  89. }; //Finishes the onclick event listener
  90. } //Finishes the if condition
  91.  
  92. })();