Spotify - Add label search link

add label search link

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Spotify - Add label search link
// @description  add label search link
// @author       to
// @namespace    https://github.com/to
// @version      0.1
// @license      MIT
// @match        https://open.spotify.com/*
// @icon         https://www.google.com/s2/favicons?domain=spotify.com
// ==/UserScript==

// ページ遷移を監視する
function watchLocation(match, callback) {
  let page;
  setInterval(() => {
    // ページ遷移が発生したか?
    if (page != location.href) {
      page = location.href;

      // 対象のページか?
      if (match.test(page))
        callback(page);
    }
  }, 500);
}

watchLocation(/\/album\//, () => {
  // DOMへの反映を待つ
  let timer = setInterval(() => {
    // 権利表記を繰り返す
    let pre;
    [...document.querySelectorAll('div[aria-colcount] + div p')].map(elmRight => {
      // 表記をクリアする
      let right = elmRight.textContent.replaceAll(/(\℗ |© |(\(P\))?\d{4} )/g, '');
      elmRight.innerHTML = '';

      // 同一の内容の場合、省略する
      if (pre == right) return;
      pre = right

      // 各権利者を繰り返す
      right.split(/ ?\/ ?/).map((label, i) => {
        i && elmRight.appendChild(document.createTextNode(" / "));

        // 検索リンクを追加する
        let elmLink = document.createElement('a');
        elmLink.href = `https://open.spotify.com/search/label:"${label}"`;
        elmLink.target = '_blank';
        elmLink.textContent = label;

        elmRight.appendChild(elmLink);
      });

      // 反映の監視を終了する(重複クリアは許容する)
      clearInterval(timer);
    });
  }, 100);
});