MyAnimeList(MAL) - Com-to-Com Links

Add Com-to-Com link between user and comment user for every comment.

  1. // ==UserScript==
  2. // @name MyAnimeList(MAL) - Com-to-Com Links
  3. // @version 1.2.1
  4. // @description Add Com-to-Com link between user and comment user for every comment.
  5. // @author Cpt_mathix & N_Ox
  6. // @match *://myanimelist.net/profile*
  7. // @match *://myanimelist.net/comments*
  8. // @exclude *://myanimelist.net/profile/*/*
  9. // @grant none
  10. // @namespace https://greasyfork.org/users/16080
  11. // ==/UserScript==
  12.  
  13. /* jshint esversion: 8 */
  14. /* jshint scripturl:true */
  15.  
  16. if (document.location.href.indexOf('profile') > -1) {
  17. var element = document.getElementById('lastcomment').getElementsByTagName('a');
  18. for (var i = 0; i < element.length; i++) {
  19. if (element[i] && element[i].innerHTML.indexOf("All Comments") > -1) {
  20. comtocom(element[i].href);
  21. break;
  22. }
  23. }
  24. } else {
  25. comtocom(document.location.href);
  26. }
  27.  
  28. function parseProfileHTML(html) {
  29. const parser = new DOMParser();
  30. const htmlDocument = parser.parseFromString(html, "text/html");
  31. const section = htmlDocument.documentElement.querySelector("#message").outerHTML;
  32.  
  33. const regex = new RegExp(/uid:(\d+)/g);
  34. return regex.exec(section)[1];
  35. }
  36.  
  37. async function getProfileIdAsync(profile) {
  38. return fetch(profile)
  39. .then(response => response.text())
  40. .then(text => parseProfileHTML(text));
  41. }
  42.  
  43. function getProfileIdSync(profile) {
  44. var request = new XMLHttpRequest();
  45. request.open('GET', profile, false);
  46. request.send(null);
  47.  
  48. if (request.status === 200) {
  49. return parseProfileHTML(request.responseText);
  50. }
  51. }
  52.  
  53. async function comtocom(url) {
  54. url = url.replace(/&*show=\d*/g, "");
  55. var i = url.indexOf('id=');
  56. if (i == -1) return;
  57. url = '/comtocom.php?id1=' + url.substr(i + 3) + '&id2=';
  58.  
  59. if (document.location.href.indexOf('profile') > -1) {
  60. document.querySelectorAll('div[id^=comBox]').forEach(function (el) {
  61. if (el.getElementsByClassName('postActions ar mt4').length !== 0) {
  62. return;
  63. }
  64.  
  65. var profile = el.querySelector('.image').href;
  66. if (!profile) return;
  67.  
  68. var div = document.createElement('div');
  69. div.className = 'postActions ar mt4 mr12';
  70.  
  71. var link = document.createElement('a');
  72. link.innerHTML = "Conversation";
  73. link.href="javascript:void(0);";
  74.  
  75. link.onclick = async (event) => {
  76. if (!link.href.includes('comtocom.php')) {
  77. link.style.cursor = "progress";
  78. link.href = url + await getProfileIdAsync(profile);
  79. link.style.cursor = "pointer";
  80. window.location.href = link.href;
  81. }
  82. };
  83. link.onauxclick = (event) => {
  84. if (!link.href.includes('comtocom.php')) {
  85. link.href = url + getProfileIdSync(profile);
  86. }
  87. };
  88. link.oncontextmenu = (event) => {
  89. if (!link.href.includes('comtocom.php')) {
  90. link.href = url + getProfileIdSync(profile);
  91. }
  92. };
  93.  
  94. div.appendChild(link);
  95. el.appendChild(div);
  96. });
  97. } else {
  98. // console.log(document.querySelectorAll('div[id^=comBox] > table > tbody > tr'));
  99. // console.log(document.querySelectorAll('div[id^=comBox]'));
  100. document.querySelectorAll('div[id^=comBox] > table > tbody > tr').forEach(function (el) {
  101. var com = el.querySelector('div[id^=com]:not([id^=comtext])');
  102. if (!com) return;
  103. if (com.children.length == 3) return;
  104.  
  105. var profile = el.querySelector('.picSurround > a').href;
  106. if (!profile) return;
  107.  
  108. com.insertAdjacentHTML("beforeend", '<div style="margin-top:10px" align="right"><a href="javascript:void(0);" class="conversation">Conversation</a></div>');
  109.  
  110. const link = com.querySelector(".conversation");
  111.  
  112. link.onclick = async (event) => {
  113. if (!link.href.includes('comtocom.php')) {
  114. link.style.cursor = "progress";
  115. link.href = url + await getProfileIdAsync(profile);
  116. link.style.cursor = "pointer";
  117. window.location.href = link.href;
  118. }
  119. };
  120. link.onauxclick = (event) => {
  121. if (!link.href.includes('comtocom.php')) {
  122. link.href = url + getProfileIdSync(profile);
  123. }
  124. };
  125. link.oncontextmenu = (event) => {
  126. if (!link.href.includes('comtocom.php')) {
  127. link.href = url + getProfileIdSync(profile);
  128. }
  129. };
  130. });
  131. }
  132. }