Spotify - Add label search link

add label search link

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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);
});