Affinity to You

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

当前为 2021-06-19 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Affinity to You
  3. // @namespace AffinityShow
  4. // @version 0.10
  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 GM_deleteValue
  11. // @grant GM_listValues
  12. // @grant GM_getValue
  13. // @grant GM_setValue
  14. // ==/UserScript==
  15.  
  16. (function() {
  17. 'use strict';
  18. var TimesExecuted = 0; //Create a new global variable
  19. var UserAffinityList = []; //Create a new global array
  20. var MALUserParsedList = new Map(); //Creates a new map to later add all non-dup mal usernames on the page
  21. var ScriptUsername = document.querySelector("a.header-profile-link").innerText; //Gets the script username
  22.  
  23. if (document.querySelector("#quickReply") !== null) //If the topic isn't locked
  24. { //Starts the if condition
  25. var AffinityList = document.createElement("button"); //Creates a btn element
  26. AffinityList.setAttribute("style", "display: none; background-color: snow; margin-left: 10px;"); //The CSS for the "button"
  27. AffinityList.setAttribute("id", "AffinityList"); //Adds an ID to the button
  28. AffinityList.setAttribute("class", "inputButton"); //Adds a class to the button
  29. document.querySelector("#quickReply").prepend(AffinityList); //Append the button above the text box element
  30.  
  31. var ShowAffinityList = document.createElement("input"); //Creates an a element
  32. ShowAffinityList.setAttribute("style", "background-color: #4165ba;"); //The CSS for the input btn
  33. ShowAffinityList.setAttribute("id", "ShowAffinityList"); //Adds an ID to the input btn
  34. ShowAffinityList.setAttribute("class", "inputButton"); //Adds a class to the input btn
  35. ShowAffinityList.setAttribute("value", " Show Affinity list"); //Adds the input btn default text
  36. ShowAffinityList.setAttribute("readonly", "readonly"); //Make the input btn default text not editable
  37. document.querySelector("#quickReply").prepend(ShowAffinityList); //Append the input btn above the text box element
  38.  
  39. document.querySelector("#ShowAffinityList").onclick = function() { //When the Show Affinity button is clicked
  40. var AffinityList = document.querySelector("#AffinityList"); //Store the Affinity List to a variable
  41. if (AffinityList.style.display === 'none') { //If the Affinity list is hidden
  42. AffinityList.style.display = ''; //Hide the Affinity List
  43. document.querySelector("#ShowAffinityList").value = " Hide Affinity list"; //Change the Show Affinity List text to Hide Affinity List
  44. } else { //If the Affinity list is being shown
  45. AffinityList.style.display = 'none'; //Show the Affinity List
  46. document.querySelector("#ShowAffinityList").value = " Show Affinity list"; //Change the Hide Affinity List text to Show Affinity List
  47. } //Finishes the else condition
  48. }; //Finishes the onclick event listener
  49. } //Finishes the if condition
  50.  
  51. if (GM_getValue('Date') === undefined) //If the date wasn't yet stored on tampermonkey
  52. { //Starts the if condition
  53. GM_setValue('Date', new Date().getMonth()); //Get and save the actual month as a number
  54. } //Finishes the if condition
  55.  
  56. if (new Date().getMonth() > GM_getValue('Date')) //If the month number stored on tampermonkey is a previous month
  57. { //Starts the if condition
  58. GM_listValues().forEach(function(a) {
  59. if (a !== GM_getValue('Date')) //If the current looped element isn't the past month number stored on tampermonkey
  60. { //Starts the if condition
  61. GM_deleteValue(a); //Delete the actual looped value of the TamperMonkey storage
  62. } //Finishes the if condition
  63.  
  64. }); //Finishes the foreach condition to erase all the users stored on tampermonkey
  65. } //Finishes the if condition
  66.  
  67. window.onscroll = function() { //Starts the onscroll event listener
  68. TimesExecuted += 1; //Sum the total amount of times that the page was scrolled
  69. if (TimesExecuted === 1) //If it's the first time that the page was scrolled
  70. { //Starts the if condition
  71. MALUserParsedList.set(ScriptUsername, {}); //Add the script username to the map,so that the script won't fetch the script user profile
  72. MALUserParsedList.set('removed-user', {}); //Add the 'removed-user' username to the map,so that the script won't fetch the non existent user profile
  73. document.querySelectorAll('td.forum_boardrow2 > div > div > a > strong').forEach(async function(UserName) { //Execute this function for each username on the topic page
  74.  
  75. if (!MALUserParsedList.has(UserName.innerText) && GM_getValue(UserName.innerText) !== undefined) { //If the username isn't already on the map and is stored on tampermonkey
  76. MALUserParsedList.set(UserName.innerText, {}); //Add the username on the map
  77.  
  78. if (GM_getValue(UserName.innerText).match(/-\d+(?:\.\d+)?(?=%)/) === null && GM_getValue(UserName.innerText).match('Unknown') === null) //If the - symbol doesn't exist and if the affinity isn't Unknown
  79. { //Starts the if condition
  80. UserAffinityList.push('<a href="' + UserName.parentElement.href + '" target="_blank" title="Click to open the ' + UserName.innerText + '\'s Profile">' + UserName.innerText + ' ' + GM_getValue(UserName.innerText).match(/\d+(?:\.\d+)?(?=%)/)[0] + '%</a>'); //Store all the topic User Names and links
  81. } //Finishes the if condition
  82.  
  83. [...document.querySelectorAll('td.forum_boardrow2 > div > div > a > strong')].filter(a => a.textContent.includes(UserName.innerText)).forEach(a => a.parentNode.parentNode.parentNode.insertAdjacentHTML("beforeend", '<br>' + GM_getValue(UserName.innerText))); //Add the affinity % to every topic reply that matches the fetched profile username
  84. } //Finishes the if condition
  85.  
  86.  
  87. if (!MALUserParsedList.has(UserName.innerText) && GM_getValue(UserName.innerText) === undefined) { //If the username isn't already on the map and isn't stored on tampermonkey
  88. MALUserParsedList.set(UserName.innerText, {}); //Add the username on the map
  89.  
  90. const html = await (await fetch('https://myanimelist.net/profile/' + UserName.innerText)).text(); //Gets the fetch response
  91. var newDocument = new DOMParser().parseFromString(html, 'text/html'); //Parses the fetch response
  92. var AffinityPercentage; //Make the variable global
  93. if (newDocument.querySelector("span[class*='bar-inner']") !== null) //If the element containing the affinity % exists
  94. { //Starts the if condition
  95. AffinityPercentage = newDocument.querySelector("span[class*='bar-inner']").innerText.replace('--', '-').trim(); //Gets the affinity %
  96. } //Finishes the if condition
  97. else //If the element containing the affinity % doesn't exist
  98. { //Starts the else condition
  99. AffinityPercentage = ' Unknown'; //Sets the affinity % to Unknown
  100. } //Finishes the else condition
  101.  
  102. if (AffinityPercentage.match('-') === null && AffinityPercentage.match('Unknown') === null) //If the - symbol doesn't exist and if the affinity isn't Unknown
  103. { //Starts the if condition
  104. 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
  105.  
  106. AffinityPercentage = '<a href="https://myanimelist.net/sharedanime.php?u1=' + UserName.innerText + '&u2=' + ScriptUsername + '" target="_blank" title="Click to open the Shared Anime page between you and ' + UserName.innerText + '"><strong style="color:blue; font-weight: normal;">Affinity to You ' + AffinityPercentage + '</strong></a>'; //Make the text blue
  107. } //Finishes the if condition
  108. else //If the - symbol DOESN'T exist
  109. { //Starts the else condition
  110. AffinityPercentage = '<a href="https://myanimelist.net/sharedanime.php?u1=' + UserName.innerText + '&u2=' + ScriptUsername + '" target="_blank" title="Click to open the Shared Anime page between you and ' + UserName.innerText + '"><strong style="color:red; font-weight: normal;">Affinity to You ' + AffinityPercentage + '</strong>'; //Make the text red
  111. } //Finishes the else condition
  112.  
  113. GM_setValue(UserName.innerText, AffinityPercentage); //Get and save the UserName and the AffinityPercentage
  114. [...document.querySelectorAll('td.forum_boardrow2 > div > div > a > strong')].filter(a => a.textContent.includes(UserName.innerText)).forEach(a => a.parentNode.parentNode.parentNode.insertAdjacentHTML("beforeend", '<br>' + AffinityPercentage)); //Add the affinity % to every topic reply that matches the fetched profile username
  115. } //Finishes the if condition
  116.  
  117. if (document.querySelector("#quickReply") !== null) //If the topic isn't locked
  118. { //Starts the if condition
  119. if (UserAffinityList.length != 0) //If there's any users that has a positive affinity with you in the page
  120. { //Starts the if condition
  121. document.querySelector("#AffinityList").innerHTML = UserAffinityList.sort(function(a, b) { //Add the sorted list to the button element
  122. var aA = parseFloat(a.match(/\d+(?:\.\d+)?(?=%)/)); //Get only the Affinity % numbers
  123. var bA = parseFloat(b.match(/\d+(?:\.\d+)?(?=%)/)); //Get only the Affinity % numbers
  124. return bA > aA ? 1 : -1; //Compare the Affinity % and sort the array
  125. }).join('<br><br>'); //Finishes the sorting condition and add "spaces" between the links
  126. } //Finishes the if condition
  127. else //If there's NO users that has a positive affinity with you in the page
  128. { //Starts the else condition
  129. document.querySelector("#AffinityList").innerHTML = '<strong style="color:red; font-weight: normal;">No one here has a positive affinity with you</strong>'; //Add a text
  130. } //Finishes the else condition
  131. } //Finishes the if condition
  132.  
  133. }); //Finishes the async function
  134. } //Finishes the if condition
  135. }; //Finishes the onscroll event listener
  136. })();