RYM: All releases chart from album page

Add links to all releases chart by featured and credited artists on album page

  1. // ==UserScript==
  2. // @name RYM: All releases chart from album page
  3. // @namespace https://github.com/sercep/userscripts
  4. // @version 1.0.0
  5. // @description Add links to all releases chart by featured and credited artists on album page
  6. // @author sercep
  7. // @match https://rateyourmusic.com/release/*
  8. // @license MIT
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. function collectArtistsFrom(selector) {
  15. const anchors = document.querySelectorAll(selector);
  16. const result = [];
  17. anchors.forEach(a => {
  18. const href = a.getAttribute('href');
  19. if (!href) return;
  20. const parts = href.split('/');
  21. if (parts.length >= 3 && parts[1] === 'artist') {
  22. const artistId = parts[2];
  23. if (!result.includes(artistId)) {
  24. result.push(artistId);
  25. }
  26. }
  27. });
  28. return result;
  29. }
  30.  
  31. function getArtistsFromUrl() {
  32. const artists = collectArtistsFrom('.tracklist_title .rendered_text a.artist');
  33. return artists;
  34. }
  35.  
  36. function buildChartUrl(artistArray) {
  37. const artistParam = artistArray.join(',');
  38. const link = `https://rateyourmusic.com/charts/popular/album,ep,comp,single,video,unauth,mixtape,musicvideo,djmix,additional/all-time/a:${artistParam}/incl:live,archival,soundtrack/`;
  39. console.log(link);
  40. return link;
  41. }
  42.  
  43. function createChartSection(artists) {
  44. if (artists.length === 0) return;
  45.  
  46. const outerDiv = document.createElement('div');
  47. outerDiv.className = 'section_lists';
  48.  
  49. const newSection = document.createElement('div');
  50. newSection.className = 'section_charts';
  51.  
  52. const headerDiv1 = document.createElement('div');
  53. headerDiv1.className = 'release_page_header';
  54. const h2 = document.createElement('h2');
  55. h2.style.float = 'left';
  56. h2.textContent = 'Charts';
  57. headerDiv1.appendChild(h2);
  58. newSection.appendChild(headerDiv1);
  59.  
  60. const chartUrl = buildChartUrl(artists);
  61. const headerDiv2 = document.createElement('div');
  62. headerDiv2.className = 'release_page_header';
  63. const ul = document.createElement('ul');
  64. ul.className = 'credits';
  65. const li = document.createElement('li');
  66. const link = document.createElement('a');
  67. link.href = chartUrl;
  68. link.textContent = 'All releases by artists';
  69. li.appendChild(link);
  70. ul.appendChild(li);
  71. headerDiv2.appendChild(ul);
  72. newSection.appendChild(headerDiv2);
  73.  
  74. const table = document.createElement('table');
  75. table.className = 'color_bar';
  76. table.style.cssText = 'width:100%;height:1px;font-size:1px;padding:0;';
  77. const tbody = document.createElement('tbody');
  78. const tr = document.createElement('tr');
  79. tr.style.cssText = 'height:1px;padding:0;';
  80. tbody.appendChild(tr);
  81. table.appendChild(tbody);
  82. newSection.appendChild(table);
  83.  
  84. outerDiv.appendChild(newSection);
  85.  
  86. const line = document.createElement('div');
  87. line.style.cssText = 'width:100%;height:1px;background-color:#ffffff;margin:10px 0;';
  88. outerDiv.appendChild(line);
  89.  
  90. const listSection = document.querySelector('.section_lists');
  91. if (listSection && listSection.parentNode) {
  92. listSection.parentNode.insertBefore(outerDiv, listSection);
  93. } else {
  94. document.body.appendChild(outerDiv);
  95. }
  96. }
  97.  
  98. const urlArtists = getArtistsFromUrl();
  99. const featuredArtists = collectArtistsFrom('ul.credits li.featured_credit a.artist');
  100. const mainCredits = collectArtistsFrom('ul.credits > li a.artist');
  101. const minorCredits = collectArtistsFrom('#minor_credits_ a.artist');
  102. const tracklistArtists = collectArtistsFrom('.tracklist_title .rendered_text a.artist');
  103.  
  104. const allCreditedArtists = [...mainCredits];
  105. for (const a of minorCredits) {
  106. if (!allCreditedArtists.includes(a)) {
  107. allCreditedArtists.push(a);
  108. }
  109. }
  110.  
  111. if (urlArtists.length === 0 && featuredArtists.length === 0 && allCreditedArtists.length === 0) return;
  112.  
  113. const outerDiv = document.createElement('div');
  114. outerDiv.className = 'section_lists';
  115.  
  116. const newSection = document.createElement('div');
  117. newSection.className = 'section_charts';
  118.  
  119. const headerDiv1 = document.createElement('div');
  120. headerDiv1.className = 'release_page_header';
  121. const h2 = document.createElement('h2');
  122. h2.style.float = 'left';
  123. h2.textContent = 'Charts';
  124. headerDiv1.appendChild(h2);
  125. newSection.appendChild(headerDiv1);
  126.  
  127. if (urlArtists.length > 0) {
  128. const urlChartUrl = buildChartUrl(urlArtists);
  129. const headerDiv2 = document.createElement('div');
  130. headerDiv2.className = 'release_page_header';
  131. const ul = document.createElement('ul');
  132. ul.className = 'credits';
  133. const li = document.createElement('li');
  134. const link = document.createElement('a');
  135. link.href = urlChartUrl;
  136. link.textContent = 'All releases by artists of album';
  137. li.appendChild(link);
  138. ul.appendChild(li);
  139. headerDiv2.appendChild(ul);
  140. newSection.appendChild(headerDiv2);
  141. }
  142.  
  143. if (featuredArtists.length > 0) {
  144. const featuredUrl = buildChartUrl(featuredArtists);
  145. const headerDiv2 = document.createElement('div');
  146. headerDiv2.className = 'release_page_header';
  147. const ul = document.createElement('ul');
  148. ul.className = 'credits';
  149. const li = document.createElement('li');
  150. const link1 = document.createElement('a');
  151. link1.href = featuredUrl;
  152. link1.textContent = 'All releases by featured artists';
  153. li.appendChild(link1);
  154. ul.appendChild(li);
  155. headerDiv2.appendChild(ul);
  156. newSection.appendChild(headerDiv2);
  157. }
  158.  
  159. if (allCreditedArtists.length > 0) {
  160. const creditedUrl = buildChartUrl(allCreditedArtists);
  161. const headerDiv3 = document.createElement('div');
  162. headerDiv3.className = 'release_page_header';
  163. const ul = document.createElement('ul');
  164. ul.className = 'credits';
  165. const li = document.createElement('li');
  166. const link2 = document.createElement('a');
  167. link2.href = creditedUrl;
  168. link2.textContent = 'All releases by credited artists';
  169. li.appendChild(link2);
  170. ul.appendChild(li);
  171. headerDiv3.appendChild(ul);
  172. newSection.appendChild(headerDiv3);
  173. }
  174.  
  175. const table = document.createElement('table');
  176. table.className = 'color_bar';
  177. table.style.cssText = 'width:100%;height:1px;font-size:1px;padding:0;';
  178. const tbody = document.createElement('tbody');
  179. const tr = document.createElement('tr');
  180. tr.style.cssText = 'height:1px;padding:0;';
  181. tbody.appendChild(tr);
  182. table.appendChild(tbody);
  183. newSection.appendChild(table);
  184.  
  185. outerDiv.appendChild(newSection);
  186.  
  187. const line = document.createElement('div');
  188. line.style.cssText = 'width:100%;height:1px;background-color:#ffffff;margin:10px 0;';
  189. outerDiv.appendChild(line);
  190.  
  191. const listSection = document.querySelector('.section_lists');
  192. if (listSection && listSection.parentNode) {
  193. listSection.parentNode.insertBefore(outerDiv, listSection);
  194. } else {
  195. document.body.appendChild(outerDiv);
  196. }
  197. })();