Spotify - Add label search link

add label search link

  1. // ==UserScript==
  2. // @name Spotify - Add label search link
  3. // @description add label search link
  4. // @author to
  5. // @namespace https://github.com/to
  6. // @version 0.1
  7. // @license MIT
  8. // @match https://open.spotify.com/*
  9. // @icon https://www.google.com/s2/favicons?domain=spotify.com
  10. // ==/UserScript==
  11.  
  12. // ページ遷移を監視する
  13. function watchLocation(match, callback) {
  14. let page;
  15. setInterval(() => {
  16. // ページ遷移が発生したか?
  17. if (page != location.href) {
  18. page = location.href;
  19.  
  20. // 対象のページか?
  21. if (match.test(page))
  22. callback(page);
  23. }
  24. }, 500);
  25. }
  26.  
  27. watchLocation(/\/album\//, () => {
  28. // DOMへの反映を待つ
  29. let timer = setInterval(() => {
  30. // 権利表記を繰り返す
  31. let pre;
  32. [...document.querySelectorAll('div[aria-colcount] + div p')].map(elmRight => {
  33. // 表記をクリアする
  34. let right = elmRight.textContent.replaceAll(/(\℗ |© |(\(P\))?\d{4} )/g, '');
  35. elmRight.innerHTML = '';
  36.  
  37. // 同一の内容の場合、省略する
  38. if (pre == right) return;
  39. pre = right
  40.  
  41. // 各権利者を繰り返す
  42. right.split(/ ?\/ ?/).map((label, i) => {
  43. i && elmRight.appendChild(document.createTextNode(" / "));
  44.  
  45. // 検索リンクを追加する
  46. let elmLink = document.createElement('a');
  47. elmLink.href = `https://open.spotify.com/search/label:"${label}"`;
  48. elmLink.target = '_blank';
  49. elmLink.textContent = label;
  50.  
  51. elmRight.appendChild(elmLink);
  52. });
  53.  
  54. // 反映の監視を終了する(重複クリアは許容する)
  55. clearInterval(timer);
  56. });
  57. }, 100);
  58. });